日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maven生成可以直接运行的jar包的多种方式

發布時間:2025/6/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven生成可以直接运行的jar包的多种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Maven可以使用mvn package指令對項目進行打包,如果使用Java -jar xxx.jar執行運行jar文件,會出現"no main manifest attribute, in xxx.jar"(沒有設置Main-Class)、ClassNotFoundException(找不到依賴包)等錯誤。

要想jar包能直接通過java -jar xxx.jar運行,需要滿足:

1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,這樣才能確定程序的入口在哪里;

2、要能加載到依賴包。

使用Maven有以下幾種方法可以生成能直接運行的jar包,可以根據需要選擇一種合適的方法。

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包

在pom.xml中配置:

[html] view plaincopyprint?
  • <build>??
  • ????<plugins>??
  • ??
  • ????????<plugin>??
  • ????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????<artifactId>maven-jar-plugin</artifactId>??
  • ????????????<version>2.6</version>??
  • ????????????<configuration>??
  • ????????????????<archive>??
  • ????????????????????<manifest>??
  • ????????????????????????<addClasspath>true</addClasspath>??
  • ????????????????????????<classpathPrefix>lib/</classpathPrefix>??
  • ????????????????????????<mainClass>com.xxg.Main</mainClass>??
  • ????????????????????</manifest>??
  • ????????????????</archive>??
  • ????????????</configuration>??
  • ????????</plugin>??
  • ????????<plugin>??
  • ????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????<artifactId>maven-dependency-plugin</artifactId>??
  • ????????????<version>2.10</version>??
  • ????????????<executions>??
  • ????????????????<execution>??
  • ????????????????????<id>copy-dependencies</id>??
  • ????????????????????<phase>package</phase>??
  • ????????????????????<goals>??
  • ????????????????????????<goal>copy-dependencies</goal>??
  • ????????????????????</goals>??
  • ????????????????????<configuration>??
  • ????????????????????????<outputDirectory>${project.build.directory}/lib</outputDirectory>??
  • ????????????????????</configuration>??
  • ????????????????</execution>??
  • ????????????</executions>??
  • ????????</plugin>??
  • ??
  • ????</plugins>??
  • </build>??
  • <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>com.xxg.Main</mainClass></manifest></archive></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.10</version><executions><execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/lib</outputDirectory></configuration></execution></executions></plugin></plugins> </build>



    maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分內容,<mainClass>com.xxg.Main</mainClass>指定MANIFEST.MF中的Main-Class,<addClasspath>true</addClasspath>會在MANIFEST.MF加上Class-Path項并配置依賴包,<classpathPrefix>lib/</classpathPrefix>指定依賴包所在目錄。

    例如下面是一個通過maven-jar-plugin插件生成的MANIFEST.MF文件片段:

    [plain] view plaincopyprint?
  • Class-Path:?lib/commons-logging-1.2.jar?lib/commons-io-2.4.jar??
  • Main-Class:?com.xxg.Main??
  • Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
    Main-Class: com.xxg.Main

    只是生成MANIFEST.MF文件還不夠,maven-dependency-plugin插件用于將依賴包拷貝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置,即lib目錄下。

    配置完成后,通過mvn package指令打包,會在target目錄下生成jar包,并將依賴包拷貝到target/lib目錄下,目錄結構如下:

    指定了Main-Class,有了依賴包,那么就可以直接通過java -jar xxx.jar運行jar包。

    這種方式生成jar包有個缺點,就是生成的jar包太多不便于管理,下面兩種方式只生成一個jar文件,包含項目本身的代碼、資源以及所有的依賴包。

    方法二:使用maven-assembly-plugin插件打包

    在pom.xml中配置:

    [html] view plaincopyprint?
  • <build>??
  • ????<plugins>??
  • ??
  • ????????<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>??
  • ??
  • ????</plugins>??
  • </build>??
  • <build>
    <plugins> &lt;plugin&gt;&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;&lt;version&gt;2.5.5&lt;/version&gt;&lt;configuration&gt;&lt;archive&gt;&lt;manifest&gt;&lt;mainClass&gt;com.xxg.Main&lt;/mainClass&gt;&lt;/manifest&gt;&lt;/archive&gt;&lt;descriptorRefs&gt;&lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;&lt;/descriptorRefs&gt;&lt;/configuration&gt;&lt;/plugin&gt;&lt;/plugins&gt;

    </build>

    打包方式:

    [plain] view plaincopyprint?
  • mvn?package?assembly:single??
  • mvn package assembly:single

    打包后會在target目錄下生成一個xxx-jar-with-dependencies.jar文件,這個文件不但包含了自己項目中的代碼和資源,還包含了所有依賴包的內容。所以可以直接通過java -jar來運行。

    此外還可以直接通過mvn package來打包,無需assembly:single,不過需要加上一些配置:

    [html] view plaincopyprint?
  • <build>??
  • ????<plugins>??
  • ??
  • ????????<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>??
  • ????????????<executions>??
  • ????????????????<execution>??
  • ????????????????????<id>make-assembly</id>??
  • ????????????????????<phase>package</phase>??
  • ????????????????????<goals>??
  • ????????????????????????<goal>single</goal>??
  • ????????????????????</goals>??
  • ????????????????</execution>??
  • ????????????</executions>??
  • ????????</plugin>??
  • ??
  • ????</plugins>??
  • </build>??
  • <build>
    <plugins> &lt;plugin&gt;&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;&lt;version&gt;2.5.5&lt;/version&gt;&lt;configuration&gt;&lt;archive&gt;&lt;manifest&gt;&lt;mainClass&gt;com.xxg.Main&lt;/mainClass&gt;&lt;/manifest&gt;&lt;/archive&gt;&lt;descriptorRefs&gt;&lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;&lt;/descriptorRefs&gt;&lt;/configuration&gt;&lt;executions&gt;&lt;execution&gt;&lt;id&gt;make-assembly&lt;/id&gt;&lt;phase&gt;package&lt;/phase&gt;&lt;goals&gt;&lt;goal&gt;single&lt;/goal&gt;&lt;/goals&gt;&lt;/execution&gt;&lt;/executions&gt;&lt;/plugin&gt;&lt;/plugins&gt;

    </build>


    其中<phase>package</phase>、<goal>single</goal>即表示在執行package打包時,執行assembly:single,所以可以直接使用mvn package打包。

    不過,如果項目中用到spring Framework,用這種方式打出來的包運行時會出錯,使用下面的方法三可以處理。

    方法三:使用maven-shade-plugin插件打包

    在pom.xml中配置:

    [html] view plaincopyprint?
  • <build>??
  • ????<plugins>??
  • ??
  • ????????<plugin>??
  • ????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????<artifactId>maven-shade-plugin</artifactId>??
  • ????????????<version>2.4.1</version>??
  • ????????????<executions>??
  • ????????????????<execution>??
  • ????????????????????<phase>package</phase>??
  • ????????????????????<goals>??
  • ????????????????????????<goal>shade</goal>??
  • ????????????????????</goals>??
  • ????????????????????<configuration>??
  • ????????????????????????<transformers>??
  • ????????????????????????????<transformer?implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">??
  • ????????????????????????????????<mainClass>com.xxg.Main</mainClass>??
  • ????????????????????????????</transformer>??
  • ????????????????????????</transformers>??
  • ????????????????????</configuration>??
  • ????????????????</execution>??
  • ????????????</executions>??
  • ????????</plugin>??
  • ??
  • ????</plugins>??
  • </build>??
  • <build>
    <plugins> &lt;plugin&gt;&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;&lt;version&gt;2.4.1&lt;/version&gt;&lt;executions&gt;&lt;execution&gt;&lt;phase&gt;package&lt;/phase&gt;&lt;goals&gt;&lt;goal&gt;shade&lt;/goal&gt;&lt;/goals&gt;&lt;configuration&gt;&lt;transformers&gt;&lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;&lt;mainClass&gt;com.xxg.Main&lt;/mainClass&gt;&lt;/transformer&gt;&lt;/transformers&gt;&lt;/configuration&gt;&lt;/execution&gt;&lt;/executions&gt;&lt;/plugin&gt;&lt;/plugins&gt;

    </build>


    配置完成后,執行mvn package即可打包。在target目錄下會生成兩個jar包,注意不是original-xxx.jar文件,而是另外一個。和maven-assembly-plugin一樣,生成的jar文件包含了所有依賴,所以可以直接運行。

    如果項目中用到了Spring Framework,將依賴打到一個jar包中,運行時會出現讀取XML schema文件出錯。原因是Spring Framework的多個jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一個jar包會互相覆蓋。為了避免互相影響,可以使用AppendingTransformer來對文件內容追加合并:

    [html] view plaincopyprint?
  • <build>??
  • ????<plugins>??
  • ??
  • ????????<plugin>??
  • ????????????<groupId>org.apache.maven.plugins</groupId>??
  • ????????????<artifactId>maven-shade-plugin</artifactId>??
  • ????????????<version>2.4.1</version>??
  • ????????????<executions>??
  • ????????????????<execution>??
  • ????????????????????<phase>package</phase>??
  • ????????????????????<goals>??
  • ????????????????????????<goal>shade</goal>??
  • ????????????????????</goals>??
  • ????????????????????<configuration>??
  • ????????????????????????<transformers>??
  • ????????????????????????????<transformer?implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">??
  • ????????????????????????????????<mainClass>com.xxg.Main</mainClass>??
  • ????????????????????????????</transformer>??
  • ????????????????????????????<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>??
  • ????????????????????????</transformers>??
  • ????????????????????</configuration>??
  • ????????????????</execution>??
  • ????????????</executions>??
  • ????????</plugin>??
  • ??
  • ????</plugins>??
  • </build>??
  • <build>
    <plugins> &lt;plugin&gt;&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;&lt;version&gt;2.4.1&lt;/version&gt;&lt;executions&gt;&lt;execution&gt;&lt;phase&gt;package&lt;/phase&gt;&lt;goals&gt;&lt;goal&gt;shade&lt;/goal&gt;&lt;/goals&gt;&lt;configuration&gt;&lt;transformers&gt;&lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;&lt;mainClass&gt;com.xxg.Main&lt;/mainClass&gt;&lt;/transformer&gt;&lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;&lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt;&lt;/transformer&gt;&lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;&lt;resource&gt;META-INF/spring.schemas&lt;/resource&gt;&lt;/transformer&gt;&lt;/transformers&gt;&lt;/configuration&gt;&lt;/execution&gt;&lt;/executions&gt;&lt;/plugin&gt;&lt;/plugins&gt;

    </build>



    轉載于:https://www.cnblogs.com/chentging/p/7492082.html

    總結

    以上是生活随笔為你收集整理的Maven生成可以直接运行的jar包的多种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。