-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
打一个可执行Jar包,并把配置、依赖项单独放置
- maven-resources-plugin:拷贝配置文件到指定文件夹,例如:conf/
- maven-dependency-plugin:拷贝依赖项到指定文件夹,例如:lib/
- maven-jar-plugin:设置主方法和classpath
<!-- 拷贝配置文件到指定文件夹,例如:conf/ -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<!-- 绑定到生命周期的compile阶段,即执行compile的时候就执行该插件 -->
<phase>compile</phase>
<!-- 使用插件的copy-resources目标 -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--拷贝到构建目录conf文件夹下 -->
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<!-- 需要拷贝的文件夹 -->
<directory>src/main/resources</directory>
<!-- 需要拷贝的文件 -->
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<!-- 排除不用拷贝的文件 -->
<excludes>
<exclude>*.txt</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- 拷贝依赖项到指定文件夹,例如:lib/ -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
<!--拷贝到构建目录的lib文件夹下 -->
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 设置主方法和classpath -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<!-- 绑定到编译阶段 -->
<phase>compile</phase>
<configuration>
<!--排除某些配置文件,放在jar的外面,方面修改,-->
<!--【测试后发现这种写方法“src/main/resources/*”并不能排除文件,这里排除文件的根目录是classpath,不是项目根目录,】 -->
<excludes>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/*.txt</exclude>
</excludes>
<archive>
<!--自动添加META-INF/MANIFEST.MF -->
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF文件里的classPath添加 lib/ 前缀,该插件并不会拷贝依赖jar包到lib文件夹下 -->
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${main.class}</mainClass>
</manifest>
<manifestEntries>
<Class-Path>. conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
打成一个单独的jar包,依赖配置都在这个jar包里
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
使用maven-shade-plugin插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Executable JAR
Resource Transformers
Selecting Contents for Uber JAR
Spring Boot 打包插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.8.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>test.Main</mainClass>
</configuration>
</plugin>