最后更新時間: 2014年11月23日
1. maven-shade-plugin
2. maven-assembly-plugin
3. maven-onejar-plugin
maven-shade-plugin是我在ebay時前輩介紹給我的,我覺得它使用方便且沒有出現過問題。但是我看別人的源代碼,發現大家用的更多的是assembly,所以這里總結下這兩種插件的用法。至於第三個,先留個坑在這,以后用到再總結。
使用插件maven-shade-plugin可以方便的將項目已jar包的方式導出,插件的好處在於它會把項目所依賴的其他jar包都封裝起來,這種jar包放在任何JVM上都可以直接運行,我最初使用eclipse的maven-build直接打包,轉移到intellij idea后沒有這個按鈕了,就只能用命令行搞了
使用步驟 :
將插件添加到pom.xml中,需要改的地方就是mainClass,在這里指定main方法的位置
使用mvn package打包,最后到projectName/target/下查找目標jar包
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>core.Test</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
Assembly 插件
pom.xml 文件
<!-- Maven Assembly Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <!-- get all project dependencies --> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <!-- MainClass in mainfest make a executable jar --> <archive> <manifest> <mainClass>com.mkyong.core.utils.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
使用步驟:
pom中添加插件依賴,命令行運行mvn package
目前,我對phase, goals, execution的理解還不夠,不知道是什么意思
maven很是個很優秀的項目管理工具,是我在寫C++代碼時朝思暮想的東西,以后我還會用到它,尤其是項目之間依賴的maven解決方法,see you soon!
參考:
[1] Create A fat jar file - maven assembly plugin