[英]How to produce an executable jar file with all maven dependencies?
[英]How to produce a executable jar with maven that contains a subset of dependencies?
我有一個Maven SWT項目,它具有多個依賴項:依賴項1,依賴項2,依賴項3,....,依賴項n。
我想制作一個僅包含項目,依賴項1和依賴項2的可執行jar,但其他依賴項將復制到/ lib目錄中。
這是我的程序集配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<configuration>
<descriptors>
<descriptor>mvnDescript.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${project.build.mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
和mvnDescript.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
<includes>
<include>${artifact}</include>
</includes>
</dependencySet>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
<excludes>
<exclude>${artifact}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
要創建這樣的可執行jar,您應該理解,您已將依賴項定義為maven-assembly-plugin的類路徑的元素,這不是您想要執行的操作。 您的項目應改為定義這些依賴項。 當然,您應該使用該插件的最新版本。
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${project.build.mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>jar-with-deps</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
您可以使用maven shade插件創建一個包含代碼以及依賴項1和2的maven項目。
然后將“超級” jar模塊作為依賴項導入到另一個WAR模塊作為依賴項,然后在其中添加其他依賴項。
最終結果是,項目代碼以及依賴項1和2將與其他jar依賴項3.jar,dependency4.jar等一起位於WEB-INF / lib / jar-with-dep1-and-2.jar中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.