Maven - 生成每个依赖源的存档

时间:2013-08-12 07:08:28

标签: java maven dependencies

有没有办法让包含每个依赖关系源的存档作为单独的工件?

我所知道的是有一种方法可以使用依赖插件来下载每个源jar,如here所述。但是,这些文件将下载到本地存储库。

我尝试实现的目标是:

发送包含一些可运行代码的JAR。另外还有一个ZIP存档,它包含了JAR中发布的依赖项的源代码。

1 个答案:

答案 0 :(得分:1)

我需要做类似的事情,除了我还需要将所包含的资源过滤到我公司团队制作的资源。您可以使用maven-dependency-pluginmaven-assembly-plugin的组合来实现此目标。

这是我使用的配置。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <configuration>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
      <executions>
        <execution>
          <id>retrieve-dependency-sources</id>
          <phase>process-sources</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <classifier>sources</classifier>
            <outputDirectory>${project.build.directory}/dep-sources</outputDirectory>
            <type>jar</type>
            <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
            <prependGroupId>true</prependGroupId>
            <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
            <excludeTransitive>false</excludeTransitive>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <id>package-dependency-sources</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <appendAssemblyId>true</appendAssemblyId>
            <attach>true</attach>
            <finalName>${your.app.finalName}</finalName>
            <descriptors>
              <descriptor>src/main/assembly/dep-source-assembly.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

以下是汇编描述符dep-source-assembly.xml,它应放在src/main/assembly中。

<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>dep-sources</id>  <!-- whatever you'd like the classifier to be -->

  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>

  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/dep-sources</directory>
      <outputDirectory></outputDirectory>
      <!-- Define the includes if you'd like to have sources only for certain 
           packages.  For my use case, I needed to include just source files
           produced elsewhere in my company, not commonly available jars like
           Spring.  -->
      <includes>
        <include>**/com.mycompany.*.jar</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

听起来您的用例可能与我的有点不同,因此您可以使用程序集插件dependencySet代替单独调用maven-dependency-plugin和文件集。

另一个潜在的问题:如果你是为战争或耳朵做这件事,你需要在项目的POM上添加一个依赖项来获得完整的依赖关系。 (见MNG-1991。)

<dependency>
  <groupId>${my.webapp.groupId}</groupId>
  <artifactId>${my.webapp.artifactId}</artifactId>
  <version>${my.webapp.version}</version>
  <type>pom</type>
</dependency>