【问题标题】:Maven - Depend on assembled zipMaven - 依赖于组装的 zip
【发布时间】:2012-08-31 03:51:56
【问题描述】:

我正在尝试让 Project B 拉下(并解压)由 Project A 构建的 ZIP 并部署到远程存储库。

ZIP 是使用maven-assembly-plugin 创建和附加的,包装类型为pom

<artifactId>project-a</artifactId>
<name>ZIP</name>
<description>Used by Project B</description>
<packaging>pom</packaging>

...

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>distribution-package</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/scripts.xml</descriptor>
        </descriptors>
        <tarLongFileMode>gnu</tarLongFileMode>
      </configuration>
    </execution>
  </executions>
</plugin>

尝试使用 maven-dependency-pluginProject B 的 pom 中删除它:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-scripts</id>
      <phase>package</phase>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <outputDirectory>${basedir}/target/staging</outputDirectory>
        <stripVersion>true</stripVersion>
        <artifactItems>
          <artifactItem>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <version>...</version>
            <overWrite>true</overWrite>
            <type>zip</type>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

失败:[ERROR] Failed to execute goal on project ...: Could not resolve dependencies for project group:artifact:pom:version: Could not find artifact group:project-a:zip:version in nexus (http://...:8081/nexus/content/groups/public) -&gt; [Help 1]

我认为这是因为我将 Project A 的包装指定为 pom 而不是 zip,但是我无法将 Project A 指定为包装输入zip,因为它会导致:

[ERROR]     Unknown packaging: zip @ line 13, column 13

我在这里做错了什么还是这根本不可能? 我只有一堆文件,我想将它们捆绑到一个工件中,并允许多个其他项目下载并解压缩它们以供使用。接受不同的建议...

我还检查以确保组装的 zip 确实在关系中。

已更新答案

为了其他人的利益,我缺少的是依赖项的&lt;classifier&gt; 必须与程序集的&lt;id&gt; 匹配。注意thisistheattachedartifactsclassifier 在以下文件中的指定位置。

scripts.xml(项目 A):

<assembly>
  <id>thisistheattachedartifactsclassifier</id>
  <formats>
    <format>zip</format>
  </formats>

  <fileSets>
    <fileSet>
      <directory>src/main/resources</directory>
      ...
    </fileSet>
  </fileSets>
</assembly>

pom.xml(项目 B):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-scripts</id>
      <phase>package</phase>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <outputDirectory>${basedir}/target/staging</outputDirectory>
        <stripVersion>true</stripVersion>
        <artifactItems>
          <artifactItem>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <version>...</version>
            <classifier>thisistheattachedartifactsclassifier</classifier>
            <overWrite>true</overWrite>
            <type>zip</type>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

【问题讨论】:

标签: maven zip pom.xml maven-assembly-plugin maven-dependency-plugin


【解决方案1】:

欢迎来到 Stack Overflow :)。

你是在正确的方式。您真正的问题是使用 zip。

以下配置没问题,对我来说很好用。这是一个旧的(2 年前),我不确定它是否符合最佳实践。但我知道这是行得通的。

这让我可以在项目之间共享一些资源,尤其是单元测试。

压缩项目:

pom.xml

<groupId>com.mycompany</groupId>
<artifactId>cfg_dev</artifactId>
<version>1.1.0</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>cfg-main-resources</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>/src/main/assembly/resources.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

程序集描述符: 它将产生这个工件:cfg_dev-1.1.0-resources.zip 请注意

  1. 这是一个 zip 存档
  2. “分类器”是资源(如程序集名称)

    资源 压缩 错误的 源/主/资源

主要项目:

pom.xml

请注意

  1. 这取决于 zip 存档
  2. 依赖“分类器”是资源(如以前的程序集名称)

    <!-- Unit test dependency -->
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>cfg_dev</artifactId>
        <version>${project.version}</version>
        <classifier>resources</classifier>
        <type>zip</type>
        <scope>test</scope>
    </dependency>
    
      ....
    
    <build>
      <testResources>
        <!-- Ressources habituelles  -->
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
        <!-- Unzipped resources from cfg_dev  -->
        <testResource>
            <directory>${project.build.directory}/test-resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    
    <plugins>
    
        <!-- Unzip shared resources -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-cfg-test-resources</id>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <phase>generate-test-resources</phase>
                    <configuration>
                        <outputDirectory>${project.build.directory}/test-resources</outputDirectory>
                        <includeArtifactIds>cfg_dev</includeArtifactIds>
                        <includeGroupIds>${project.groupId}</includeGroupIds>
                        <excludeTransitive>true</excludeTransitive>
                        <excludeTypes>pom</excludeTypes>
                        <scope>test</scope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      </plugins>
    

我希望这很清楚,对你有帮助:)

【讨论】:

  • 谢谢。有点模糊,但你让我朝着正确的方向前进。为了其他人的利益,描述符 XML 中程序集的 id 用于识别特定工件,因为它是 classifier
  • 装配name ?你的意思是id
  • 请注意,使用目标unpack 并指定&lt;artifact&gt;&lt;artifactItems&gt; 要容易得多。 (与您使用 unpack-dependencies 并排除传递性并包括您需要的特定依赖项相反。)
【解决方案2】:

另一种方法可能是完全放弃压缩,使用标准 Maven 生命周期将文件作为资源打包到 jar 文件中,并通过类路径从其他项目访问它们。

除非您有特定的打包要求(包括、排除等),否则不需要额外配置:只需将您的东西放在项目的 src/main/resources 目录中即可。

这种方法还有一个额外的好处,即当从 Eclipse 等 IDE 中调用时,无需更改即可工作。

【讨论】:

    【解决方案3】:

    如果您想从命令行执行此类操作(例如,无需编写 pom.xml 文件即可从脚本执行此操作),请按照以下方法操作...

    您可以指定单个属性:

    mvn dependency:copy -DgroupId=org.apache.maven -DartifactId=maven-core -Dversion=2.2.1 -Dpackaging=zip -Dclassifier=thisistheattachedartifactsclassifier
    

    或者在一个artifact参数中指定它们:

    mvn dependency:copy -Dartifact=org.apache.maven:maven-core:2.2.1:zip:thisistheattachedartifactsclassifier
    

    对于后者,将classifier 保留在packaging/type 属性之后很重要。像这样:-Dartifact=groupId:artifactId:version:type:classifier

    如果需要,您还可以选择使用-DoutputDirectory=&lt;directory&gt; 参数指定目标目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2014-04-12
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多