Why doesn't Jenkins download my latest snapshot? - java

I have a Jenkins build job for a maven 3 project. The project has a SNAPSHOT dependency. The build failed because Maven can't find the SNAPSHOT artifact, which is deployed to a intranet Sonatype Nexus Repository. The SNAPSHOT repository is part of the "public" group, which is the mirror URL for <mirrorOf>*</mirrorOf>.
Jenkins is configured to create a local Maven repository local to the workspace (one repostiory per job).
All other non-snapshot dependencies are resolved and downloaded well. Other jobs for projects without SNAPSHOT-dependencies are also built successfully.
Things I tried so far (without success):
Expired Cache in Nexus
Checked the local repository (in the job directory) - there was no artifact directory
Set "Build -> Goals and options" to "-U clean install" in the job configuration
Wait one hour
My setup:
Windows Server 2003
Java 1.6.0_31
Jenkins 1.480
Maven 3.0.3

This could be the "gotcha" I also discovered, downloading snapshot revisions from Nexus.
The solution is provided in the Nexus book, but not fully explained:
<settings>
<mirrors>
<mirror>
<id>nexus</id>
<url>http://myserver/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</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>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
Seems one must explicitly tell Maven that the Nexus provided repository group can also contain Snapshot revisions. Presumably what this does is trigger Maven to start looking for the special metadata files that are used to discover which timestamped file is in fact the latest snapshot.

Since you already defined mirrorOf/*, just add this in your .m2/settings.xml to instruct maven to search that mirror also for snapshot:
<profile><id>alwaysactive</id>
<activation><activeByDefault>true</activeByDefault></activation>
<repositories>
<repository><id>unused</id><url>unused</url></repository>
</repositories>
</profile>

Related

Maven settings.xml - upload same build jar files to multiple maven repositories

I'm looking to use mvn deploy to deploy all of the same jar file build artifacts to different maven repositories. Basically, I'm looking to make duplicate copies in multiple places. I'm trying to do this because I'm trying to migrate from one repository to another and I want to push to both places as a transitional step. (And I can't mirror the repositories as a whole.)
My (probably flawed) understanding is that I'd setup multiple "repositories" and "servers" in my settings.xml.
from settings.xml
<servers>
<server>
<id>${env.MAVEN_SERVER_ID_1</id>
<username>${env.MAVEN_SERVER_USERNAME_1}</username>
<password>${env.MAVEN_SERVER_PASSWORD_1}</password>
</server>
<server>
<id>${env.MAVEN_SERVER_ID_2</id>
<username>${env.MAVEN_SERVER_USERNAME_2}</username>
<password>${env.MAVEN_SERVER_PASSWORD_2}</password>
</server>
</servers>
<profiles>
<profile>
<id>my-profile</id>
<repositories>
<repository>
<id>${env.MAVEN_SERVER_ID_1}</id>
<name>${env.MAVEN_SERVER_NAME_1}</name>
<url>${env.MAVEN_REPOSITORY_URL_1}</url>
<layout>default</layout>
</repository>
<repository>
<id>${env.MAVEN_SERVER_ID_2}</id>
<name>${env.MAVEN_SERVER_NAME_2}</name>
<url>${env.MAVEN_REPOSITORY_URL_2}</url>
<layout>default</layout>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>my-profile</activeProfile>
</activeProfiles>
Then, when deploying, this is run.
mvn --settings=/path/to/settings.xml \
deploy \
-DaltDeploymentRepository="${MAVEN_SERVER_ID_1}::default::${MAVEN_REPOSITORY_URL_1}" \
/path/to/project/
This only pushes to one of the 2 repositories, but how do I change it so the deploy will push to both repositories at the same time? This maven deploy is something that is currently automated, so I'm trying to update the automation to deploy to both places.
I'm pretty new to maven, so maybe this is an obvious answer, but I'd be very appreciative of any help.

How to upload or deploy multiple versions of an artifact to Sonatype Nexus?

I'm looking for a way to upload and store multiple versions of an artifact to the Sonatype Nexus maven-2 repository, so a team has access to previous versions of the artifact if a new version was released and uploaded.
I upload a java library to the hosted maven-2 release repository. Everything works well until I upload another version of the same library to the same repository.
After uploading the second version all files are present in the repository, but Maven cannot resolve not first nether second versions.
I tried both ways to upload artifacts - manually with the nexus UI and using the Maven deploy command. Results are the same.
I believe there is a way to store multiple versions in one repository.
Is there a special configuration for that case?
Please, help me to figure out how can I solve this issue.
As I mentioned, I have all settings that allow me to deploy and download one artifact from nexus.
In the library pom.xml I have:
<groupId>com.company.lib</groupId>
<artifactId>LibName</artifactId>
<version>1.0.7</version>
<name>LibName</name>
...
<distributionManagement>
<repository>
<id>testdeploy</id>
<url>https://nexus.company.com/repository/testdeploy/</url>
</repository>
</distributionManagement>
In the project where I want to download a library I have:
<dependencies>
<dependency>
<groupId>com.company.lib</groupId>
<artifactId>LibName</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>testdeploy</id>
<url>https://nexus.company.com/repository/testdeploy/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
In settings.xml I have:
<servers>
<server>
<id>testdeploy</id>
<username>...</username>
<password>...</password>
</server>
<servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>https://nexus.company.com/repository/maven-public</url>
<name>Nexus M2</name>
</mirror>
<mirror>
<id>central_new</id>
<mirrorOf>central</mirrorOf>
<url>https://repo.maven.apache.org/maven2</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>release</id>
<repositories>
<repository>
<id>testdeploy</id>
<name>custom repo</name>
<url>https://nexus.company.com/repository/testdeploy/</url>
</repository>
</repositories>
</profile>
</profiles>
After uploading two versions of the library to nexus I have the following file structure:
com
company
lib
LibName
1.0.6
LibName-1.0.6.jar
LibName-1.0.6.jar.md5
LibName-1.0.6.jar.sha1
LibName-1.0.6.pom
LibName-1.0.6.pom.md5
LibName-1.0.6.pom.sha1
1.0.7
LibName-1.0.7.jar
LibName-1.0.7.jar.md5
LibName-1.0.7.jar.sha1
LibName-1.0.7.pom
LibName-1.0.7.pom.md5
LibName-1.0.7.pom.sha1
maven-metadata.xml
maven-metadata.xml.md5
maven-metadata.xml.sh1
When I try to download any of these versions via Maven I get an error
"Could not find artifact com.company.lib:LibName:1.0.6 in nexus (nexus.company.com/repository/maven-public)"
Solution:
In my case there were two problems:
The "testdeploy" repository was not added in maven-public group.
The "testdeploy" repo was made to test deployment artifact's versions from maven. I wanted to deploy another version of the library into the real repo after testing. The problem was that the real repo still had the library with the same artifact id and version. So there was a conflict between the real repo and test repo ("testdeploy") even after adding the "testdeploy" version into the "maven-public" group. To test deployment I had to change the librarie's artifact id.
Once these two issues were solved I didn't need to specify "testdeploy" repository in pom.xml. Defining the settings of the mirror (maven-public) and the server credentials in settings.xml were enough to make it work.
Maven identifies a unique artifact by a combination of 3 values: the groupId, the artifactID and the version. Appending -SNAPSHOT to a version has some additional special behaviour described here
The unique identifier for your artifact per your first pom snippet, would be
com.company.lib:LibName:1.0.7
If you want different addressable artifacts, change the version number. If people are still using the original 1.0.7 you should be producing 1.0.8 or some other incremented version.
Worth noting that projects that build in this often use SemVer and it's a convention that works in well understood ways so is probably a safe place to start.
EDIT TO ADD:
Based on comments, and re-review it looks like whatever is trying to pull 1.0.6 is not doing so from https://nexus.company.com/repository/testdeploy/. Instead it's trying to pull from nexus.company.com/repository/maven-public and failing.
For the project trtying to pull 1.0.6 what is the repository config? Your settings.xml only adds the testdeploy repo if the releases profile is active, so the testdeploy repo would need to be called out in the pom.
Your approach is correct and the normal way to work with different versions.
I guess you just have problems with different repository definitions in your Nexus, or some other network related issue.

Create Maven Project with Nexus Repository OSS 3 [offline]

My problem is that I can't create a new Maven project after adding my Nexus repository to the .m2 settings.xml.
I've installed Nexus Repositoy Manager OSS 3.0.2 as my local Maven repository. I have a machine which is in offline mode and can not connect to the internet. What I can do is transfer data from an online machine to it thought. So I can deploy all necessary libraries on the online machine and just switch the /data folder later on.
Error after creating a new Maven project with Eclipse:
Could not calculate build plan: Plugin org.apache.maven.plugins:maven- >resources-plugin:2.6 or one of its dependencies could not be resolved: >Failed to read artifact descriptor for org.apache.maven.plugins:maven- >resources-plugin:jar:2.6
The maven-resources-plugin-2.6.jar is deployed/available on my Nexus Repository: Path org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar
My Maven settings.xml (partly):
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-test/</url>
</mirror>
</mirrors>
...
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
...
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
As you can see my repository is named "maven-test". Is it normal that the Nexus repositories are not browseable by their URLs (http://localhost:8081/repository/NAME_OF_REPO/)?
Is there any documentation which Maven libraries are mandatory to create a simple Maven project. The minimum amount of .jars?
What I've tried so far:
created a simple Maven project on a internet connected machine and downloaded all necessary Libraries with:
mvn dependency:go-offline. After that I deployed all .jars from my local m2 repository to Nexus (about 160 Jars - luckily I used a Shell script for the deployment). For the offline simulation I deleted my local repository and went offline. Now the error occurs after creating a new Maven project in Eclipse.
I'm using Maven 3.3.9 - Java 1.8 - Eclipse 4.4 - Mac OS
Please any help would be appreciated!
Thanks
[Edit] forgot to upload the .pom files to Nexus. Clean install worked for a existing project. Still got the error after creating a new Maven project
Run a mvn clean install on the project without using the nexus tool.
This will download all the required jars and poms to your local repository as specified in your maven settings.xml
Copy all of these files to your offline nexus installation.
The caveat with any new projects you create each time is that they may require new jars and poms that weren't used by the first project and so were never downloaded and copied to your Nexus install which will give you issues.

maven and eclipse build conflict

I'm new to maven and eclipse and trying to familiarize myself with an existing framework we have.
On ubuntu I installed maven3, pulled down 2 repositories from git (one relies on the other), did a mvn clean install on both and then imported those two into Eclipse and created a settings.xml file with the contents below and placed it in the .m2 directory. I also scp'ed over the "repository" folder into .m2 from another user.
Issues:
on the command line, "mvn clean install" generates a failed build (complaining the url specified in the mirrors section is refusing connection) but works when I take out the mirrors section. However I can only build within eclipse (run as maven build with goals- jetty:run) with the mirrors section intact. Without the mirrors url, it will download a bunch of stuff and then fail due to dependencies. What is going on here and how do I resolve this. What is being looked at by maven? the settings.xml or repository, how are they related? It seems without the mirrors url, it's just searching within repository? I know it's somewhat vague.
<settings>
<servers>
<server>
...
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>..</id>
<mirrorOf>..</mirrorOf>
<url>..</url>
</mirror>
</mirrors>
<profiles>
<profile>
<repositories>
..
</repositories>
<pluginRepositories>
<pluginRepository>
..
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
..
</activeProfiles>
</settings>
Please check the usage of mirrors here. And also why did you create a new settings.xml file? Maven's main settings file can be found at %MVN_HOME\conf", for example in my case it is located at "C:\WorkSoft\apache-maven-3.2.1\conf" and this settings file is used when you run the build from command line.

How can I add source code to my dependency libraries in Maven?

For instance, I have included into my dependencies junit-addons : junit-addons. But in the maven repository there isn't any source code. And I know it exists (I have downloaded it). How can I modify the dependencies in order to use libraries from my local project instead from the maven repository (I would omit the junit-addons from the respository and use a local JAR and its source code instead).
Note: I use m2eclipse.
I've solved the problem in a very straight forward way:
I have copied into the folder ${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/ the source file following MAVEN standard: {artifactId}-{version}-sources.jar and it works as a charm! Eclipse checks the local repository and finds the sources.
I don't know if this is the MAVEN way, though.
How can I modify the dependencies in order to use libraries from my local project instead from the maven repository
You can't. If a sources JAR isn't available in the central repository, just put the sources somewhere on your file system in a folder, JAR or zip (you could install:install-file a sources archive in your local repository, following Maven's conventions) and setup Eclipse to use them (right-click on the JAR under Maven Dependencies in the Package Explorer, select Java Source Attachment and setup the Location path).
I use free version of Artifactory repository. I created a jar file {artifactId}-{version}-sources.jar and uploaded to the repository into the same group-id as binary jar file.
Then in my pom I added dependency:
<dependency>
<groupId>mygroupid</groupId>
<artifactId>artifact</artifactId>
<version>1.0</version>
<classifier>source</classifier>
</dependency>
During maven build phase source jar was downloaded to my local repository.
I use netbeans 7.0 and it automatically managed everything for me. For example, right click on method and choosing go toSource correctly brings me to source code in the jar.
You could use the install-file mojo to locally install artifacts into your local maven repository. If you want to share this artifact with others (say your team or another workstation), you could use your own repository manager (e.g. Nexus) and configure it as a mirror for any repository, e.g. central. Nexus will fetch (and cache) artifacts from central. Additionally, you may upload just about any artifact (like junit-addons sources) to your nexus installation.
In order to do configure a mirror, you'll have to edit ${user.home}/.m2/settings.xml
<settings>
<!-- SNIP -->
<mirrors>
<mirror>
<id>nexus-releases</id>
<mirrorOf>*</mirrorOf>
<!-- replace nexus.example.com with the location of your nexus installation -->
<url>http://nexus.example.com/releases</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<id>nexus</id>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</profile>
</profiles>
</settings>
After you have downloaded it into your local repository, you could make a copy of it. Give it a new artifactId (e.g. libraryName-myVersion) and add dependencies in the pom. Make sure you change the folder names, jar names, pom names and the artifactId itself in the pom. Store everything in your local repository. Now you can use your hacked version of your dependency.
But to be honest, I do not thing this is a good idea to do. But maybe it helps/could not be avoided in your case.

Categories