前言
最近做个项目,java 开发的基于springboot写的服务。这里记录一下关于如何编写pom.xml文件中build标签,仅供参考。如果使用,需要修改一些配置属性,例如
project_path
mainClass
配置文件
<properties>
<project_path>ptronics</project_path>
</properties>
<build>
<!--指定资源的位置 (class文件中包含mapper)-->
<resources>
<!--指定将java下面的mapper文件放入到class中-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--这个要指定(打包的时候会test错误)-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<!-- 打包时不打包test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--打包jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--过滤资源配置文件,方便动态配置-->
<excludes>
<exclude>*.properties</exclude>
<exclude>logback-boot.xml</exclude>
<exclude>*.sh</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前缀-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本标识-->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口类-->
<mainClass>com.test.PtronicsApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
<Class-Path>./ptronics_resources/</Class-Path>
</manifestEntries>
</archive>
<!--上面配置的属性,可以动态设置-->
<outputDirectory>${project.build.directory}/${project_path}</outputDirectory>
</configuration>
</plugin>
<!--拷贝依赖jar包 copy-dependencies-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/${project_path}/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--拷贝资源文件 copy-resources-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<!--设置需要打包的资源文件所在文件夹-->
<directory>src/main/resources</directory>
<!-- 过滤不需要打包的文件-->
<exclude>*.sh</exclude>
<!-- 开启插件过滤,可以使用其他配置文件中定义的属性-->
<filtering>true</filtering>
</resource>
</resources>
<outputDirectory>${project.build.directory}/${project_path}/ptronics_resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
<includes>
<include>
<groupId>null</groupId>
<artifactId>null</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--使用外部配置文件,jar包里没有资源文件-->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}/${project_path}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
<classifier>${project_path}</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- 文件移动插件 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- <property name="dist">target/build</property>-->
<!-- <property name="res">target/build/resources</property>-->
<!-- <property name="dist-tmp">target/build/tmp</property>-->
<!-- <property name="app-name">${project.artifactId}-${project.version}</property>-->
<!-- <property name="real-app-name">${project.artifactId}</property>-->
<!-- <delete dir="${dist}" />-->
<!-- <mkdir dir="${dist}" />-->
<!-- <mkdir dir="${dist-tmp}" />-->
<!-- <mkdir dir="${res}" />-->
<!-- <copy file="target/${app-name}.jar" tofile="${dist-tmp}/${app-name}.jar" />-->
<!-- <move file="target/${app-name}-classes.jar" tofile="${dist}/${real-app-name}.jar"/>-->
<!-- <move todir="${dist}/lib">-->
<!-- <fileset dir="target/lib" />-->
<!-- </move>-->
<!-- <copy todir="${res}">-->
<!-- <fileset dir="target/classes">-->
<!-- <include name="**/*.properties" />-->
<!-- <include name="**/*.xml" />-->
<!-- <include name="**/*.yml" />-->
<!-- </fileset>-->
<!-- </copy>-->
<!-- 路径中不能使用定义的属性,需要自己定义 -->
<property name="dest">target/${project_path}</property>
<copy todir="${dest}">
<fileset dir="src/main/resources">
<include name="*.sh"/>
</fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
打包结果:
具体可以自己执行,修改为自己想要的名字。
注意:
正式发布版本的时候需要去除一些依赖,比如dev-tools等插件。
同时跳过测试,使用mvn命令行编译:
在src同级目录执行下列命令:
mvn clean package -Dmaven.test.skip=true
当然可以参考maven说明网址
https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/maven-plugin/usage.html