Maven - Depend on assembled zip

后端 未结 3 958
星月不相逢
星月不相逢 2020-11-30 02:18

I\'m trying to have a Project B pull down (and unpack) a ZIP built by Project A and deployed to a remote repository.

The ZIP is cre

相关标签:
3条回答
  • 2020-11-30 02:48

    An alternate approach could be to give up on zipping entirely, use the standard Maven lifecycle to pack your files as resources in a jar file and access them from your other projects via classpath.

    Unless you have specific packing requirements (including, excluding, etc.) this would require no additional configuration: just put your stuff in your project's src/main/resources directory.

    This approach has the added benefit of working unchanged when invoked from within an IDE such as Eclipse.

    0 讨论(0)
  • 2020-11-30 02:55

    Welcome to Stack Overflow :).

    You are on the right way. Your real problem is using a zip.

    The following configuration is ok and work great for me. It's an old one (2 years ago), and I'm not sure that match the best practices. But I Know that's working.

    This allow me to share some resources between projects, especially for unit tests.

    Zip Project :

    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 : It will produce this artifact : cfg_dev-1.1.0-resources.zip Please, note that

    1. this is a zip archive
    2. the "classifier" is resources (like assembly name)

      resources zip false src/main/resources

    Main Project :

    pom.xml

    Please, note that

    1. this depends on a zip archive
    2. the dependency "classifier" is resources (like previous assembly name)

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

    I hope this is clear and that will help you :)

    0 讨论(0)
  • 2020-11-30 03:07

    If you ever want to do something like this from the command line (e.g. from a script without having to write a pom.xml file), here's the way to do it...

    You can either specify the individual properties:

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

    Or specify them all in one artifact parameter:

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

    With the latter, it is important to keep the classifier at the end, after the packaging/type attribute. Like this: -Dartifact=groupId:artifactId:version:type:classifier

    You can also optionally specify the target directory using the -DoutputDirectory=<directory> parameter if required.

    0 讨论(0)
提交回复
热议问题