首页 文章

Maven - 取决于组装的拉链

提问于
浏览
41

我正在努力将_29450_下拉(并解压缩)由 Project A 构建的ZIP并部署到远程存储库 .

使用 maven-assembly-plugin 创建并附加ZIP,包装类型为 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) -> [Help 1]

我认为这是因为我将 Project A 的包装指定为 pom 而不是 zip ,但我不能将 Project A 指定为包装类型 zip ,因为它会导致:

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

我在这里做错了还是这根本不可能?我只有一堆文件,我想捆绑到一个工件,并允许多个其他项目下载和解压缩使用 . 对不同的建议开放......

我还检查确保组装的拉链确实在连接中 .

UPDATED WITH ANSWER

对于其他任何人的好处,我缺少的是依赖项的 <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>

3 回答

  • 0

    欢迎来到Stack Overflow :) .

    你是正确的方式 . 你真正的问题是使用拉链 .

    以下配置没问题,对我来说很有用 . 这是一个旧的(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>
    

    Assembly descriptor : 它会产生这个神器:cfg_dev-1.1.0-resources.zip请注意

    • 这是一个zip存档

    • “分类器”是资源(如程序集名称)

    资源zip zip src / main / resources

    主要项目:

    pom.xml

    请注意

    • 这取决于zip存档

    • 依赖“分类器”是资源(如以前的程序集名称)

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

    我希望这很清楚,这将有助于你:)

  • 31

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

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

    从Eclipse(例如Eclipse)中调用时,此方法具有不变的工作方式 .

  • 4

    如果你想从命令行做这样的事情(例如从脚本而不必写一个 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
    

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

    如果需要,还可以使用 -DoutputDirectory=<directory> 参数指定目标目录 .

相关问题