默認情況下,使用maven打包的jar項目(執行maven install)不會包含其他包引用,要想打包為帶其他項目引用的jar,需要加入插件
要得到一個可以直接在命令行通過java命令運行的JAR文件,還要滿足兩個條件:
1、JAR包中的/META-INF/MANIFEST.MF元數據文件必須包含Main-Class信息。
2、項目的依賴包也要合並在打的jar包中,即項目所有的依賴都必須在Classpath中
有多種插件可以完成這個任務:
1、這種方式需要使用mvn package命令來執行
<plugin> <!-- 打包為jar,goal為package --> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.guangshan.framework.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
默認命令為mvn assembly:assembly,加了executions之后添加了一些其他命令
2、maven-shade-plugin,它可以讓用戶配置Main-Class的值,然后在打包的時候將值填入/META-INF/MANIFEST.MF文件。關於項目的依賴,它很聰明地將依賴JAR文件全部解壓后,再將得到的.class文件連同當前項目的.class文件一起合並到最終的CLI包中,這樣,在執行CLI JAR文件的時候,所有需要的類就都在Classpath中了。
同樣是mvn package
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>hello.SayHello</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
3、稍微復雜一點
默認情況下,maven的默認打包:
對應於同樣的package生命周期階段,Maven為jar項目調用了maven-jar-plugin,為war項目調用了maven-war-plugin,換言之,packaging直接影響Maven的構建生命周期。了解這一點非常重要,特別是當你需要自定義打包行為的時候,你就必須知道去配置哪個插件。
maven-war-plugin可以讀取Eclipse項目的配置,所以直接打的包就包含了其他的包引用(Deployment Assembly設置里面)
maven-jar-plugin則不會包含引用的包,所以需要使用插件把依賴包打進來。
Web 工程的輸出發布包沒什么好說的,因為 <packaging>war
</packaging>,所以 mvn package
出來的 WAR 包里就有站點運行的所有內容了,用到的依賴會在 WEB-INF/lib 目錄下列着。
而對於那些 <packaging>jar
</packaging> 的工程,用 mvn package
只會生成一個 JAR 包,它所依賴的各個類庫仍然分散在本地倉庫中,而我們的發布包應該包含這些第三方依賴的。
在pom.xml中添加jar和dependency插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>cc.unmi.Main</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>pre-package</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.50</version> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWrite>true</overWrite> </artifactItem> <artifactItem> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.3-SNAPSHOT</version> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWrite>true</overWrite> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>