2014-04-20 75 views
2

我有一个maven SWT项目,有多个依赖项:依赖项1,依赖项2,依赖项3,....,依赖项n。如何使用包含依赖关系子集的maven生成可执行jar?

我想创建一个只包含项目和依赖项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> 
+0

如果标记除1所有的依赖和2 “提供”,并使用 “ JAR-与依赖性” 它不工作? –

回答

0

可以使用maven shade plugin创建一个包含代码Maven项目,以及依赖关系1和2.

然后导入'超级'jar mod作为与依赖关系放入不同WAR模块的依赖关系,并在其中添加其他依赖关系。

最终结果是项目代码和依赖关系1和2将在WEB-INF/lib/jar-with-dep1-and-2.jar中与其他jar dependency3.jar,dependency4一起。 jar等

1

要创建这样一个可执行的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> 
相关问题