Maven - 依赖于组装的 zip

2024-02-22

我正在尝试有一个项目B拉下(并解压)由以下人员构建的 ZIP项目A并部署到远程存储库。

使用以下命令创建并附加 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>

试图将其拉下来项目B的 pom 与maven-dependency-plugin:

<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) -> [Help 1]

我认为这是因为我指定了项目A的包装为pom并不是zip,但是我无法指定项目A作为包装类型zip因为它会导致:

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

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

我还检查过以确保组装好的 zip 确实位于连接中。

更新了答案

为了其他人的利益,我所缺少的是<classifier>依赖项必须匹配<id>大会的。注意哪里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>

欢迎来到堆栈溢出:)。

你走在正确的道路上。你真正的问题是使用拉链。

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

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

邮编项目:

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. “分类器”是资源(如程序集名称)

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

主要项目:

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>
    

我希望这很清楚并且对您有所帮助:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Maven - 依赖于组装的 zip 的相关文章

随机推荐