首先在项目的pom文件中添加依赖
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 项目启动类 -->
<mainClass>com/cloud/services/cronweb/server/Application</mainClass>
<!-- 依赖的jar的目录前缀 -->
<classpathPrefix>../lib</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<includes>
<!-- 只打包指定目录的文件-->
<include>com/cloud/services/cronweb/server/**</include>
</includes>
<!-- 项目启动jar包排除目录 -->
<excludes>
<exclude>bin/**</exclude>
<exclude>config/**</exclude>
<exclude>logs/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- 生成的tar.gz文件的名字,如果没有设置就默认用pom文件里的artifactId+version-->
<finalName>${project.artifactId}</finalName>
<!-- 属性控制是否在生成的打包文件的文件名中包含assembly id -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
多环境打包:设置profiles变量,一般是设置在pom文件的最下方
<!-- 多环境打包 start -->
<profiles>
<!-- 开发环境配置 -->
<profile>
<id>dev</id>
<properties>
<profilesactive>dev</profilesactive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<!-- 测试环境配置 -->
<profile>
<id>st</id>
<properties>
<profilesactive>st</profilesactive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<!-- 正式环境 -->
<profile>
<id>uat</id>
<properties>
<profilesactive>uat</profilesactive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<!-- 多环境打包 end -->
配置assembly.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>${profileActive}-${project.version}</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>${basedir}/src/main/bin</directory>
<outputDirectory>/bin</outputDirectory>
<fileMode>0777</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 指定输出templates中的文件到templates目录中 -->
<fileSet>
<directory>src/main/resources/templates</directory>
<outputDirectory>config/templates</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<!-- 指定输出static中的文件到static目录中 -->
<fileSet>
<directory>src/main/resources/static</directory>
<outputDirectory>config/static</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<!-- 指定输出resources中的配置文件到conf目录中 -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>application-${profilesactive}.properties</include>
<include>application.properties</include>
<include>quartz.properties</include>
<include>error-message.properties</include>
<include>i18n/*</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.class</exclude>
</excludes>
</fileSet>
<!-- 将项目启动jar打包到boot目录中 -->
<fileSet>
<directory>${basedir}/target</directory>
<outputDirectory>/boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
</fileSets>
<!-- 将第三方依赖打包到lib目录中 -->
<dependencySets>
<!-- copy all third party jars to lib folder. -->
<dependencySet>
<outputDirectory>lib</outputDirectory>
<excludes>
<!-- <exclude>base:*</exclude> -->
</excludes>
</dependencySet>
</dependencySets>
之后项目就能打成tar包了.
shart.sh内容:
nohup java -Xbootclasspath/a:$NEW_CLASSPATH -jar $APPLICATION_JAR --spring.config.location=$NEW_CLASSPATH > log/logs.log 2>&1 &
nohup代表后台运行注意带着结尾的标识符。运行日志输出到log/logs.log中。-Xbootclasspath/a:代表添加路径到class路径中。
pom的resource中要配置项目需要用到的配置文件,不然会出现找不到文件的错误。
assembly.xml主要是配置哪些文件需要打包到文件夹中,并且配置文件夹名字。本项目中所有依赖的jar包都会打包到lib目录下。
最后生成的文件目录
bin
config
-----mapper
-----static
-----templates
boot
lib
原项目目录
java
------bin
------assembly
------resources
------------static
------------templates
------java