在使用spring boot开发的时候,会根据环境配置多个application.yml文件(dev,sit等到),在最后发布版本的时候不想把这些不用的配置文件打包到发布包中。在assembly插件的配置文件中也没有找到根据变量排除或加载指定文件的方法,于是就想到了配置多个assembly配置文件(每个配置文件根据环境排查不需要的配置文件),根据pom.xml中的profiles属性指定打包的assembly配置文件。具体的代码如下:
pom.xml中profiles属性配置:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.assembly.package.suffix>dev</profile.assembly.package.suffix>
</properties>
</profile>
<profile>
<id>sit</id>
<properties>
<profile.assembly.package.suffix>sit</profile.assembly.package.suffix>
</properties>
</profile>
</profiles>
pom.xml中assembly插件的配置: (最简化配置)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/assembly/package-${profile.assembly.package.suffix}.xml</descriptor>
</descriptors>
<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
</plugin>
assembly插件的配置文件:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>customer</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>sbin</directory>
<outputDirectory>/sbin</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<excludes>
<exclude>mvn_*</exclude>
<exclude>shutdown.sh</exclude>
<exclude>startup.sh</exclude>
<exclude>assembly*</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/classes</outputDirectory>
<excludes>
<exclude>/com/**/xx/**</exclude> <!-- 排除某个目录下的所有文件 -->
<exclude>/com/**/xxxx/**</exclude>
<exclude>/com/**/xxxxxxx.class</exclude> <!-- 排除某个文件 -->
<exclude>/static/**</exclude>
<exclude>/templates/**</exclude>
</excludes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
<fileSet>
<directory>logs</directory>
<outputDirectory>/logs</outputDirectory>
<excludes>
<exclude>/*.log</exclude>
<exclude>/backup/*.log</exclude>
</excludes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>/lib</outputDirectory>
<excludes> <!-- 排除不需要的依赖包 -->
<exclude>org.springframework:spring-jdbc</exclude>
<exclude>org.springframework.boot:spring-boot-starter-jdbc</exclude>
<exclude>org.springframework.boot:spring-boot-starter-data-jpa</exclude>
<exclude>org.springframework.boot:spring-boot-starter-redis</exclude>
<exclude>org.springframework.data:spring-data-jpa</exclude>
<exclude>org.hibernate.javax.persistence:hibernate-jpa-2.1-api</exclude>
</excludes>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
执行命令:
mvn -f $Project_HOME/pom.xml clean assembly:assembly -DskipTests #跳过测试