Jenkins - deploy artifacts to Maven repository - maven

I have a basic Springboot Maven project and I want to be able to deploy it and make API call as it works in local.
I have a remote linux machine with Jenkins on it, and I am able to make a build of my application correctly. Now I want to deploy this build in the same linux machine, in a certain folder /deploy.
Right now I have added a Post Build Action on Jenkins to Deploy artifacts to Maven repository having the following parameters:
and right now I did not make any changes to my pom.xml or my maven settings.xml.
The error that I get is the following:
[INFO] Deployment in http://localhost:8080/deploy (id=test2,uniqueVersion=true)
Deploying the main artifact reag.login-0.0.1-SNAPSHOT.jar
Downloading: http://localhost:8080/deploy/reag/login/reag.login/0.0.1-SNAPSHOT/maven-metadata.xml
ERROR: Failed to retrieve remote metadata reag.login:reag.login:0.0.1-SNAPSHOT/maven-metadata.xml: Could not transfer metadata reag.login:reag.login:0.0.1-SNAPSHOT/maven-metadata.xml from/to deploymentRepo (http://localhost:8080/deploy): Access denied to: http://localhost:8080/deploy/reag/login/reag.login/0.0.1-SNAPSHOT/maven-metadata.xml , ReasonPhrase:Forbidden.
org.apache.maven.artifact.deployer.ArtifactDeploymentException: Failed to retrieve remote metadata reag.login:reag.login:0.0.1-SNAPSHOT/maven-metadata.xml: Could not transfer metadata reag.login:reag.login:0.0.1-SNAPSHOT/maven-metadata.xml from/to deploymentRepo (http://localhost:8080/deploy): Access denied to: http://localhost:8080/deploy/reag/login/reag.login/0.0.1-SNAPSHOT/maven-metadata.xml , ReasonPhrase:Forbidden.
The machine where I'm working is protected by username and password, I tried to put them in the settings.xml file but nothing changes. Does anyone know which are the steps to make this process work?
Thanks in advance.

Local artifact deployment is done by mvn clean install.
The install goal copies your artifact into your local maven repository (Default [USER_HOME]/.m2/repository).
If you want to deploy your artifact to another service, like a Nexus Maven Repository, then you need to deploy and also provide the credentials for that machine in your settings.xml, or even better setup a private/public key authentication for the machines.
I think Jenkins is trying to do the remote deploy, which is only working if you run a Maven Repository on your machine (like the Nexus)

"deploy" in Maven parlance means "upload the built artifacts and their metadata (such as pom files) to a repository manager.
It does not mean "copy the artifact to any location that you want".
Therefore as #funfried mentioned, you need to actually have a repository manager running.
If that is the case then the following is how you set up the maven configuration.
Maven links credentials to servers via the id element.
Your settings.xml file would have your credentials setup something like:
<servers>
<server>
<id>newhope-nexus</id>
<username>steve</username>
<password>{1T7Jmp/PBoQH4cvFjZDTaDe/F/Z+D9rJ925rf+3H1LY=}</password>
</server>
</servers>
And then your project model (project pom or parent pom) should define it's distributionManagement:
<distributionManagement>
<repository>
<id>newhope-nexus</id>
<url>http://newhope:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>newhope-nexus</id>
<url>http://newhope:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
Note that the ids all match.
The mvn deploy from Jenkins should then work correctly.

Related

Publishing SNAPSHOT to nexus from jenkins/maven build

I'm trying to publish the result of a build jenkins/maven to a Nexus repository.
The build is a war SNAPSHOT, here is the beginning of the POM :
<http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.elis</groupIenter code hered>
<artifactId>accueil_ce</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>accueil_ce</name>
I use clean install maven goals for the build.
I've installed and configure the Nexus Repository Manager Publisher plugin for jenkins so it can publish to my nexus Repo in a post build step.
The problem is :
if I configure the repo as 'snapshot' and 'allow redeploy' in Nexus, it doesnt appear in the list of repos available in Jenkins, when configuring the after build step 'Nexus Repository Manager Publisher' of the job.
if I configure the repos as 'release' and 'allow redeploy' in Nexus, then I got the following error when running the jenkins job :
java.io.IOException:com.sonatype.nexus.api.exception.RepositoryManagerException: Unable to upload component: Bad Request The version 0.0.1-SNAPSHOT does not match the repository policy!
I don't understand, there is no way to publish SNAPSHOTs to nexus from jenkins builds with this plugin ??
You need to add distribution management to you pom to control snapshot and release repos.
See https://maven.apache.org/pom.html#Distribution_Management and How to configure maven project to deploy both snapshot and releases to Nexus?

How to update release artifacts in Maven?

I have the following case:
- Maven project compiles fine on one machine where artifact A (release one) exists in that machine's local Maven repository,
- artifact A was removed from the remote repository.
How can we be sure that the project builds fine on a new machine where local Maven repository is empty?
Try the install task install-file to put the jar into your local repo from your local hard drive:
mvn install:install-file -Dfile=<path-to-file>
See maven.apache.org for more details.
Few cases :
One, if you own artifact A and use <repository> to keep the versions in sync. You can anytime rebuild your project on the new machine to fetch the artifact.
Second, If artifact A is owned by you and you didn't use repositories until now, you can simply bump up its version and deploy it on the remote server(<repository>) and consume the new version on the machine where you want to. Use <repositories> in your pom.xml to specify the remote path as follows :
<repository>
<id>clojars</id>
<name>Clojars Maven Repository</name>
<url>http://clojars.org/repo/</url>
</repository>
Third, if you don't own the artifact A and it exists in a public maven central repositories, all you need to do is rebuild the correct dependency to fetch it over the net.
Worst case, the artifact A used is owned by you and doesn't exist in a public central repository. You can still copy-paste the jar file using any mode and move it to the machine's /m2/repository where you require it. But do note in such case you shouldn't reach this case ever again.
Also in the last case, you can copy it into your project and use it as suggested by #Adam
mvn install:install-file -Dfile=<path-to-file>

Maven Configuration Issue : Failed to find plugin prefix deploy

I am trying to setup Maven(3.2.5) on linux with nexus repo for non Java artifacts. I was going through all the SO questions, and got to know that with Maven command line, one can publish artifacts to repo without POM for non java artifacts.
When I am running maven deploy file command to push non java artifacts(zip file) to nexus repo, and it throws the following error
"Failed to find plugin prefix deploy". I
Here is the pluginGroup tag from my settings.xml, which is under conf dir. How do I get rid of this failed to find plugin error.
Thanks for your time!
<pluginGroups>
<pluginGroup>org.apache.maven.plugins</pluginGroup>
</pluginGroups>
This plugin group tag is not required in setting.xml file. I was not able to download plugin from maven due to firewall issue.
after setting up proxy, I was able to use maven build successfully.

maven automatically deploy artifacts on Archiva?

Maybe the question is asked for more than hundred times, but I didn't find it with the search function.
Ok we've a CI Server running Jenkins and Archiva. Building our code with maven. But now our IT Department changed the proxy configuration for the CI Server. It isn't able to go online.
When I use a new artifact and build it locally, mvn downloads this and stores it in the local repo.
But when I commit the sourcecode in the SVN Jenkins fails, caused of the missing internet connection.
So is there a way to automatic deploy artifacts and maven-plugins to Archiva when I build my projects local with maven?
use mvn clean deploy that will deploy to Archiva.
You have to configure correctly distributionManagement section in your pom.
See http://archiva.apache.org/docs/1.4-M4/userguide/deploy.html
I created a Jenkins job that will execute a shell script to ask Maven to deploy our private artifact in Archiva
Prerequisites
In your .m2/settings.xml file add your server, i.e.
<server>
<id>archiva.release</id>
<username>admin</username>
<password>123456</password>
</server>
Deploy 3rd party artifacts to archiva repository shell script
#!/bin/bash
rm_host=localhost # archiva server address
rm_port=8080 # archiva port
major_version=1 #
minor_version=0 #
version_build=1 #
repository=private # your repository id in archiva
mvn deploy:deploy-file
-Dfile=myApp.jar
-DgroupId=com.company
-DartifactId=myApp
-Dversion=${major_version}.${minor_version}.${version_build}
-Dpackaging=jar
-Durl=http://${rm_host}:${rm_port}/repository/${repository}/ -DrepositoryId=archiva.release

Divert to locally stored maven-install-plugin

I tried to upload a Windows build to Nexus with the Maven Commandline:
mvn deploy:deploy-file -Durl=http://unity.apps.company.net/nexus/content/repositories/idesktopbuildimages-releases/ -DrepositoryId=idesktopbuildimages-releases -DgroupId=images.WINDOWS7X64EnterpriseSP0unattendedcapture.sources -DartifactId=install -Dversion=6.4 -Dpackaging=wim -Dfile=install.wim
And got the error:
Downloading:
http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom
[WARNING] Failed to retrieve plugin descriptor for
org.apache.maven.plugins:maven-clean-plugin:2.4.1: Plugin
org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its
dependencies could not be resolved: Failed to read artifact descriptor
for org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1
This happens because I don't have Internet access (only Intranet access).
So is there the possibility to divert to a locally stored maven-install-plugin file in my Maven Command?
PS: I tried -DpomFile but got the same output.
Solution:
Adding the Proxy settings to my settings.xml
If you don't have internet access, I assume you have a mirror of maven central available on your intranet. If it's the case you must configure your settings.xml to declare the mirror. So add something like that in your settings.xml file:
<mirrors>
<mirror>
<id>central-mirror</id>
<name>Local mirror of central repo</name>
<url>http://unity.apps.company.net/nexus/content/repositories/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
EDIT
Other thing to look at : you may need to configure some proxy settings (it seems strange that they aren't already there...)
see this
You have to set up your settings file to access the public group of Nexus as described in the Nexus book and ensure that your Nexus server has access to Central.
The url for the public group should be .../nexus/content/groups/public. This default group has the "Central" repository in it.
If you are using a different group like your "releases" you should ensure that the Central repository (or Maven Central in older Nexus releases) is part of the list of ordered group repositories as visible at http://books.sonatype.com/nexus-book/reference/confignx-sect-managing-groups.html#fig-group-config
Once that is set you can confirm that the Central repository is reachable by checking out the the proxy repository Central itself in the repositories view and confirm that you can access the remote repo by using the "Browse Remote" tab visible in http://books.sonatype.com/nexus-book/reference/confignx-sect-manage-repo.html#fig-repo-config
If this browsing does not work you Nexus server is most likely blocked by an internal proxy server. If that is the case you have to configure it in Administration - Server - Default HTTP Proxy Settings.

Resources