一、IDEA自帶打包插件
內(nèi)容:此種方式可以自己選擇制作胖包或者瘦包,但推薦此種方式制作瘦包。
輸出:輸出目錄在out目錄下
流程步驟:
- 第一步: 依次選擇 file->projecct structure->artifacts->點(diǎn)擊+ (選擇jar)->選擇 from module with dependencies
- 第二步:彈出窗口中指定Main Class,是否選擇依賴jar包,是否包含測(cè)試。(盡量不選依賴包,防止依賴包選擇不全)
- 第三步:點(diǎn)擊Build–>Build Artifacts–>選擇bulid
二、maven插件打包
輸出:輸出目錄在target目錄下
2.1 制作瘦包(直接打包,不打包依賴包)
內(nèi)容:僅打包出項(xiàng)目中的代碼到JAR包中。
方式:在pom.xml中添加如下plugin; 隨后執(zhí)行maven install
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>指定版本</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
2.2 制作瘦包和依賴包(相互分離)
內(nèi)容:將依賴JAR包輸出到lib目錄方式(打包方式對(duì)于JAVA項(xiàng)目是通用的)
將項(xiàng)目中的JAR包的依賴包輸出到指定的目錄下,修改outputDirectory配置,如下面的${project.build.directory}/lib。
方式:
- pom.xml的build>plugins中添加如下配置。
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package打包
注意:如果想將打包好的JAR包通過(guò)命令直接運(yùn)行,如java -jar xx.jar。需要制定manifest配置的classpathPrefix與上面配置的相對(duì)應(yīng)。如上面把依賴JAR包輸出到了lib,則這里的classpathPrefix也應(yīng)指定為lib/;同時(shí),并指定出程序的入口類,在配置mainClass節(jié)點(diǎn)中配好入口類的全類名。
<plugins>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.yourpakagename.mainClassName</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
注意:默認(rèn)的classpath會(huì)在jar包內(nèi)。為了方便,可以在Main方法配置后加上manifestEntries配置,指定classpath。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 主函數(shù)的入口 -->
<mainClass>com.yourpakagename.mainClassName</mainClass>
<!-- 打包時(shí) MANIFEST.MF文件不記錄的時(shí)間戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
2.3 制作胖包(項(xiàng)目依賴包和項(xiàng)目打?yàn)橐粋€(gè)包)
內(nèi)容:將項(xiàng)目中的依賴包和項(xiàng)目代碼都打?yàn)橐粋€(gè)JAR包
方式:
- pom.xml的build>plugins中添加如下配置;
- 點(diǎn)擊maven project(右邊欄)->選擇Plugins->選擇assembly->點(diǎn)擊assembly:assembly
注意:1. 針對(duì)傳統(tǒng)的JAVA項(xiàng)目打包;
2. 打包指令為插件的assembly命令,盡量不用package指令。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
2.4 制作胖包(transform部分自定義)
<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>
<filters>
<filter>
<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.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxx.xxxInvoke</mainClass>
</transformer>
</transformers>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</execution>
</executions>
</plugin>
三、SpringBoot項(xiàng)目打包
內(nèi)容:將當(dāng)前項(xiàng)目里所有依賴包和當(dāng)前項(xiàng)目的源碼都打成一個(gè)JAR包,同時(shí)還會(huì)將沒有依賴包的JAR包也打出來(lái),以.original保存
方式:
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
四、Scala項(xiàng)目打包
方式:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-720568.html
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
五、groovy項(xiàng)目打包
方式:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-720568.html
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addStubSources</goal>
<goal>compile</goal>
<goal>execute</goal>
</goals>
</execution>
</executions>
</plugin>
一、IDEA自帶打包插件
內(nèi)容:此種方式可以自己選擇制作胖包或者瘦包,但推薦此種方式制作瘦包。
輸出:輸出目錄在out目錄下
流程步驟:
- 第一步: 依次選擇 file->projecct structure->artifacts->點(diǎn)擊+ (選擇jar)->選擇 from module with dependencies
- 第二步:彈出窗口中指定Main Class,是否選擇依賴jar包,是否包含測(cè)試。(盡量不選依賴包,防止依賴包選擇不全)
- 第三步:點(diǎn)擊Build–>Build Artifacts–>選擇bulid
二、maven插件打包
輸出:輸出目錄在target目錄下
2.1 制作瘦包(直接打包,不打包依賴包)
內(nèi)容:僅打包出項(xiàng)目中的代碼到JAR包中。
方式:在pom.xml中添加如下plugin; 隨后執(zhí)行maven install
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>指定版本</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
2.2 制作瘦包和依賴包(相互分離)
內(nèi)容:將依賴JAR包輸出到lib目錄方式(打包方式對(duì)于JAVA項(xiàng)目是通用的)
將項(xiàng)目中的JAR包的依賴包輸出到指定的目錄下,修改outputDirectory配置,如下面的${project.build.directory}/lib。
方式:
- pom.xml的build>plugins中添加如下配置。
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package打包
注意:如果想將打包好的JAR包通過(guò)命令直接運(yùn)行,如java -jar xx.jar。需要制定manifest配置的classpathPrefix與上面配置的相對(duì)應(yīng)。如上面把依賴JAR包輸出到了lib,則這里的classpathPrefix也應(yīng)指定為lib/;同時(shí),并指定出程序的入口類,在配置mainClass節(jié)點(diǎn)中配好入口類的全類名。
<plugins>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.yourpakagename.mainClassName</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
注意:默認(rèn)的classpath會(huì)在jar包內(nèi)。為了方便,可以在Main方法配置后加上manifestEntries配置,指定classpath。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 主函數(shù)的入口 -->
<mainClass>com.yourpakagename.mainClassName</mainClass>
<!-- 打包時(shí) MANIFEST.MF文件不記錄的時(shí)間戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
2.3 制作胖包(項(xiàng)目依賴包和項(xiàng)目打?yàn)橐粋€(gè)包)
內(nèi)容:將項(xiàng)目中的依賴包和項(xiàng)目代碼都打?yàn)橐粋€(gè)JAR包
方式:
- pom.xml的build>plugins中添加如下配置;
- 點(diǎn)擊maven project(右邊欄)->選擇Plugins->選擇assembly->點(diǎn)擊assembly:assembly
注意:1. 針對(duì)傳統(tǒng)的JAVA項(xiàng)目打包;
2. 打包指令為插件的assembly命令,盡量不用package指令。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
2.4 制作胖包(transform部分自定義)
<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>
<filters>
<filter>
<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.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.xxx.xxxInvoke</mainClass>
</transformer>
</transformers>
<minimizeJar>true</minimizeJar>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</execution>
</executions>
</plugin>
三、SpringBoot項(xiàng)目打包
內(nèi)容:將當(dāng)前項(xiàng)目里所有依賴包和當(dāng)前項(xiàng)目的源碼都打成一個(gè)JAR包,同時(shí)還會(huì)將沒有依賴包的JAR包也打出來(lái),以.original保存
方式:
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
四、Scala項(xiàng)目打包
方式:
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
五、groovy項(xiàng)目打包
方式:
- 在pom.xml的build>plugins中加入如下配置
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addStubSources</goal>
<goal>compile</goal>
<goal>execute</goal>
</goals>
</execution>
</executions>
</plugin>
到了這里,關(guān)于Maven工程打jar包的N種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!