Pushing artifacts to the Nexus using Jenkins maven-release-plugin - maven

In my Jenkins dashboard, I'm using maven-release-plugin (v0.16.2) to perform a maven release to Nexus repository. I have some issues while using this plugin.
Issue 1:
As shown in the image, it uses <project_name>-<release_version> as the SCM default tag. But I want to change it as v<release_version> value. Therefore, I have added the following change into my project pom.xml file's plugin section.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<tagNameFormat>v#{project.version}</tagNameFormat>
</configuration>
</plugin>
</plugins>
</build>
But still, it uses the aforementioned default tag. Is there any other configuration I have to do in Jenkins or pom.xml file?
Issue 2:
I have configured my pom.xml file with distributionManagement section to push my built artifact to the Nexus repository as follows.
<distributionManagement>
<repository>
<id>releases</id>
<name>Release Distribution Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
With this configuration, Jenkins pushes all the project built artifacts (packaging JAR artifact + dependency XML artifacts in M2) to the given Nexus repository successfully. But in my case, I want to deploy only the JAR file to the Nexus repository (excluding other dependency XML files). Is it possible to do with this Jenkins maven release plugin? Deploy only a specific file or directory?

Related

Not able to download Jars from nexus

I have project A which is dependency of project B. Since project B should get latest jar version of Project A . I purge Project A jar from local so Project B will get Project A jar from nexus since it has been deleted from local.
I can see in the logs that It can download jar from nexus . but still getting pom for jar is missing
below are the logs
Downloading from : ://10.245.240.43:8081/nexus/repository/maven-snapshots/com/sgl/smartpra/master/model/smartpra-master-model/0.0.1-SNAPSHOT/smartpra-master-model-0.0.1-20191126.094645-53.jar
[INFO] Downloaded from : ://10.245.240.43:8081/nexus/repository/maven-snapshots/com/sgl/smartpra/master/model/smartpra-master-model/0.0.1-SNAPSHOT/smartpra-master-model-0.0.1-20191126.094645-53.jar (183 kB at 1.7 MB/s)
The POM for com.sgl.smartpra.master.model:smartpra-master-model:jar:0.0.1-20191126.094645-53 is missing, no dependency information available
You can see that it can able to pull jars from nexus still getting artifact missing.
I added maven plugin to perge dependent jars from local so it will pull latest jar from nexus
refer Below snippet
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>purge-local-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>purge-local-repository</goal>
</goals>
<configuration>
<includes>
<include>com.sgl.smartpra.global.master.model:smartpra-global-master-model</include>
<include>com.sgl.smartpra.batch.global.model:smartpra-batch-global-model</include>
<include>com.sgl.smartpra.master.model:smartpra-master-model</include>
<include>com.sgl.smartpra.exception.txn.model:smartpra-exception-txn-model</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Below is nexus repository configuration for downloading jars from nexus
Please check below code
refer Below snippet
I have project A which is dependency of project B. Since project B should get latest jar version of Project A . I purge Project A jar from local so Project B will get Project A jar from nexus since it has been deleted from local.
<repositories>
<repository>
<id>maven-group</id>
<url>**strong text**/10.245.240.43:8081/nexus/repository/maven-group/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>

Deploy snapshots and releases via Maven commandline

I want to deploy projects inside my CI/CD pipeline without using the distributionManagement block inside my pom which would look like this:
<distributionManagement>
<repository>
<id>central</id>
<name>x-releases</name>
<url>http://serverhostname/artifactory/libs-release-local</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>x-snapshots</name>
<url>serverhostname/artifactory/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
I tried specifying the repositories via command line:
-DaltSnapshotDeploymentRepository=myserver::default::serverhostname/artifactory/libs-snapshot-local \
-DaltReleaseDeploymentRepository=myserver::default::serverhostname/artifactory/artifactory/libs-release-loca
But it fails with
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
Specifying -DaltDeploymentRepository allows me to deploy an artifact to a repository, but I am not sure how to distringuish between SNAPSHOT and RELEASE anymore. I thought maven would be able to do this somehow automatically.
What commandline options to use to replace this distributionManagement block?
Check your maven-deploy-plugin version.
I was using version 2.7 and getting the same error but upgraded to 2.8.2 and the build completed successfully.
Here's the dependency:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>

Deploy Maven project to local Artifactory service

I have forked a webjar project for working locally in my company's environment. We use Artifactory/Ivy for dependency management.
Currently Smart Table (and other webjars) pom.xml show the following for deployment:
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.5</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
It will by default publish to Sonatype, which is good for publicly-visible open source projects once you have release credentials.
However we do currently want to work locally on a fork of the project and deploy to our local Artifactory server. Contributions (to the real project) will be shared via Pull Request, so we are not interested in going to Sonatype repository.
Question
How do I change Maven pom.xml so that mvn deploy will deploy to a locally-configured Artifactory service? (For which credentials are stored in Maven configuration of course)
Bonus question
Can I tell Maven to publish using Ivy layout or should I create a new Maven-layout repository in Artifactory?
First option is to use the standard Maven deploy plugin
<distributionManagement>
<repository>
<id>repo-id</id>
<name>Artifactory</name>
<url>http://server:8081/artifactory/repo-id</url>
</repository>
</distributionManagement>
You should configure your settings.xml file to define corresponding entries which provides authentication information. Server entries are matched to the different parts of the distributionManagement using their elements.
<server>
<id>repo-id</id>
<username>repo-username</username>
<password>password/encrypted password</password>
</server>
Second option is to use the JFrog Maven Artifactory plugin, available at the JCenter repository in Bintray
<build>
<plugins>
...
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>2.4.0</version>
<inherited>false</inherited>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>publish</goal>
</goals>
<configuration>
<deployProperties>
<gradle>awesome</gradle>
<review.team>qa</review.team>
</deployProperties>
<publisher>
<contextUrl>https://server:8081/artifactory</contextUrl>
<username>username</username>
<password>{DESede}...</password>
<repoKey>libs-release-local</repoKey>
<snapshotRepoKey>libs-snapshot-local</snapshotRepoKey>
</publisher>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Through the Maven Artifactory Plugin, Artifactory is fully integrated with Maven builds and allows you to do the following:
Attach properties to published artifacts in Artifactory metadata.
Capture a BuildInfo object which can be passed to the Artifactory REST API to provide a fully traceable build context.
Automatically publish all build artifacts at the end of the build.
More detailed usage examples of the plugin can be found in this Github project.
Bonus question
Maven can only deploy to a Maven2 (default) or Maven1 (legacy) layout repository. You will have to create a new Maven repository in Artifactory.

mvn deploy:file to different repositories for snapshot and release version

Is it possible to in some way tell the maven deploy:file goal to deploy to two independent artifactories based on whether the version of the project is a snapshot / release?
I'm hoping there might be a property which indicates the fact the version has -SNAPSHOT prepended, or perhaps the default artifactory to deploy to (which has been worked out based on the version number already).
I thought about using two different profiles and working out if its a snapshot in ant by parsing the pom.xml file, but I'd rather a cleaner solution if possible.
Currently, my deploy plugin looks as follows, but this just deploys to the release artifactory regardless of the version;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>deploy-zip-created-by-ant-to-artifactory</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
<url>${project.distributionManagement.repository.url}</url>
<file>${project.basedir}/Build/deploy/MyArtifact.zip</file>
<pomFile>${project.basedir}/MyArtifact-pom.xml</pomFile>
</configuration>
</execution>
</executions>
</plugin>
Many Thanks
If you defined your repositories within your settings.xml you can use the
mvn deploy:deploy-file -DrepositoryId=releases -DartifactId=... -Durl=
Over here, I used the GMaven plugin to choose the repository from the distributionManagement section of the POM and store it in a property.
The deploy plugin can then use that property.
Maybe you want to use the build-helper-maven-plugin to deploy an additional artifact
This is presumably the Maven way:
<distributionManagement>
<repository>
<id>release</id>
<url>http://my-releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://my-snapshots</url>
</snapshotRepository>
</distributionManagement>
When doing a deploy of a snapshot version, it'll go the snapshots repository. For a non-snapshot release the regular repository will be used.
Just run deploy and it'll work. :-)

maven release:prepare not deploying the projects with release version

I have a flat project structure with multiple projects.
I am using Nexus for internal repository and SVN for Source code management.
I am able to deploy the SNAPSHOT build of my project.
In my parent pom i have added the maven release plug-in:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
</plugin>
and the distribution info:
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
When I am doing a mvn release:prepare, the artifacts with the release versions are not getting deployed to repo. So if I have a project A with dependency on project B. Project A is not able to get the artifact of B with the release version.
The release:prepare by default calls "clean" and "verify" goals which simply tries to compile and run test. So nothing is deployed to your remote repository nor installed in your local repository.
To handle dependencies in multi-module projects with the new release version you need to have things installed in local repository during release:prepare, so change the default goals to "clean" and "install" with the preparationGoals property.
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<preparationGoals>clean install</preparationGoals>
</configuration>
</plugin>
You can add any goals you would need during your build.
The actual deployment to remote repository will be done by the release:perform goal.
Laurent

Resources