copy dependency assembly resources to specific folder - maven

I have ProjectA which uses the Maven assembly plugin to pack some resources from a module into the repository.
I have then ProjectB which has a dependency on ProjectA. In ProjectB, I'd like to use the maven-dependency-plugin to unpack the module resources(packed by the assembly plugin) into some target folder of my choice.
I configured the dependency plugin as following, but when I run maven, it will only copy the module's resources and not also the assembly resources.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

You would need to specify the correct classifier and type to maven so that it can do this. Your assembly should available in your local repository or downloadable from repository.
For instance, assuming your assembly was named moduleA-distribution-1.0.zip, you would alter the above snippet as follows:
<artifactItem>
<groupId>path.to.projectA.groupId</groupId>
<artifactId>moduleA</artifactId>
<version>1.0</version>
<classifier>distribution</classifier>
<type>zip</type>
<outputDirectory>some/path/here</outputDirectory>
</artifactItem>

Related

How to use the artifact located in local .m2 repository instead of looking in nexus?

I have an artifact in the form of an WAR file which i want to store in my local .m2 repository. This WAR file is NOT yet present elsewhere i.e in any other URL repository like nexus etc.
C:\Users\user1.m2\repository\com\mycompany\prodcode\myapp\2.3.1423\myapp-2.3.1423-test.war
Now when i run my POM using mvn then i want the WAR artifact i copied above to be used AND DO NOT maven to go and search the repositories (like nexus etc).
How can i do the same?
I have made following POM file changes but they don't seem to work. Is there anything i need to do so that artifact i copied is the one used by the Maven build system?
<build>
<directory>${project.basedir}/target</directory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<id>download-dependencies</id>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mycompany.prodcode</groupId>
<artifactId>myapp</artifactId>
<version>2.3.1423</version>
<classifier>test</classifier>
<type>war</type>
<includes>**/*.*</includes>
<outputDirectory>${master.dir}/myapp</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven include zip dependency

We have an zip artifact hosted on our local repository. Within my Maven project I would like to retrieve this zip dependency.
From command line I can do that via:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
-DrepoUrl=https://artifactory.company.net/artifactory/releases\
-Dartifact=com.company.api:my-swagger-doc:$VERSION:zip:resources
This will download the my-swagger-doc-2.5.3-resources.zip. I don't know how to do this in XML in my pom.xml.
My goal is to generate Spring controllers from this swagger file.
My question is:
How can I download this zip artifact and let it extract in my target directory?
You can use unpack goal of Maven Dependency Plugin.
Find below an example snippet:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<type>zip</type>
<outputDirectory>target/</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
In my example, I have attached the execution to generate-resources phase. Depending on your build, you may have to attach the execution of this goal to the appropriate phase.

Download war file from nexus with maven

If I have the artifactId, the groupId and the version how I can configure the pom.xml so maven will download test.war file from the nexus repository.
I assume that this should happened in the
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
.....
</plugin>
In the execution block.
I already tried this but no luck:
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<artifactId>mytest</artifactId>
<groupId>com.mytest</groupId>
<version>${version}</version>
<type>war</type>
<destFileName>mytest-${version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
Are you certain that ${version} is being set properly for you? If you need the version of the current project, you should probably be using <version>${project.version}</version> and possibly setting up <dependencyManagement/> to handle that for you.

Gradle equivalent for mavens maven-dependency-plugin

I'm moving from Maven to Gradle,
In my maven code i used the "maven-dependency-plugin"
and i could not find an easy translation for the following:
so my question is how can i get my dependencies into a specific structure?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.group1</groupId>
<artifactId>artifact1</artifactId>
<version>1</version>
<type>swf</type>
<outputDirectory>/flex/output1</outputDirectory>
<destFileName>artifact1.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group2</groupId>
<artifactId>artifact2</artifactId>
<version>2</version>
<type>swf</type>
<outputDirectory>/flex/output2</outputDirectory>
<destFileName>artifact2.swf</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.group3</groupId>
<artifactId>artifact3</artifactId>
<version>3</version>
<type>swf</type>
<outputDirectory>/flex/output3</outputDirectory>
<destFileName>artifact3.swf</destFileName>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
so finding no equivalent in Gradle what i ended up doing is holding a map of artifact id to the new name (outputDirectory + destFileName) and after resolving the dependencies into a new special configuration which i called "Flex" and then renaming every dependencies from "flex" using the mapping.
For those looking for Maven-like dependency management features in Gradle, check out the Dependency Management Plugin. It provides familiar dependencyManagement blocks and BOM support.
Note that you don't need to use Spring in your project to use the plugin.

How to add resources of an external jar into my maven build jar?

I have a module , while building this module i want to include some resources from an external jar into my module jar.
I tried to use maven-shade-plugin but I am not able to achieve it it.
Any help is appreciated.
Unpack your external resource into project target, after this unpacked resources will be included in generated artifact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<type>jar</type>
<includes>*.xsb, *.properties</includes>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
For non-maven file use method from this answer https://stackoverflow.com/a/3264160/516167

Resources