1

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

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

我试图实现的是:

发送一个包含一些可运行代码的 JAR。此外,还有一个 ZIP 存档,其中包含 JAR 中随附的依赖项的源代码。

4

1 回答 1

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 和fileSet。

另一个潜在的问题是:如果你这样做是为了战争或耳朵,你将需要在项目的 POM 上添加一个依赖项以获取完整的依赖项集。(见MNG-1991。)

<dependency>
  <groupId>${my.webapp.groupId}</groupId>
  <artifactId>${my.webapp.artifactId}</artifactId>
  <version>${my.webapp.version}</version>
  <type>pom</type>
</dependency>
于 2013-08-12T16:36:01.577 回答