[英]How to produce a executable jar with maven that contains a subset of dependencies?
I have a maven SWT project that has multiple dependencies: dependency 1, dependency 2, dependency 3,...., dependency n. 我有一个Maven SWT项目,它具有多个依赖项:依赖项1,依赖项2,依赖项3,....,依赖项n。
I want to make an executable jar that contains only the project and dependency 1 and dependency 2 but the other dependencies will be copied on the /lib directory. 我想制作一个仅包含项目,依赖项1和依赖项2的可执行jar,但其他依赖项将复制到/ lib目录中。
Here is my assembly config : 这是我的程序集配置:
<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>
and the mvnDescript.xml : 和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>
To create such an executable jar you should understand that you defined the dependencies as elements for the classpath of the maven-assembly-plugin which is not what you want to do. 要创建这样的可执行jar,您应该理解,您已将依赖项定义为maven-assembly-plugin的类路径的元素,这不是您想要执行的操作。 Your project should define those dependencies instead. 您的项目应改为定义这些依赖项。 And of course you should use an up-to-date version of the 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>
You can use the maven shade plugin to create a maven project that contains the code, and dependencies 1 and 2. 您可以使用maven shade插件创建一个包含代码以及依赖项1和2的maven项目。
Then import that 'uber' jar module as a dependency into a different WAR module as dependency, and add the other dependencies there. 然后将“超级” jar模块作为依赖项导入到另一个WAR模块作为依赖项,然后在其中添加其他依赖项。
The end result will the that the project code and dependencies 1 and 2 will be in WEB-INF/lib/jar-with-dep1-and-2.jar, together with the other jars dependency3.jar, dependency4.jar, etc. 最终结果是,项目代码以及依赖项1和2将与其他jar依赖项3.jar,dependency4.jar等一起位于WEB-INF / lib / jar-with-dep1-and-2.jar中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.