Deploy snapshots and releases via Maven commandline - maven

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>

Related

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

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?

GitLab Package Registry as Maven Artifactory

I am trying to setup a GitLab project as the remote maven repository for all my other projects using GitLab Maven Package Registry. I have uploaded all dependency jar files to the package registry. All the jar files have been uploaded using maven's deploy feature. I am trying now to setup this project's package registry as the remote maven repository for all my other projects. The necessary repository settings is provided in the package registry itself, in each individual artifact page, as follows:
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://<gitlab_instance>/api/v4/projects/<project_id>/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://<gitlab_instance>/api/v4/projects/<project_id>/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://<gitlab_instance>/api/v4/projects/<project_id>/packages/maven</url>
</snapshotRepository>
</distributionManagement>
However, even after adding this in the pom.xml of other projects, their builds are failing citing dependency unavailability. It seems that this project's package registry is unreachable from other projects. What am I doing wrong here? Are there any additional steps I need to undertake?
Did you amend the settings.xml which you are using when calling a maven command?
There something like this:
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Job-Token</name>
<value>${env.CI_JOB_TOKEN}</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
should be present. To use the settings.xml with your specific maven command, you can specify it by typing:
mvn -s settings.xml package
You need to add an authenticate header(personal token, CI job token, or deploy token).
This is the doc: Adding the GitLab Package Registry as a Maven remote

Can't discover maven.restlet.com

When i build my project the jars for org.restlet.jee, as a dependency for apache-camel, are missing.
[WARNING] The POM for org.restlet.jee:org.restlet.ext.spring:jar:2.2.2 is missing, no dependency information available
So i added in Nexus a new proxy repository for http://maven.restlet.com/.
After adding there is an error during the discovery process:
Remote strategy prefix-file on M2Repository(id=maven-releases) detected invalid input, results discarded: Prefix file does not start with expected "## repository-prefixes/2.0" header, refusing to load the file.
Is there any solution how i can use the restlet repository with Nexus?
Edited: my pom.xml
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://xxx:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://xxx:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>deployment</serverId>
<nexusUrl>http://xxx:8081/nexus/</nexusUrl>
</configuration>
</plugin>
...
</plugins>
</build>
The prefix file message has to do with automatic routing. The site at maven.restlet.com doesn't support this feature. It's nothing to worry about though, see here for an explanation:
https://support.sonatype.com/entries/30645946-
Check to make sure the proxy repository you just added is a member of the group repository you've configured maven to use in your settings.xml file.
If that isn't the problem you can get information about this problem by using the technique described here:
https://support.sonatype.com/entries/21433982
I've had the same problem recently with restlet repo and proxying it through nexus. The problem is it doesn't accept proxying, you'll get this information by using techniques mentioned by rseddon.
To solve the problem you have to update your proxy repository configuration in nexus by setting a user agent in the optional http request settings to something being used by browsers, e.g. Mozilla/5.0 (X11; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0.
It should help.

how to deploy my artifact on to my nexus?

I am using nexus open source as my repository manager for Maven 3.0.3
Maven is able to create artifact *.jar.
Now, I would like to know how I can push the generated artifact *.jar to the nexus repo manager, so that other dependent modules can pull from it.
I referred to this guide.
In settings.xml, I have
<server>
<id>nexus-site</id>
<username>admin</username>
<password>xxxx</password>
</server>
It fails.
How can invoke my deployment from mvn command or how to deploy my artifact on to my nexus?
Just try
mvn deploy
that will deploy your artifact to the nexus repo manager.
Have you configured the distributionManagement section ?
And if you want to add it to the snapshot repository, you need the following configuration inside your pom.xml
<distributionManagement>
<repository>
<id>nexus-site</id>
<name>MyCo Internal Repository</name>
<url>http://Nexus url</url>
</repository>
<snapshotRepository>
<id>nexus-site</id>
<name>Your Snapshot Repository</name>
<url>http://Nexus url</url>
</snapshotRepository>
</distributionManagement>
Repository element should also be specified.
Snippet:pom.xml
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>MyCo Internal Repository</name>
<url>http://Nexus url</url>
</repository>
</distributionManagement>
There are two ways to do so.
The first is do it via Nexus web interface, just upload the artifact with necessary project information (groupId, artifactId, version)
The other is using mvn deploy. You need to set distributionManagement for repository to upload to, and user to authenticate as.
The second approach is strongly recommended if you are going it do deployment regularly. It is automated, and you can leverage on other Maven commands like mvn release

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