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

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>

Related

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.

maven: With one pom.xml I am creating jar and tar.gz but don't want to deploy jar

With one pom.xml first I am creating a jar file with maven-jar-plugin and signing it with maven-jarsigner-plugin, second I am creating a tar.gz package with maven-assembly-plugin, copying jar file and other necessary files into tar.gz. Just because only tar.gz package is enough for me, I want only tar.gz package to deploy remote repository. When I run the "mvn deploy" command, both the jar and tar.gz packages are being deployed. Are there any method for not to deploy jar file to remote repository.
I tried your suggestion. It is not working for maven-deploy-plugin but it is working for maven-install plugin. Here is the relative part of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/${project.build.finalName}-dev.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
<classifier>dev</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>http://repo/artifactory/snapshots</url>
<file>${project.build.directory}/${project.build.finalName}-dev.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
<classifier>dev</classifier>
</configuration>
</execution>
</executions>
</plugin>
maven-install-plugin is installing only tar.gz file to local repository but maven-deploy-plugin deploying both tar.gz and jar files to remote repository. I think this behaviour can be maven-deploy-plugin's bug.
By default the Maven Deploy plugin will deploy all the artifacts attached to your project or module, i.e. both your .jar and tar.gz file.
deploy:deploy is used to automatically install the artifact, its pom and the attached artifacts produced by a particular project.
What you can do is skip the deploy:deploy goal and configure a personalized deploy:deploy-file goal, such as:
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip> <!-- Skip the default deploy -->
</configuration>
<executions>
<!-- Deploy our tar.gz -->
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/${project.build.finalName}.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
You'll have to configure <file> to use your generated tar.gz file.

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?

is it possible to version and deploy a configuration file to nexus via maven commands

I am working on a java project and I would like to version and store a configuration file on nexus. Lets assume the file structure of java project is as below.
src/
conf/application.config
pom.xml
Is it possible to deploy application.config file to nexus when I run mvn clean install. After each build I expect an application artifact and a configuration artifact to be deployed to nexus. Is there any maven plugin for this purpose.
I manged to deploy file with maven-deploy-plugin.
http://maven.apache.org/plugins/maven-deploy-plugin/usage.html
You can find an example below.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy-file</id>
<!-- change to deploy-->
<phase>install</phase>
<goals>
<goal>deploy-file</goal>
</goals>
</execution>
</executions>
<configuration>
<file>app.properties</file>
<repositoryId>stack.example.release</repositoryId>
<url>http://nexusserver/nexus/content/repositories/releases/</url>
<groupId>com.stack.example.config</groupId>
<artifactId>app</artifactId>
<packaging>properties</packaging>
<version>1.0.0</version>
</configuration>
</plugin>
</plugins>
</build>

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.

Resources