Spring Boot 2关于maven打包插件的使用
spring-boot-maven-plugin插件
https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html
该插件可以进行运行、打包spring boot项目
1、直接引用
mvn clean compile -T 4 -D maven.test.skip=true
mvn jar:jar -T 4 -D maven.test.skip=true
mvn jar:test-jar -T 4
mvn package
- 生成springboot相关的jar包
- 复制resources文件
- mybatis配置文件与mapper文件放在一起
<build>
<resources>
<!--资源文件复制-->
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.directory}/classes</targetPath>
</resource>
<!--mybatis配置文件与mapper文件放在一起-->
<resource>
<directory>${project.basedir}/src/main/java</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<configuration>
<mainClass>xx.xxx.Application</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、通过参数spring.config.location区分不同环境
- resources
- a.xml
- b.properties
- env
- dev
- application.yml
- aa.properties
- test
- application.yml
- aa.properties
- prod
- application.yml
- aa.properties
- dev
-Dspring.profiles.active=dev -Dspring.config.location=classpath:/env/dev/
此种方式插件配置与第一种模式一样,就是对于代码中的配置文件需要处理一下
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"${spring.config.location}person.properties"}, encoding = "UTF-8")
public class PersonProperties implements Serializable {
//
}
3、通过参数profiles区分不同环境
mvn clean compile -T 4 -D maven.test.skip=true -D env=dev
mvn jar:jar -T 4 -D maven.test.skip=true
mvn jar:test-jar -T 4
mvn package
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<resources>
<!--资源文件复制-->
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<excludes>
<exclude>env/**</exclude>
</excludes>
</resource>
<!--mybatis配置文件与mapper文件放在一起-->
<resource>
<directory>${project.basedir}/src/main/java</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--不同环境配置文件-->
<resource>
<directory>${project.basedir}/src/main/resources/env/${env}</directory>
<targetPath>${project.build.directory}/classes</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!--https://docs.spring.io/spring-boot/docs/current/maven-plugin/index.html-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<configuration>
<mainClass>xxx.xxx.Application</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>