Maven include zip dependency - maven

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.

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>

Unzipping downloaded artifacts + dependency list

We use maven-dependency-plugin to download ZIP artifacts from our repository in Nexus and then unzip them. The corresponding command in pom.xml is like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-zip-artifacts</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.ourdomain</groupId>
<artifactId>our-item</artifactId>
<version>1.0.0</version>
<classifier>bin</classifier>
<type>zip</type>
<outputDirectory>./ourfolder</outputDirectory>
<overWrite>true</overWrite>
</artifactItem>
Everything works perfectly.
Nevertheless, we'missing one thing: when we create a site for the artifact (command mvn site) the downloaded and unzipped artifacts don't appear under Dependencies because the corresponding entries in pom.xml's <dependencies> section are missing. Of course we can make the entries but they are REDUNDANT to those made above. Having dozens of artifacts it's pretty lot.
Is there any possibility to solve this problem in an 'elegant' way?

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.

copy dependency assembly resources to specific folder

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>

Invoke a jar file in the M2 repository

I have a project, in which I want to invoke another Jar file in M2 repo during the post execution phase of the current project.
Sample skeleton of my POM
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
executable>java</executable>
<arguments> <argument>-jar</argument>
<argument>JarToInvoke.jar</argument>
</arguments>
<**workingDirectory**>/C:/path to repo</workingDirectory>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<dependencies> <dependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
I tried with maven-exec-plugin, but having the following issues;
Where I need to specify to JarToInvoke dependency ? As a project dependency or as a exec-plugin dependency ?
With hard coding the working directory(/C:/path to repo), I am able to invoke the JarToInvoke artifact. But it is not a good solution, because finally this project should run in any m/c with different OS's. So how can I make the exec-plugin to search for the JarToInvoke artifact in the M2 repo of the project(default classpath) ?
3.While hard coding the M2 repo path in the working directory, I was able to invoke the JarToInvoke artifact. But while running the JarToInvoke artifact, it throws another dependency issue, some of the log4j dependencies to the JarToInvoke could not find. I made the JarToInvoke as a shaded jar and it work as expected. But it is not a permanent or good solution(Because the shaded jar size is of 35 MB). How can I instruct the exec-plugin to look for the dependent Jars in M2 repo.
Please share your suggestions. Thanks in Advance.
This example page from the Exec plugin's documentation describes what you want I think.
If you could use the exec:java goal instead of exec:exec, finding the JVM is taken care of for you. You can also pull in either plugin dependencies or project dependencies by changing the includeProjectDependencies and includePluginDependencies configuration options of the plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
</executableDependency>
<!-- Look up the main class from the manifest inside your dependency's JAR -->
<mainClass>com.example.Main</mainClass>
<arguments>
<!-- Add any arguments after your JAR here --->
</arguments>
</configuration>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>GroupId of JarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
The only disadvantage is that you have to explicitly specify the main class in the JAR to run. You can look this up by opening up the manifest in the dependency JAR and read the Main-Class attribute.
If you really need to use exec:exec, you could use the Maven Dependency Plugin's copy-dependencies goal to copy dependencies from your local repository to a predefined location (such as ${project.build.directory}/exec-jars) and then you can feed this directory in the exec plugin's workingDirectory configuration option.
Probably an easier way to locate the absolute path to the jar file would be to use maven-dependency-plugin with properties goal.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>exec-one</id>
<phase>verify</phase>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>${GroupIdofJarToInvoke:JarToInvoke:jar}</argument>
</arguments>
<workingDirectory>/C:/path to repo</workingDirectory>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<dependencies>
<dependency>
<groupId>GroupIdofJarToInvoke</groupId>
<artifactId>JarToInvoke</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependencies>

Resources