Maven 打胖jar


自己去看原版:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/ 

  一個Eclipse的工程,在pom中配置了若干依賴,需要將pom中所有的依賴全部打包進一個jar包中,可以選擇的方案有maven-assembly-plugin和fatjar。以前采用fatjar進行打包,但是fatjar有不少問題,

1. 最近一次更新是在09年,無法支持新版本的eclipse。

2.支持最高的jdk版本是1.7  

3. 打包速度慢(不是一般的慢)

4. 打成的jar包體積略大。

  下面是一個Eclipse的工程,其中含有不少的maven依賴包:


采用export成runnable jar包的方式是行不通的,正確做法是在工程的pom.xml文件中配置maven-assembly-plugin,pom.xml的配置如下:

復制代碼
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.tomas</groupId>
<artifactId>weather</artifactId>
<version>1.0</version>

<repositories>
<repository>
<id>sonatype-nexus-staging</id>
<name>Sonatype Nexus Staging</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sms</artifactId>
<version>3.0.0-rc1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 打包為可執行胖jar -->


<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.tomas.waether.SendMessage</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

</project>



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM