在单独的程序集中包含jar-with-dependencies?

时间:2012-11-03 03:18:32

标签: java maven maven-assembly-plugin

我使用maven程序集插件打包可分发的ZIP存档。但是,我想在最终存档中包含一个单独的程序集jar-with-dependencies的结果。我怎样才能做到这一点?我意识到我可能只是手动包含JAR,但是如何确保我的自定义程序集在JAR程序集之后运行?

1 个答案:

答案 0 :(得分:2)

您可以使用多模块项目:

parent
  |- ...
  |- jar-with-dependencies-module
  |- final-zip-module

jar-with-dependencies模块中,您将使用所有依赖项组装“uber-jar”。 POM中的构建配置应如下所示:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.3</version>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

final-zip-module中,您可以将jar-with-dependencies添加为依赖项,并将其用于ZIP文件的程序集描述符中。 POM看起来像这样:

<project>
  ...

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>jar-with-dependencies-module</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <classifier>jar-with-dependencies</classifier>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

汇编描述符看起来像这样:

<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">

  <id>final-assembly</id>
  <formats>
    <format>zip</format>
  </formats>

  <dependencySets>
    <!-- Include the jar-with-dependencies -->
    <dependencySet>
      <includes>
        <include>com.example:jar-with-dependencies-module:*:jar-with-dependencies</include>
      </includes>
      <useProjectArtifact>false</useProjectArtifact>
      <!-- Don't use transitive dependencies since they are already included in the jar -->
      <useTransitiveDependencies>false</useTransitiveDependencies>
    </dependencySet>t>
  </dependencySets>
</assembly>

由于依赖于jar-with-dependency-module,maven将在您的超级jar构建之后始终构建final-zip-module