Generate the maven artifact path - maven

Maven install knows all the artifacts generated by a build, and will push them locally.
Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the local repository.
the help plugin probably supports this, but not sure of right expression
# has all the pieces (artifact, version, type) but is it fair to assume filename will always be that combo?
mvn help:evaluate -Dexpression=project.artifact
Is there any way to get that list of paths from a maven command?
I want to generate a list of specific artifacts to persist as artifact results in a build process, without publishing to a maven repo.

Artifact paths in Maven repository will follow the following formula by default:
groupId is broken into folders using the full stops as delimiter, then artifactId and version form the last two folders
filename of the artifact consists of artifactId and version, type is defined by packaging
So, let's say you have a multi-module project with main pom.xml:
<groupId>com.foobar.my.business</group>
<artifactId>myApp</artifactId>
<version>1.0-SNAPSHOT</version>
And it has two submodules, first is web module that creates a REST endpoint:
<parent>
<groupId>com.foobar.my.business</group>
<artifactId>myApp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>myApp-web</artifactId>
<packaging>war</packaging>
Second one is persistence layer:
<parent>
<groupId>com.foobar.my.business</group>
<artifactId>myApp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>myApp-persistence</artifactId>
<packaging>jar</packaging>
Let's say your local repository is found from ~/.m2/repository. Then your artifacts will be saved in local repository at:
~/.m2/repository/com/foobar/my/business/myapp/1.0-SNAPSHOT/myapp-1.0-SNAPSHOT.pom
~/.m2/repository/com/foobar/my/business/myapp-web/1.0-SNAPSHOT/myapp-web-1.0-SNAPSHOT.pom
~/.m2/repository/com/foobar/my/business/myapp-web/1.0-SNAPSHOT/myapp-web-1.0-SNAPSHOT.war
~/.m2/repository/com/foobar/my/business/myapp-persistence/1.0-SNAPSHOT/myapp-persistence-1.0-SNAPSHOT.pom
~/.m2/repository/com/foobar/my/business/myapp-persistence/1.0-SNAPSHOT/myapp-persistence-1.0-SNAPSHOT.jar
An artifact's final build name and the local repository location can be tinkered with. But you can use the following expressions to check those:
${settings.localRepository} will return path to local repository.
${project.build.finalName} will return final build artifact name.
To get this list in almost the correct format, you can run:
On Windows mvn -q exec:exec -Dexec.executable="cmd" -Dexec.args="/C echo ${settings.localRepository}\${project.groupId}\${project.artifactId}\${project.version}\${project.build.finalName}.${project.packaging}"
On POSIX mvn -q exec:exec -Dexec.executable='echo' -Dexec.args='${settings.localRepository}/${project.groupId}/${project.artifactId}/${project.version}/${project.build.finalName}.${project.packaging}'
Then you just have to fix the full stops in groupId.
There is also a mvn dependency:build-classpath command which will show the location of each dependency on the file system that can come in handy sometimes.

Related

version property dynamic in pom.xml

I have a Maven pom.xml, I build project and executable jar deploy in Nexus with Jenkins.
But I want changes of version name according to branch name.
For example: I have in pom.xml
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
I need set like this : (Branch- Master/Test1/Test2/..)
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>BranchName_0.0.1-SNAPSHOT</version>
<name>iin-parent</name>
<packaging>pom</packaging>
How can this be done?
I was using MVN build like -Drevision=BranchName-SNAPSHOT clean compile package deploy. But I want dynamically fetch the branch name.
enter code here
If you use clean compile package deploy you are duplicating several parts..only clean deploy is needed. Please read the documentation about Maven Life cycle.
Furthermore if you like to change the version dynamically you can do that by using the correct properties (Starting with Maven 3.5.0+) which are ${revision}, ${sha1} and ${changelist} so for example:
<groupId>net.test</groupId>
<artifactId>test-parent</artifactId>
<version>${revision}</version>
<properties>
<revision>1.0-SNAPSHOT</revision>
</properties>
This can be done in Maven like this:
mvn -Drevision=2.0-SNAPSHOT clean package
or if you like to do this for a branch:
mvn -Drevision=2.0-BranchName-SNAPSHOT clean package
You have to be aware if you like to do mvn clean deploy please read carefully the docs and follow them.

How to force maven to always download parent pom

I am using a parent pom to share dependencies and plugins across multiple projects. The parent pom is deployed to our nexus repository. In child project, parent pom is included like this:
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>app-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.mycompany.app</groupId>
<artifactId>app-web</artifactId>
<version>1.2.0</version>
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://nexus/repo</url>
</repository>
</repositories>
</project>
When parent pom gets updated, would like to keep the same version so child project automatically gets the updated dependencies without updates. However, it seems parent pom is not updated even if I use
mvn clean -U
How do I force maven (maven 3) to always download parent pom from remote repo even if it is already in local repository?
If you have defined such parent which is a release it will be downloaded exactly once. Afterwards it will not downloaded a second time cause release are imutable. As already mentioned you mind need to think about using SNAPSHOT's instead.
you should re-install parent project into your local repo, try run mvn install under app-parent directory!
FWIW I had a case where maven was not downloading a parent but just a .lastUpdated file (which listed a download failure). Even if I ran help:effective-pom it seem to just ignore the fact the parent was undownloadable and absent, and continue on its merry way.
Fix was to upgrade from maven 3.3.3 to 3.3.9

What is required to use maven for artifact deployment?

I am setting up a CI system with repository archive, based on apache Archiva. Among various techniques for deploying file there, the most promising one seems to be using maven (as opposed to REST api that would require too much curl calls and Web interface that is not for automation).
It seems that for deploying artifact, such as zip archives of build artifacts, in maven there is a following plugin: deploy:deploy-file. However an attempt to simply invoke that command gave me no results.
I did not work with maven before; currently our builds are done by invoking cmake on source directory, then make from shell script. What do i need to add and have to be able to use maven for deploying the resulting artefact?
Is it necessary to create a pom file? If so, what steps do i need to add?
You can use deploy:deploy-file to upload these artifacts, and by default it will generate a POM for you. What you will need is to:
install Maven
create a settings.xml file, with a <server> element that contains the credentials for deploying to Archiva over HTTP
There's a bit more information here: http://maven.apache.org/plugins/maven-deploy-plugin/usage.html. If you are generating the POM, then you will need to supply the groupId (distinct grouping of artifacts), artifactId (filename without version), version (artifact version), and packaging (typically extension, such as zip) parameters along with the required ones of repositoryId, url and file.
However, it's not required to use Maven, or the REST API - you can also just use a simple HTTP PUT call:
curl -T artifact.zip http://localhost:8080/archiva/repository/my-releases/group/artifact/version/artifact-version.zip
You may also use scp, ftp, etc. to place the file directly in the Archiva filesystem. Note that in that case, you'll have to wait for Archiva's scheduled scan to pick it up.
Maven needs a WebDAV like repository (see also Nexus, Artifactory) to deploy its artifacts (mostly jar, war, ear or assemblies.
Look here for a how to on setting up Maven with Archiva.
Yes, it is necessary to have pom.xml in maven and also it is obvious that you need maven to be installed on your machine. Bare minimum, you need to provide artifact ID(like name of jar), group id(like package to identify the location of artifact in repository) and version. By default package structure will be jar unless you mention it as war/ear/pom. If you want to use any dependencies you can mention them in dependencies section.
Following is the minimum pom that is required.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
</project>
Here is the quick tutorial to get acquainted with maven.

Install remote maven artifact to local repository

I would like to install an artifact from maven central repository to my local repository. Can anyone help me on getting this? With other words, I want some jars from maven central repositories to be downloaded into my local repository, but using maven, not going in browser and downloading needed jar files.
I am not entirely sure why would you want artifacts from maven for reasons other than using them in a maven based project... But since that's what you want:
Maven installs artifacts locally when they are used - that is when you install a project that has them in dependencies. Therefore the simplest solution would be to create a dummy project, put the artifacts you want as dependencies and install it. Something like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dummy</groupId>
<artifactId>dummy</artifactId>
<version>dummy</version>
<dependencies>
<-- artifacts you want -->
</dependencies>
</project>
within pom.xml file in an empty folder. It will additionally create a dummy artifact in your local repository you might want to get rid of manually if it bothers you.

Tell Maven to look in repos for parent pom before looking in file system

Per Maven documentation Maven will only look in local and remote repos for a parent pom after it fails to find it locally. The best solution I've found to dummy this out is by adding
<relativePath>.</relativePath>
which is obviously a kludge and produces warnings (as it well should). Maven seems to be like file-system coupling when dealing with parent modules and multi-module projects so this is the only way I see to have both of those co-exist without something that feels obviously wrong (e.g. inheriting from a filesystem child).
You reference the parent pom using the tag:
<parent>
<groupId>com.mycompany</groupId>
<artifactId>my-parentpom</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
You must install the parent pom into your local repo using the mvn install -N command from the directory that contains the parent POM.

Resources