48

プロジェクトBに、プロジェクトAによって作成されてリモートリポジトリにデプロイされたZIPをプルダウン(および解凍)させようとしています。

maven-assembly-pluginZIPは、パッケージタイプを使用して作成および添付され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しなかったためだと思いますが、プロジェクトAをパッケージタイプとしてzip指定できないため、次のようになります。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>
4

3 に答える 3

35

StackOverflowへようこそ:)。

あなたは正しい道を進んでいます。あなたの本当の問題はzipを使用することです。

次の構成は問題なく、私にとってはうまく機能します。これは古いもの(2年前)であり、ベストプラクティスと一致するかどうかはわかりません。しかし、私はそれが機能していることを知っています。

これにより、特に単体テストの場合、プロジェクト間でいくつかのリソースを共有できます。

Zipプロジェクト:

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. 「分類子」はリソース(アセンブリ名など)です

    リソースzipfalsesrc / main / resources

主なプロジェクト:

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>
    

私はこれが明確であり、それがあなたを助けることを願っています:)

于 2012-09-06T12:01:24.897 に答える
5

別のアプローチは、完全に圧縮することをあきらめ、標準のMavenライフサイクルを使用してファイルをリソースとしてjarファイルにパックし、クラスパスを介して他のプロジェクトからそれらにアクセスすることです。

特定のパッキング要件(含む、除外するなど)がない限り、追加の構成は必要ありません。プロジェクトのsrc/main/resourcesディレクトリにコンテンツを配置するだけです。

このアプローチには、EclipseなどのIDE内から呼び出されたときに変更されずに機能するという追加の利点があります。

于 2012-09-06T12:25:38.757 に答える
2

コマンドラインからこのようなことをしたい場合(たとえば、pom.xmlファイルを作成せずにスクリプトから)、これを行う方法は次のとおりです...

個々のプロパティを指定することができます。

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

または、それらすべてを1つの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=<directory>オプションで、必要に応じてパラメータを使用してターゲットディレクトリを指定することもできます。

于 2016-09-05T07:04:05.303 に答える