Build and Deploy a Maven Project to Nexus via Jenkins - jenkins

I have configured a simple maven freestyle project. I was able to successfully build the project but not deploy to Nexus. I get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.1:deploy (default-deploy) on project eqs_utility: Failed to deploy artifacts: Could not find artifact com.companyName.eqs:eqs_utility:jar:1.0.1-20190529.191240-1 in nexus (https://nexus.companyRepo.com/repository/maven-snapshot/) -> [Help 1]
I have tried to change configurations to simplify the project but still nothing. Setting.xml changes.
EDITED
I added the following to my POM.xml
<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>nexus</id>
<name>Company Nexus Repository</name>
<url>https://nexus.mycompany.com/repository/maven-release/</url>
</repository>
<snapshotRepository>
<uniqueVersion>true</uniqueVersion>
<id>nexus</id>
<name>Company Nexus Snapshots</name>
<url>https://nexus.companyName.com/repository/maven-snapshot/</url>
</snapshotRepository>
</distributionManagement>
Then, updated my settings.xml with this
<server>
<id>nexus</id>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
<!-- Another sample, using keys to authenticate. -->
<server>
<id>nexus</id>
<username>NexusUser</username>
<password>MyLongTokenValueHere</password>
</server>

According to Apache Maven Deploy Plugin setup
You need a section:
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>MyCo Internal Repository</name>
<url>Host to Company Repository</url>
</repository>
</distributionManagement>
As you're using Nexus, you probably need a section in the pom that aligns with your nexus repositories (one for snapshots and one for release artifacts since you can't combine them in nexus):
<distributionManagement>
<repository>
<id>mavenrepository</id>
<url>http://mavenrepository.companyName.com/nexus/content/repositories/m3</url>
</repository>
<snapshotRepository>
<id>tmavenrepository</id>
<url>http://mavenrepository.companyName.com/nexus/content/repositories/m3-snapshots</url>
</snapshotRepository>
</distributionManagement>
Plus of course, in your local settings or other private location
<server>
<id>mavenrepository</id>
<username>maven</username>
<password>foobar</password>
</server>
This is described by Sonatype and cleaner by others.

Related

Docker resolves maven artifacts only from local repository?

I have a working java spring-boot application that fetches a dependency from a gitlab repository.
That works fine on a local mvn package command. But as soon as I execute the mvn command inside a local docker container, maven tries to resolve the spring artifacts also from my gitlab repository. Why?
Dockerfile:
# syntax=docker/dockerfile:1
FROM maven:3.8.4-eclipse-temurin-11 as dependencies
COPY pom.xml .
COPY settings.xml .
RUN mvn --debug -U -B package -s settings.xml
pom.xml:
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
</parent>
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://git.example.com/api/v4/projects/295/packages/maven</url>
</repository>
</repositories>
<dependencies>
...
</dependencies>
</project>
Result:
[INFO] Scanning for projects...
[DEBUG] Resolving artifact org.springframework.boot:spring-boot-starter-parent:pom:2.6.6 from [gitlab-maven (https://git.example.com/api/v4/projects/295/packages/maven, default, releases+snapshots), central (https://repo.maven.apache.org/maven2, default, releases)]
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://git.example.com/api/v4/projects/295/packages/maven
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://git.example.com/api/v4/projects/295/packages/maven
[INFO] Downloading from gitlab-maven: https://git.example.com/api/v4/projects/295/packages/maven/org/springframework/boot/spring-boot-starter-parent/2.6.6/spring-boot-starter-parent-2.6.6.pom
settings.xml:
<settings>
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Private-Token</name>
<value>xxx</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
It must have been any sort of caching issue. I removed docker configs, restarted the machine, now it works.
Your query is why is resolve to your git repo right?
You are building the pom with your repo path, so it resolve to your repo
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://git.example.com/api/v4/projects/295/packages/maven</url>
</repository>
</repositories>
````

Peculiar Maven Repository Resolution Problem

I am facing a strange phenomena related to Maven repository resolution (Maven version: 3.8.4) which I am unable to explain. The matter is as follows:
We have a internal Nexus proxy setup for Central Repository. The URL is http://[companyDomain]:8081/repository/maven-central
I am declaring a repository in the pom.xml as follows:
<repository>
<id>nexus-maven-central</id>
<name>Maven Central Repository Internal Proxy</name>
<url>http://[companyDomain]:8081/repository/maven-central</url>
<layout>default</layout>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
The effective pom is showing the above repository as the first in the list of repositories and default Maven Central repository (id:central) after that as it should be.
However, when I build the pom, Maven is directly going to the Maven Central repository, not considering the above repository.
Now, when I start a Nexus instance in my local host and change the above URL to:
http://localhost:8081/repository/maven-central
Maven resolves the repository and downloads from the localhost.
Further, when I use the http://[companyDomain]:8081/repository/maven-central again in the pom and this time configure a mirror in the global setting.xml (I am not using user specific settings)
<mirror>
<id>nexus-maven-central-mirror</id>
<name>Mirror for Maven Central Repository</name>
<url>http://[companyDomain]:8081/repository/maven-central</url>
<mirrorOf>central</mirrorOf>
</mirror>
Maven perfectly resolves the internal proxy repository and downloads artifacts.
As for the environment, I am connecting to the corporate network through a VPN. IP of the internal Nexus server is resolvable when I am connected to the VPN; there are no VPN outages when I am trying the above.
I was expecting Maven to at least try the internal proxy repo when configured with the 'downloading from ...' message, nothing of that nature shows up in the console. It goes directly to Central Repository.
I further tested with the following URLs:
http://repo1.maven.org/maven2/ ---> Does Not Work
https://repo1.maven.org/maven2/ ---> Works
While I am aware that Maven does not prefer to download from 'http' and Maven Central explicitly states that it will send error if accessed over http, then the question is why is it downloading from localhost on http and not remote; does Maven have this behaviour of ignoring remote http.
Or are the 'localhost' and remote repos treated differently by Maven. Or are there some other matter I am missing.
I, unfortunately could not unearth much documentation on the above; any help would be appreciated.
The first thing is: I am declaring a repository in the pom.xml as follows:
If you use a repository manager always define the repositories via the settings.xml like this:
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>https://central</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://central</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
Remove all repository entries from your pom files...

Create local remote repository using Maven

I am using Maven for build and Jenkins for CI/CD. For production, need to keep all my Maven build jars in my remote repository, I am adding my local repository by
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://10.16.70.55:9001/var/lib/jenkins/.m2/repository</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>snapshots</id>
<name>Archiva Managed Snapshot Repository</name>
<url>http://10.16.70.55:9001/var/lib/jenkins/.m2/repository</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
Is this the correct way? Does Jenkins provide remote repository? Is there any other tools which we can use?
Have you looked at using an actual repository manager tool like Nexus OSS or JFrog Artifactory ?

Why does M2 Release Plugin on Jenkins not authenticate to Artifactory?

We are trying to get the M2 Release Plugin to deploy released artifacts to our Artifactory server. The configuration in our pom.xml looks like this
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
....
<distributionManagement>
<repository>
<id>artifactory</id>
<url>http://example.com/artifactory/jenkins-release</url>
</repository>
</distributionManagement>
Using the Config File Provide Plugin we specified a global settings.xml with the proper credentials
<settings>
<servers>
<server>
<id>artifactory</id>
<username>jenkins</username>
<password>secret!</password>
</server>
</servers>
</settings>
If we now start a release build on Jenkins, Maven tells us that there it received an HTTP 401 from artifactory
[INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project example-maven-plugin: Failed to deploy artifacts: Could not transfer artifact com.example.maven.plugins:example-maven-plugin:jar:0.0.9 from/to artifactory (http://example.com/artifactory/jenkins-release): Failed to transfer file: http://example.com/artifactory/jenkins-release/com/example/maven/plugins/example-maven-plugin/0.0.9/example-maven-plugin-0.0.9.jar. Return code is: 401, ReasonPhrase:Unauthorized. -> [Help 1]
In the server logs we see that a user "non_authenticated_user" is trying to do a HTTP PUT request to this URL.
Is there some configuration in Maven or Jenkins we are missing? The credentials are correct and if I use the settings.xml from Jenkins on my local machine everything works as expected.
There is a bug in the Maven release plugin prior to 2.2.2 that makes it ignore the settings file being passed to Maven by the Config File Provider. The solution is to specify a newer maven release plugin in your projects pom.xml, like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.4.2</version>
</plugin>
</plugins>
</build>

Maven cannot for life download maven-plugin-descriptor-2.0.6

Basic info:
I use embedded Maven 3.0.4 in Eclipse Juno (with JBoss Tools).
settings.xml (straight from community.jboss.org/wiki/MavenGettingStarted-Users):
<profile>
<id>jboss-public-repository</id>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>jboss-public-repository</activeProfile>
<activeProfile>jboss-deprecated-repository</activeProfile>
</activeProfiles>
Note: I tried also repository.jboss.org/nexus/content/groups/jboss and repository.jboss.org/nexus/content/groups/public/ with same results.
What I did:
I created Maven project from scrath using artifact org.jboss.spec.archetypes jboss-javaee6-webapp. System try to download repos. Everything downloads fine except two POMs:
maven-plugin-descriptor-2.0.6.pom
plexus-2.0.2.pom
I will focus on maven-plugin-descriptor, but plexus behave identically.
Error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.5:resources (default-resources) on project example:
Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.5:resources failed:
Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved:
Failed to collect dependencies for org.apache.maven.plugins:maven-resources-plugin:jar:2.5 ():
Failed to read artifact descriptor for org.apache.maven:maven-plugin-descriptor:jar:2.0.6:
Could not transfer artifact org.apache.maven:maven-plugin-descriptor:pom:2.0.6 from/to central (repo.maven.apache.org/maven2):
No response received after 60000 -> [Help 1]
Requested file is not present at all in repository.jboss.org/nexus/content/groups/public-jboss/org/apache/maven/maven-plugin-descriptor/2.0.6/ (returns 404).
I tried to go to repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-descriptor/2.0.6/ directly in browser. File maven-plugin-descriptor-2.0.6.pom is present, but after click it thinks... thinks... thinks... aaand connection reset. Note that many other POMs was downloaded from repo.maven.apache.org without problems. Only this file for some reason Just Don't Work(TM).
Other:
I am NOT behind proxy. I DID use "delete whatever.lastUpdated" trick. Even deleted entire repo to force downloading everything again. Did not work, of course.
Any ideas? Or maybe I am barking at wrong tree and some other repository must be set in settings.xml? I have impression that maven tries to download too old versions...
Problem was solved. If someone have similiar problem to mine, he have to use mirrors.
In settings.xml:
<settings>
...
<mirrors>
<mirror>
<id>uk.maven.org</id>
<mirrorOf>central</mirrorOf>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
</mirror>
</mirrors>
...
</settings>

Resources