如何使用包含依赖项子集的maven生成可执行jar?

时间:2014-04-20 10:22:14

标签: java maven maven-assembly-plugin

我有一个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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以使用maven shade plugin创建一个包含代码和依赖项1和2的maven项目。

然后导入那个&#39; uber&#39; jar模块作为依赖关系转换为不同的WAR模块,并在那里添加其他依赖关系。

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