使用springboot的童鞋们,有没曾经想把项目打包成 bin/conf/libs/logs 等这样的结构然后直接运行的,但是找了很多办法都不够完美(因为G是个完美主义
)
![14.gif](/assets/TQEditor-2.3.8/face/14.gif)
好了直接来看解决方案:
1、修改执行打包的子工程的pom.xml文件(加入两个插件):
<build>
<finalName>app</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>script/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<skip>true</skip>
<archive>
<manifest>
<mainClass>com.xxx.bbb.MyMainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
<Built-By>Wangl.sir</Built-By>
<Built-Mail-By>983708408@qq.com</Built-Mail-By>
<Bundle-Name>Project Description </Bundle-Name>
<Implementation-Vendor></Implementation-Vendor>
<Implementation-URL>http://www.xxx.com/wiki</Implementation-URL>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>app-${project.version}-bin</finalName>
<!-- not append assembly id in release file name -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、新增assemble配置文件(保存至src/main/assemble/package.xml):
<assembly 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/assembly-1.0.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<!-- src/main/resources/script目录保存的如:app_ctrl.sh、app_start.sh、app_stop.sh等脚本,maven-assemble会将此目录下的.sh文件复制到最后生成的bin目录下 -->
<directory>src/main/resources/script</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>libs</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>logs</outputDirectory>
<excludes>
<exclude>*.*</exclude>
<exclude>script/**</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>libs</outputDirectory>
<scope>runtime</scope>
<excludes>
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
3、最后就只需执行 mvn clean install -DskipTests
等待maven执行完成,在target目录下会出现app-{project.version}-bin.zip