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

标签 java maven dependencies

有没有办法获取包含每个依赖源作为单独 Artifact 的存档?

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

我试图实现的是:

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

最佳答案

我需要做类似的事情,除了我还需要将包含的源过滤为仅由我公司团队制作的源。您可以使用 maven-dependency-plugin 的组合和 maven-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/assemble 中。

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

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

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

关于java - Maven - 生成每个依赖源的存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18181288/

相关文章:

android - 为 OpenCv 添加模块依赖性的问题

java - 在慢速和快速连接上写入套接字的速度差异?

java - 具有自定义行布局的 radio 警报对话框

java - 依次读取三个URL

maven - Maven JAXB 2.x 插件的预期或推荐用法

java - 如何在 Eclipse 中运行带有 Bower 的 Maven Web 应用程序?

java - 定义切入点以捕获接口(interface)但不捕获父接口(interface)或子接口(interface)

java - 运行 Maven 项目时出错 : Unsupported protocol: t3

ssh - 在 dokku 部署期间将私有(private)仓库作为依赖项拉取?

java - 如何安排 spring 上下文配置文件以匹配项目依赖项?