最终打包后的目录结构
│ start.sh
├───config
│ application-dev.yml
│ application-prod.yml
│ application.yml
│ ehcache.xml
│ logback.xml
├───logs
├───lib
│ spring-context-support-5.1.2.RELEASE.jar
│ spring-core-5.1.2.RELEASE.jar
│ spring-oxm-5.1.2.RELEASE.jar
│ spring-retry-1.2.2.RELEASE.jar
│ spring-tx-5.1.2.RELEASE.jar
│ stax2-api-3.1.4.jar
│ woodstox-core-5.0.3.jar
│ zkclient-0.10.jar
│ zookeeper-3.4.11.jar
│ .........
└───logs
pom.xml
<build>
<plugins>
<!-- Compiler 插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- resource插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>cn.wisec.meerkat</groupId>
<artifactId>meerkat-slice</artifactId>
</include>
</includes>
</configuration>
</plugin>
<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>target/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions> <!--执行器 mvn assembly:assembly -->
<execution>
<id>make-zip</id><!--名字任意 -->
<phase>package</phase><!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal><!-- 只运行一次 -->
</goals>
<configuration>
<descriptors> <!--描述文件路径 -->
<descriptor>src/main/bin/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
src/main/bin/assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/config/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>lib/*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>start.sh</include>
<include>logs</include>
</includes>
</fileSet>
</fileSets>
</assembly>
src/main/bin/start.sh
#!/bin/sh
dir=`cd $(dirname $0); pwd -P`
jar=$(find ./lib -type f -name meerkat-slice-*.jar)
echo $jar
nohup java -server -Duser.dir=$dir -XX:-UseGCOverheadLimit -Xms1024m -Xmx30720m -XX:PermSize=256M -XX:MaxPermSize=512M -Dloader.path=./lib,./config -jar $jar > /dev/null &
注意事项
- 不同的项目需要在pom.xml/build/plugins中
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>cn.wisec.meerkat</groupId>
<artifactId>meerkat-slice</artifactId>
</include>
</includes>
</configuration>
</plugin>
- 在start.sh中将
jar=$(find ./lib -type f -name meerkat-slice-*.jar)
中的meerkat-slice改成你打包工程的artifactId