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 ,如果想要格式 ...
随机推荐
- 【NOIP2016提高A组五校联考2】tree
题目 给一棵n 个结点的有根树,结点由1 到n 标号,根结点的标号为1.每个结点上有一个物品,第i 个结点上的物品价值为vi. 你需要从所有结点中选出若干个结点,使得对于任意一个被选中的结点,其到根的 ...
- Python 元组Ⅱ
删除元组 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下实例: 以上实例元组被删除后,输出变量会有异常信息,输出如下所示: 元组运算符 与字符串一样,元组之间可以使用 + ...
- postman—使用newman来执行postman脚本
我们知道postman是基于javascript语言编写的,而导出的json格式的postman脚本也无法直接在服务器运行,它需要在newman中执行(可以把newman看做postman脚本的运行环 ...
- Composite UI Application Block(CAB)
序言 资料 https://www.cnblogs.com/lglruirui/archive/2010/06/21/1761737.html?tdsourcetag=s_pcqq_aiomsg ht ...
- uiautomatorviewer报错 Error taking device screenshot: EOF
报以下错误 估计是端口冲突 解决方法: 1. netstat -ano | findstr 5037 查看占用5037端口的进程 2. taskkill /pid 10508 /f 杀掉此进程 3 ...
- 将数据库中带出的列,在gridview中影藏起来
前台增加事件:OnRowCreated="GridView1_RowCreated" protected void GridView1_RowCreated(object send ...
- sed将一个文件插入到另一个文件(合并两个文件)
将before.sh的内容插入到catalina.sh的第一行之后 sed -i '1r /srv/tomcat8/bin/before.sh' /srv/tomcat8/bin/catalina.s ...
- mysql命令使用2
mysql查询默认不区分大小写,如果需要区分大小写,使用binary mysql>select * from teacher where binary name='niu'; mysql查询默认 ...
- Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm
Delphi XE2 之 FireMonkey 入门(36) - 控件基础: TForm 当我第一次读取 Form1.StyleLookup 并期待出现 "formstyle" 时 ...
- Selenium学习之==>三种等待方式
在UI自动化测试中,必然会遇到环境不稳定,网络慢的情况,这时如果你不做任何处理的话,代码会由于没有找到元素,而报错.这时我们就要用到wait(等待),而在Selenium中,我们可以用到一共三种等待, ...