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 created and attached using the maven-assembly-plugin
, with packaging type 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>
Attempting to pull it down from Project B's pom with the 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>
fails with: [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]
I would assume this is because I specified Project A's packaging as pom
and not zip
, however I can't specify Project A as packaging type zip
because it results in:
[ERROR] Unknown packaging: zip @ line 13, column 13
Am I doing something wrong here or is this simply not possible? I just have a bunch of files that I want to bundle up into an artifact and allow multiple other projects to download and unpack them for use. Open to different suggestions...
I've also checked to make sure that the assembled zip is indeed in the nexus.
UPDATED WITH ANSWER
For anyone else's benefit, what I was missing is that the <classifier>
of the dependency has to match the <id>
of the assembly. Notice where thisistheattachedartifactsclassifier
is specified in the following files.
scripts.xml (Project A):
<assembly> <id>thisistheattachedartifactsclassifier</id> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>src/main/resources</directory> ... </fileSet> </fileSets> </assembly>
pom.xml (Project 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>
In your maven project create a folder assembly . Add zip. xml file in the assembly folder. Add below code in zip.
No need to download all those. Maven will take care of all the artifact's dependencies for the specified dependency mentioned in pom file.
The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.
However, as previously mentioned, the user may have a need for third-party plugins. Since the Maven project is assumed to have control over the default plugin groupId, this means configuring Maven to search other groupId locations for plugin-prefix mappings. As it turns out, this is simple.
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.
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
the "classifier" is resources (like assembly name)
resources zip false src/main/resources
pom.xml
Please, note that
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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With