最近負責一個純maven項目(項目需求盡量輕量化),需要自己完成打包工作.
因此,基於maven-compiler-plugin以及maven-shade-plugin完成項目的打包工作.
其中:
maven-compiler-plugin負責項目編譯;maven-shade-plugin負責最終的打包操作.
以下所示操作,均在pom.xml文件中進行.
項目基本屬性
<groupId>com.test</groupId>
<artifactId>app</artifactId> //
<version>0.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
添加插件
<build>
<plugins>
<!-- 編譯java項目-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- 創建一個fat jar,包含所有必須的依賴 -->
<!-- 根據自己的main函數替換<mainClass>...</mainClass>中的配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<!-- 指定不需要打包的文件(可以使用通配符) -->
<excludes>
<exclude>org.apache.flink:force-shading</exclude>
<exclude>com.google.code.findbugs:jsr305</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>log4j:*</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<!-- Do not copy the signatures in the META-INF folder.
Otherwise, this might cause SecurityExceptions when using the JAR. -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.test.app.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Tips:
maven-shade-plugin比較強大,還可以解決打包文件中的依賴沖突,讀者可以自行尋找相關文章.
PS:
如果您覺得我的文章對您有幫助,請關注我的微信公眾號,謝謝!

