最近因项目须要,须要将项目打成jar包运行,项目为普通jar包,在pom配置以下:java
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <!-- 指定main方法所在类 --> <mainClass>com.xxx.Main</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>
打包完成后,会将全部依赖的jar包打成一个jar文件,命令行使用apache
java -jar xxx.jar
运行。不过,在运行时发现一个问题:使用了maven多模块开发,相同名称的配置在多个模块中都存在,打包后,配置文件并非所指望的,所以放弃了该方法。maven
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 指定main方法所在类 --> <mainClass>com.xxx.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
一样,打包完成后,会将全部依赖的jar包打成一个jar文件,命令行使用命令行
java -jar xxx.jar
运行。该方法打包后,配置文件为所指望的配置文件,所以使用了此打包方法。 特此记录。code