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的更多相关文章

  1. 五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链

    买买买结算系统 一年一度的双十一购物狂欢节就要到了,又到剁手党们开始表演的时刻了.当我们把种草很久的商品放入购物车以后,点击"结算"按钮时,就来到了买买买必不可少的结算页面了.让我 ...

  2. SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL

    1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...

  3. 在SpringBoot项目中添加logback的MDC

    在SpringBoot项目中添加logback的MDC     先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...

  4. 自身使用的springboot项目中比较全的pom.xml

    在学习的时候常建新的项目,mark下商用的jar <dependency> <groupId>org.mybatis</groupId> <artifactI ...

  5. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

  6. 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用

    mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...

  7. Springboot项目中 前端展示本地图片

    Springboot项目中 前端展示本地图片 本文使用的是Springboot官方推荐的thymeleaf(一种页面模板技术) 首先在pom文件加依赖 <dependency> <g ...

  8. 国际化的实现i18n--错误码国际化以及在springboot项目中使用

    国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母); 主要涉及3个类: Locale用来设置定制的语言和国家代码 Resource ...

  9. springboot 项目中获取默认注入的序列化对象 ObjectMapper

    在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...

随机推荐

  1. 在Rails中最方便集成使用Bootstrap的方式

    创建项目 rails new BootstrapProject 创建模型 rails g scaffold xxx --skip-stylesheets 运行迁移 rake db:migrate -- ...

  2. HDU-2389-Rain on your Parade (最大匹配,kopcroft-karp)

    链接: https://vjudge.net/problem/HDU-2389 题意: You're giving a party in the garden of your villa by the ...

  3. Spring对单例的底层实现,单例注册表

    Spring框架对单例的支持是采用单例注册表的方式进行实现的,源码如下: public abstract class AbstractBeanFactory implements Configurab ...

  4. contenteditable 光标定位到最后

    在Vue做项目时,做了一个div[contenteditable=true]的组件作为文本输入框 在非手动输入值后,光标会丢失,经测试以下这段代码可用,直接将光标定位到最后 function keep ...

  5. CF1260F

    题目大意 一棵树,每个节点的权为L[i]~R[i],一棵树的贡献为\(\sum\limits_{h_{i} = h_{j}, 1 \le i < j \le n}{dis(i,j)}\),其中\ ...

  6. 新建com组件项目步骤

    一.菜单栏  新建->项目->ATL->ATL项目->动态链接库 后续默认完成二.菜单栏  项目->添加类->ATL控件->“写入类的命名如:CeshiMai ...

  7. BZOJ1491 Red is good

    题目链接:Click here Solution: 考虑设\(f(i,j)\)表示当前还有\(i\)张红牌,\(j\)张黑牌时的期望收益 易得状态转移方程:\(f(i,j)=\frac{i}{i+j} ...

  8. Android图片缩放 指定尺寸

    //使用Bitmap加Matrix来缩放     public static Drawable resizeImage(Bitmap bitmap, int w, int h)     {       ...

  9. Spring Boot教程(二十一)开发Web应用(2)

    在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. @Controller public class HelloController { ...

  10. HDU2294--Pendant(DP,矩阵优化)

    Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...