springboot项目中使用maven resources
maven resource 组件可以把pom的变量替换到相关的resouces目录中的资源文件变量
示例项目:内容中心 (文章管理) 生成jar包,生成docker ,生成k8s文件
1.项目结构
├── content-api
│ ├── pom.xml
│ └── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── itstudy
│ │ └── content_api
│ │ ├── domain
│ │ │ └── ArticleInfo.java
│ │ └── service
│ │ └── ArticleService.java
│ └── test
│ └── java
│ └── com
│ └── itstudy
│ └── content_api
│ └── AppTest.java
├── content-server
│ ├── pom.xml
│ └── src
│ ├── bin
│ │ └── app.sh
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── itstudy
│ │ │ └── content_server
│ │ │ ├── ContentServerApp.java
│ │ │ ├── controller
│ │ │ │ └── ArticleController.java
│ │ │ └── service
│ │ │ └── impl
│ │ │ └── ArticleServiceImpl.java
│ │ └── resources
│ │ ├── dev
│ │ │ ├── application.yml
│ │ │ ├── docker
│ │ │ │ └── Dockerfile
│ │ │ └── k8s
│ │ │ ├── content-center-rc.yml
│ │ │ └── content-center-svc.yml
│ │ └── pro
│ │ ├── application.yml
│ │ ├── docker
│ │ │ └── Dockerfile
│ │ └── k8s
│ │ ├── content-center-rc.yml
│ │ └── content-center-svc.yml
│ └── test
│ └── java
│ └── com
│ └── itstudy
│ └── content_api
│ └── ContentServerAppTest.java
└── pom.xml
2. 父级项目pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itstudy</groupId>
<artifactId>contentcenter</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>content-api</module>
<module>content-server</module>
</modules> <packaging>pom</packaging>
<name>contentcenter</name> <parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.SR5</version>
<relativePath></relativePath>
</parent> <distributionManagement>
<!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致 -->
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://maven.mysite.com/nexus/content/repositories/demo-Release/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://maven.mysite.com/nexus/content/repositories/demo-Snapshot/</url>
</snapshotRepository>
</distributionManagement> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 使用dockerfile方式
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<imageName>dockerimage.mysite.com/contentcenter/${project.artifactId}</imageName>
<imageTags>contentcenter-${project.version}</imageTags>
<dockerDirectory>src/main/docker</dockerDirectory>
<serverId>docker-image</serverId>
<useConfigFile>true</useConfigFile>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
-->
<!--使用配置方式-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<imageName>dockerimage.mysite.com/contentcenter/${project.artifactId}</imageName>
<baseImage>dockerimage.mysite.com/java/alpine-oraclejdk8:8.131.11-slim</baseImage>
<imageTags>contentcenter-${project.version}</imageTags>
<maintainer>my@mail.com</maintainer>
<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom","-jar", "${project.build.finalName}.jar"]</entryPoint>
<volumes>
<volume>/tmp</volume>
</volumes>
<serverId>docker-image</serverId>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2实体类
package com.itstudy.content_api.domain;
public class ArticleInfo {
private String id;
private String content;
//省略geter setter
}
2.3接口
package com.itstudy.content_api.service;
import com.itstudy.content_api.domain.ArticleInfo;
public interface ArticleService {
void insert(ArticleInfo item);
void update(ArticleInfo item);
}
3 content-server项目 具体业务服务
3.1 pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>contentcenter</artifactId>
<groupId>com.itstudy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>content-server</artifactId> <name>content-server</name> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itstudy</groupId>
<artifactId>content-api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> <profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile-name>pro</profile-name>
<app-mongodb-replica-set>192.168.146.8:27017,192.168.146.9:27017,192.168.146.10:27017</app-mongodb-replica-set>
<app-mongodb-credentials>username:password@dbname-test</app-mongodb-credentials>
<app-mongob-db>dbname-test</app-mongob-db>
<plateform-ds>http://192.168.146.76:50002/eureka/</plateform-ds>
<app-dfs1>192.168.146.11:22122</app-dfs1>
<app-dfs2>192.168.146.11:22122</app-dfs2>
<app-dfs-sotimeout>1501</app-dfs-sotimeout>
<app-dfs-connecttimeout>601</app-dfs-connecttimeout>
<app-name>${project.name}</app-name>
<app-port>8080</app-port>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profile-name>pro</profile-name>
<app-mongodb-replica-set>192.168.146.8:27017,192.168.146.9:27017,192.168.146.10:27017</app-mongodb-replica-set>
<app-mongodb-credentials>username:password@dbname</app-mongodb-credentials>
<app-mongob-db>dbname</app-mongob-db>
<plateform-ds>http://ds:8761/eureka/</plateform-ds>
<app-dfs1>192.168.146.11:22122</app-dfs1>
<app-dfs2>192.168.146.11:22122</app-dfs2>
<app-dfs-sotimeout>1501</app-dfs-sotimeout>
<app-dfs-connecttimeout>601</app-dfs-connecttimeout>
<app-port>8080</app-port>
<app-name>${project.name}</app-name>
</properties>
</profile>
</profiles> <build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>dev/**</exclude>
<exclude>pro/**</exclude>
</excludes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources/${profile-name}</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/Dockerfile</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources/</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!--不加此组件,会导至jar包启动时找不到manifest-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3.2 service.impl
package com.itstudy.content_server.service.impl; import com.itstudy.content_api.domain.ArticleInfo;
import com.itstudy.content_api.service.ArticleService;
import org.springframework.stereotype.Service; @Service
public class ArticleServiceImpl implements ArticleService {
@Override
public void insert(ArticleInfo item) { } @Override
public void update(ArticleInfo item) { }
}
3.3 controller
package com.itstudy.content_server.controller; import com.itstudy.content_api.domain.ArticleInfo;
import com.itstudy.content_api.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/article")
public class ArticleController { @Autowired
ArticleService articleService; @PostMapping("/insert")
public void postInsert(@RequestBody ArticleInfo info){
//其它业务
} @PostMapping("/update")
public void postUpdate(@RequestBody ArticleInfo info){
//其它业务
}
}
3.4 springboot 启动类app
package com.itstudy.content_server; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /**
* 项目启动类
*/
@SpringBootApplication
@EnableEurekaClient
public class ContentServerApp
{
public static void main( String[] args )
{
System.out.println("内容中心-启动中..."); SpringApplication.run(ContentServerApp.class, args); System.out.println("内容中心-启动成功");
}
}
3.5 resources目录
3.5.1 resources/dev/docker/Dockerfile
FROM dockerimage.mysite.com/java/alpine-oraclejdk8:8.131.11-slim
VOLUME /tmp
ADD @project.build.finalName@.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"
3.5.2 resource/dev/k8s/rc 及 resources/dev/k8s/svc
1) rc文件
apiVersion: v1
kind: ReplicationController
metadata:
name: @project.artifactId@
spec:
replicas: 1
selector:
name: @project.artifactId@
template:
metadata:
labels:
name: @project.artifactId@
spec:
containers:
- name: api
image: dockerimage.mysite.com/@app-name@/@app-name@
ports:
- containerPort: 8080
2)svc文件
apiVersion: v1
kind: Service
metadata:
name: @app-name@ #也可替换为 @project.artifactId@
spec:
selector:
name: @app-name@ #也可替换为 @project.artifactId@
ports:
- name: http
port: 8080
protocol: TCP
nodePort: 30008
type: NodePort
3.6 application.yml文件
server:
port: @app-port@
spring:
application:
name: @app-name@ # 项目名称尽量用小写
jmx:
enabled: false
hystrix:
command:
default:
execution:
timeout:
enabled: false
eureka:
client:
serviceUrl:
defaultZone: @plateform-ds@ # 指定注册中心的地址
instance:
preferIpAddress: true
fdfs:
soTimeout: @app-dfs-sotimeout@
connectTimeout: @app-dfs-connecttimeout@
thumbImage: #缩略图生成参数
width: 150
height: 150
trackerList: #TrackerList参数,支持多个
- @app-dfs1@
- @app-dfs2@
3.7 resource/pro文件夹与dev文件夹文件相似,只是个别配置不同。
4.重要总结
4.1 在pom文件中,引用maven变量使用 ${变量名}即可
4.2 在springboot项目中,使用maven resources组件时,使用@变量名@ 进行取值(因为springboot项目中进行了特殊处理)
4.3 在使用maven resources组件时
<excludes>
<exclude>dev/**</exclude>
<exclude>pro/**</exclude>
</excludes> * 匹配 0或N个字符
** 匹配 0或N个目录
如果使用*不能实现相应功能,请切换** 即可
4.4 在使用maven resources组件时,只要增加了resource这个节点,必须重新指定resources文件夹位置
如示例: https://www.cnblogs.com/liuxm2017/p/10688789.html
springboot项目中使用maven resources的更多相关文章
- 五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
买买买结算系统 一年一度的双十一购物狂欢节就要到了,又到剁手党们开始表演的时刻了.当我们把种草很久的商品放入购物车以后,点击"结算"按钮时,就来到了买买买必不可少的结算页面了.让我 ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 自身使用的springboot项目中比较全的pom.xml
在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...
- SpringBoot项目中遇到的BUG
1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...
- 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用
mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...
- Springboot项目中 前端展示本地图片
Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...
- 国际化的实现i18n--错误码国际化以及在springboot项目中使用
国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
随机推荐
- 【30分钟学完】canvas动画|游戏基础(extra1-1):美图我也行
前言 本文是接续系列教程的extra1,主要是介绍颜色系统在canvas中的应用. 本来是与extra1一起成文的,因为segmentfault莫名其妙的字数限制bug只能分割放送了. canvas操 ...
- Docker报错解决
今天我在学习docker的时候,用docker安装nginx的时候报了如下错误: 尝试了卸载docker重装,删除nginx,删除nginx镜像文件都没有用,最后发现还是Linux和docker版本兼 ...
- electron打包成.exe后限制只启动一个应用
注意:这是2.x的文档 const {app} = require('electron') let myWindow = null const shouldQuit = app.makeSingleI ...
- z-tree官方提供的下拉菜单案例
1.z-tree官方提供的下拉菜单案例 <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - selec ...
- R-CNN常见问题
可以不进行特定样本下的微调吗?可以直接采用AlexNet CNN网络的特征进行SVM训练吗? 不针对特定任务进行微调,而将CNN当成特征提取器,pool5层得到的特征是基础特征,类似于HOG.SIFT ...
- scrapy项目1:爬取某培训机构老师信息(spider类)
1.scrapy爬虫的流程,可简单该括为以下4步: 1).新建项目---->scrapy startproject 项目名称(例如:myspider) >>scrapy.cfg为项目 ...
- Json和XML的一些差别
XML: 扩展标记语言,可以用来标记数据.定义数据类型, 优缺点: 1.格式统一,符合标准: 2.容易与其他系统进行远程交互,数据共享比较方便 3.XML文件庞大,文件格式复杂,传输占带宽,较复杂 J ...
- ELK5+redhat7.4配置elasticsearch集群
ELK介绍 ELK是三个开源软件的缩写,即elasticsearch.logstack.kibana. Elasticsearch:开源分布式搜索引擎,提供搜集.分析.存储数据三大功能.它的特点有:分 ...
- selectKey 标签
原文: https://blog.csdn.net/Sun_of_Rainy/article/details/81564433 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数 ...
- LeetCode_1114.按顺序打印(多线程)
LeetCode_1114 LeetCode-1114.按顺序打印 我们提供了一个类: public class Foo { public void one() { print("one&q ...