用于自定义打包格式
使用方式
<build> <plugins> <!--不采用spring boot的打包方式,我们只--> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- </plugin>--> <!--自定义打包方式 排除所有资源文件 因为我们的资源文件都在conf里面 这样会在我们的jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <!--会在META-INF/MANIFEST.MF 指定main--> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.liqiang.springbootenvironment02.SpringBootEnvironment02Application</mainClass> </manifest> </archive> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.yml</exclude> <exclude>static/**</exclude> <exclude>public/**</exclude> <exclude>templates/**</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version> <executions> <execution><!--执行器 mvn assembly:assembly--> <id>assemble</id><!--名字任意 --> <goals> <goal>single</goal> <!-- 该打包任务只运行一次 --> </goals> <phase>package</phase><!-- 绑定到package生命周期阶段上 --> </execution> </executions> <configuration> <appendAssemblyId>false</appendAssemblyId> <attach>false</attach> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <!--使用默认的变量分割符即${} 可以自己定义格式--> <configuration> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> <version>3.1.0</version> </plugin> </plugins> </build>
</profiles> <profile> <id>release</id> <activation> <!--maven打包的变量名和参数名字 如: mvn clean install -Dmaven.test.skip -Denv=release--> <property> <name>env</name> <value>release</value> </property> </activation> <!--占位符定义--> <properties> <spring.profiles.active>pro</spring.profiles.active> </properties> <build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <!--这个配置的意思是过滤上面指定属性文件中的占位符,占位符是${变量名称}这样的形式,maven会自动读取includes配置文件,然后解析其中的占位符,使用上面pom文件中定义的属性进行替换--> <filtering>true</filtering> <includes> <include>*.*</include> </includes> </resource> </resources> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!--自定义打包xml文件--> <descriptors> <descriptor>${basedir}/src/main/assembly/release.xml</descriptor> </descriptors> <!--打包文件名字--> <finalName>spring-boot-environment02</finalName> <!--打包输出目录--> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin> </plugins> </build> </profile> </profiles>
/src/main/assembly/release.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>dist</id> <formats> <!-- 指定打包格式。maven-assembly-plugin插件支持的打包格式有zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、jar、dir、war,可以同时指定多个打包格式 --> <format>tar.gz</format> </formats> <!--指定打的包是否包含打包层目录,比如finalName是terminal-dispatch,当值为true,所有文件被放在包内的terminal-dispatch目录下,否则直接放在包的根目录下,--> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>.</directory> <outputDirectory>/</outputDirectory> <includes> <include>README*</include> </includes> </fileSet> <!--打包启动脚本--> <fileSet> <directory>./src/main/bin</directory> <outputDirectory>bin</outputDirectory> <includes> <include>**/*</include> </includes> <!-- 文件文件执行权限 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限; 0644->即用户具有读写权限,组用户和其它用户具有只读权限; --> <fileMode>0755</fileMode> </fileSet> <fileSet> <!--将target class下 的打包到conf 因为我们可能用resource占位--> <directory>./target/classes</directory> <outputDirectory>/conf</outputDirectory> <includes> <include>application-dev.yml</include> <include>application.yml</include> <include>logback.xml</include> <include>static/**</include> <include>templates/**</include> </includes> </fileSet> </fileSets> <!--所有依赖放在lib包--> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <excludes> <exclude>junit:junit</exclude> </excludes> </dependencySet> </dependencySets> </assembly>
执行
mvn clean install -Dmaven.test.skip -Denv=release