1. Idea自身打包方式
1.1 創建Artifacts
快捷鍵(Ctrl+Alt+Shift+S)打開項目的Project Structure。在Artifacts創建
接着,指定main class,如下:
最后,得到創建得到的artifacts
注:
這里可以通過“+”或者“-”選擇那些需要打入artifacts的第三方jar文件,如圖:
1.2 打包Artifacts
在菜單欄目選Build,最后一欄Build Artifacts
最后,在出現的窗口創建
得到打包結果:
2. maven打包方式
maven打包的步驟:
- 先mvn clean,再 package即可
- 如需把項目編譯成jar包,打進maven倉庫里面,可以運行install命令
2.1 maven-jar-plugins 無依賴第三方jar的打包
maven 默認打包插件,用來創建 project jar
pom.xml配置如下:
<!--①無依賴其他任何jar打包--> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.conf</include> </includes> </resource> </resources> <plugins> <!--scala編譯打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java編譯打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <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>com.swordfall.restserver.base.WebServer</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
運行:在target中找到打包出來的xxx.jar包,運行java -jar xxx.jar即可,但是如果程序有依賴其他包,比如程序依賴jdbc去查詢db,這時候再執行就會出現找不到jdbc依賴,因為我們並沒有將依賴包打進去。
2.2 maven-assembly-plugins 解決依賴第三方jar包,並可執行jar的打包
支持定制化打包方式,更多是對項目目錄的重新組裝。以下方式用的比較少,因為我們依賴的jar,也會打進到我們最終生成的jar中,會導致一個jar文件過大,而且如果要給第三方使用,最好給一個純凈的。
pom.xml配置如下:
<!--②解決依賴第三方,可執行jar的打包,全量打包--> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.conf</include> </includes> </resource> </resources> <plugins> <!--scala編譯打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java編譯打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.swordfall.restserver.base.WebServer</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2.3 maven-assembly-plugins zip打包
支持定制化打包方式,更多是對項目目錄的重新組裝。在2.2基礎上,我們還可以利用assembly,將我們依賴的jar歸類,打包成一個zip。發布項目的時候,將zip包copy到服務器上,直接unzip xxx.zip,里面包含要運行的jar以及依賴的lib,還有配置的config文件,還可以包括執行腳本,可以直接啟動服務。
<!-- ③maven-assembly-plugin --> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!-- 排除外置的配置文件(運行時注釋上,使IDE能讀到配置文件;打包時放開注釋讓配置文件外置,方便修改)可以不配置,maven-jar-plugin下面已配置 --> <!--<excludes> <exclude>config.properties</exclude> </excludes>--> </resource> <!-- 配置文件外置的資源(存放到conf目錄,也是classpath路徑,下面會配置)--> <!--<resource> <directory>src/main/resources</directory> <includes> <include>config.properties</include> </includes> <targetPath>${project.build.directory}/conf</targetPath> </resource>--> </resources> <plugins> <!--scala編譯打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java編譯打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!-- ③打成一個zip包,發布項目的時候,將zip包copy到服務器上,直接unzip xxx.zip,里面包含要運行到的jar以及依賴的lib,還有配置的config文件,即可直接啟動服務 --> <!--The configuration of maven-jar-plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <!--The configuration of the plugin--> <configuration> <!-- 不打包資源文件(配置文件和依賴包分開) --> <excludes> <exclude>*.properties</exclude> <exclude>*.xml</exclude> <exclude>*.txt</exclude> </excludes> <!--Configuration of the archiver--> <archive> <!--生成的jar中,不要包含pom.xml和pom.properties這兩個文件--> <addMavenDescriptor>false</addMavenDescriptor> <!--Manifest specific configuration--> <manifest> <!--是否把第三方jar放到manifest的classpath中--> <addClasspath>true</addClasspath> <!--生成的manifest中classpath的前綴,因為要把第三方jar放到lib目錄下,所以classpath的前綴是lib/--> <classpathPrefix>lib/</classpathPrefix> <!--應用的main class--> <mainClass>com.swordfall.restserver.base.WebServer</mainClass> </manifest> <!-- 給清單文件添加鍵值對,增加classpath路徑,這里將conf目錄也設置為classpath路徑 --> <manifestEntries> <Class-Path>conf/</Class-Path> </manifestEntries> </archive> <!--過濾掉不希望包含在jar中的文件--> <!-- <excludes> <exclude>${project.basedir}/xml/*</exclude> </excludes>--> </configuration> </plugin> <!--The configuration of maven-assembly-plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <!--The configuration of the plugin--> <configuration> <!--Specifies the configuration file of the assembly plugin--> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
maven-assembly-plugin還需要配置一個assembly.xml文件,用於過濾不必要的第三方jar包文件,或者確定自己編譯的項目文件路徑、項目的啟動腳本文件目錄,打包的類型格式zip或者tar.gz等。
assembly.xml文件內容為:
<assembly> <id>bin</id> <includeBaseDirectory>false</includeBaseDirectory> <!-- 最終打包成一個用於發布的zip文件 --> <formats> <format>zip</format> </formats> <!-- Adds dependencies to zip package under lib directory --> <dependencySets> <dependencySet> <!-- 不使用項目的artifact,第三方jar不要解壓,打包進zip文件的lib目錄 --> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>lib</outputDirectory> <unpack>false</unpack> </dependencySet> </dependencySets> <fileSets> <!-- 把項目相關的說明文件,打包進zip文件的根目錄 --> <!--<fileSet> <directory>${project.basedir}</directory> <outputDirectory>/</outputDirectory> </fileSet>--> <!-- 把項目的配置文件,打包進zip文件的config目錄 --> <!--<fileSet>--> <!--<directory>${project.basedir}/src/main/resources</directory>--> <!--<outputDirectory>/conf</outputDirectory>--> <!--<includes>--> <!--<include>*.xml</include>--> <!--<include>*.properties</include>--> <!--</includes>--> <!--</fileSet>--> <!-- 把項目自己編譯出來的jar文件,打包進zip文件的根目錄 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory></outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> <!-- 把項目的腳本文件目錄(src/main/scripts)中的啟動腳本,打包進zip文件的根目錄 --> <fileSet> <directory>${project.basedir}/src/main/scripts</directory> <outputDirectory>bin</outputDirectory> <includes> <include>startup.*</include> </includes> </fileSet> </fileSets> </assembly>
打包結果為:
2.4 maven-shade-plugin 打包
用來打可執行包,包含依賴,以及對依賴進行取舍過濾。使用這種方式打出來的是揉合在一起,成為一個jar文件。這種打包方式的優點是可以利用transformer把第三方jar相同的配置文件組合成一個文件,如reference.conf文件等等。
pom.xml配置如下:
<!--④ maven-shade-plugin--> <build> <!--資源文件路徑配置--> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.conf</include> </includes> </resource> </resources> <plugins> <!--scala編譯打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java編譯打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!-- ④ maven-shade-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!--資源轉換 在打包時,存在將多個構件中的class文件或資源文件聚合的需求。shade插件提供了豐富的Transformer工具類。這里介紹一些常用的Transformer--> <transformers> <!--ManifestResourceTransformer可以輕松實現往MANIFEST文件中寫入Main-Class,這是可執行包的必要條件--> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.hongshan.bdp.restserver.base.WebServer</mainClass> </transformer> <!--AppendingTransformer用來處理多個jar包中存在重名的配置文件的合並,尤其是spring--> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>reference.conf</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>version.conf</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>akka-http-version.conf</resource> </transformer> <!--ServicesResourceTransformer JDK的服務發現機制是基於META-INF/services/目錄的,如果同一接口存在多個實現需要合並 ,則可以使用此Transformer--> <!--<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>--> </transformers> <shadedArtifactAttached>true</shadedArtifactAttached> <shadedClassifierName>jar-with-dependencies</shadedClassifierName> <!--過濾不需要的jar包--> <artifactSet> <excludes> <exclude>io.spray:spray-json_2.11:jar:</exclude> <exclude>org.slf4j:slf4j-api:jar:</exclude> <exclude>org.slf4j:slf4j-log4j12:jar:</exclude> <exclude>log4j:log4j:jar:</exclude> <exclude>commons-logging:commons-logging:jar:</exclude> <exclude>commons-lang:commons-lang:jar:</exclude> </excludes> </artifactSet> <!--配置<minimizeJar>將項目中沒有使用的依賴自動移除,使項目最小化--> <minimizeJar>true</minimizeJar> </configuration> </execution> </executions> </plugin> </plugins> </build>
打包結果:
2.5 其他插件
2.5.1 maven-surefire-plugin
該插件主要用於項目maven編譯打包時,跳過單元測試,pom.xml配置為:
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project>
總結
【github地址】
https://github.com/SwordfallYeung/MavenPackage
【參考資料】
https://blog.csdn.net/qq_16055765/article/details/79481258
https://www.cnblogs.com/Andrew520/p/8857603.html
https://blog.csdn.net/zzm3280/article/details/84953070
https://blog.csdn.net/hxpjava1/article/details/79711710
https://blog.csdn.net/u012834750/article/details/80937747 scala打包插件配置①
https://blog.csdn.net/tf461991046/article/details/80834685 scala打包插件配置②
https://blog.csdn.net/u013019338/article/details/83377070 spring-boot配置讀取外部配置文件
https://www.cnblogs.com/hdwang/p/6627912.html 普通jar包如何讀取外部的配置文件
https://www.cnblogs.com/wangfajun/p/9585530.html linux shell腳本啟動或停止jar
https://blog.csdn.net/qq_18300109/article/details/80798334 IDEA如何打包可運行jar,外部引用jar包版
https://blog.csdn.net/qingfengmuzhu1993/article/details/80284739 IDEA自身打包方式
https://my.oschina.net/u/2377110/blog/1585553 shade 過濾包名