项目的目录结构如图:
下面是主要的配置文件内容:
assembly.xml
<?xml version="1.0" encoding="UTF-8"?> <assembly> <id>package</id> <formats> <format>zip</format> <format>dir</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>target/appassembler</directory> <outputDirectory>/</outputDirectory> <excludes> <exclude>**/spring-boot-devtools*</exclude> </excludes> </fileSet> <fileSet> <directory>src/main/resources</directory> <outputDirectory>/conf</outputDirectory> <excludes> <exclude>*.yml</exclude> <exclude>logback.xml</exclude> </excludes> </fileSet> </fileSets> </assembly>
deploy目录存放不同环境下的配置文件
pom.xml添加如下配置:
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <jvm.args>-Xms3g -Xmx3g</jvm.args> <app.resources>src/main/resources</app.resources> </properties> </profile> <profile> <id>test</id> <properties> <jvm.args>-Xms3g -Xmx3g</jvm.args> <app.resources>src/main/deploy/test</app.resources> </properties> </profile> <profile> <id>prod</id> <properties> <jvm.args>-Xms3g -Xmx3g</jvm.args> <app.resources>src/main/deploy/prod</app.resources> </properties> </profile> </profiles> <build> <finalName>${project.artifactId}</finalName> <resources> <!--指定src/main/resources资源要过滤--> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.xxg.Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>1.7</version> <configuration> <repositoryLayout>flat</repositoryLayout> <!-- 在脚本的classpath开始增加配置文件路径 --> <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath> <!-- 生成配置文件路径 --> <configurationDirectory>conf</configurationDirectory> <!-- 配置文件原路径 --> <configurationSourceDirectory>${app.resources}</configurationSourceDirectory> <!-- 从原配置文件复制 --> <copyConfigurationDirectory>true</copyConfigurationDirectory> <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath> <!-- 生成哪几种平台的脚本 --> <platforms> <platform>windows</platform> <platform>unix</platform> </platforms> <!-- 生成脚本的后缀 --> <binFileExtensions> <unix>.sh</unix> <windows>.bat</windows> </binFileExtensions> <!-- 使用classpath通配符 --> <useWildcardClassPath>true</useWildcardClassPath> <!-- JVM参数 --> <extraJvmArguments>${jvm.args}</extraJvmArguments> <!-- 依赖包目录 --> <repositoryName>lib</repositoryName> <programs> <program> <!-- springboot项目主启动类 --> <mainClass>xxxx.xxx.xx.xxx</mainClass> <name>main</name> </program> </programs> </configuration> <!-- 绑定生命周期 --> <executions> <execution> <id>appassembler</id> <phase>package</phase> <goals> <goal>assemble</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <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-dependency-plugin</artifactId> <version>2.10</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 可执行jar插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>ssp.lanxinpay.settlement.MainApp</mainClass> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> <excludes> <exclude>**/*.xml</exclude> <exclude>**/*.conf</exclude> <exclude>**/*.properties</exclude> <exclude>**/*.yml</exclude> </excludes> </configuration> </plugin> <!-- maven资源文件复制插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>copy-resources</id> <!-- here the phase you need --> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/etc</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <exclude>**/*.xml</exclude> <exclude>**/*.conf</exclude> <exclude>**/*.properties</exclude> <exclude>**/*.yml</exclude> </includes> <filtering>true</filtering> </resource> </resources> <encoding>UTF-8</encoding> </configuration> </execution> </executions> </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>${project.build.directory}/etc/lib</outputDirectory> <!-- 是否不包含间接依赖 --> <excludeTransitive>false</excludeTransitive> <!-- 忽略版本 --> <stripVersion>false</stripVersion> </configuration> </execution> </executions> </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.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>--> </plugins> </build>
按照这种配置打包出来会生成zip包和ja包。以及针对不同操作系统生成不同的启动脚本。