Release a snapshot to nexus using maven 3.0.5 - maven

I am unable to release a snapshot version of an artifact which I build using maven to nexus. The version of my artifact states 1.0.0-SNAPSHOT.
I can execute mvn clean install without an issue. But when I try to deploy using mvn deploy , I get the following error :
Return code is: 400, ReasonPhrase: Repository version policy: RELEASE does not allow version: 1.0.0-20161019.214318-1. -> [Help 1]
According to what I was able to find out was that maven3 adds the timestamp instead of the SNAPSHOT suffix on the artifact that I want to deploy. The <uniqueVersion> tag of maven is not supported in maven3. What is the approach that I need to take to deploy these artifacts using mvn deploy command.
Updated :
pom.xml
<distributionManagement>
<repository>
<id>my-nexus-snapshots</id>
<name>Internal Snapshot Releases</name>
<url>http://localhost:9999/repository/maven-snapshots/</url>
</repository>
<snapshotRepository>
<id>my-nexus-releases</id>
<name>Internal Releases</name>
<url>http://localhost:9999/repository/maven-releases/</url>
</snapshotRepository>
</distributionManagement>
settings.xml
<server>
<id>my-nexus-snapshots</id>
<username>user</username>
<password>user123</password>
</server>
<server>
<id>my-nexus-releases</id>
<username>user</username>
<password>user123</password>
</server>

Usually, your nexus has separate repositories "snapshots" and "releases". SNAPSHOT versions are deployed to the former, non-SNAPSHOT versions to the latter. For deployment, these repositories have to be specified by you. You can do this by adding the distributionManagement section to your pom. There you can define specific targets for both targets.
<distributionManagement>
<repository>
<id>releases</id>
<name>releases</name>
<url>http://somerepo:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>snapshots</name>
<url>http://somerepo:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>

If you are using Gradle it can be done in your repositories settings.
Just add the maven-snapshots url
For example:
repositories {
maven {
url = 'http://nexus.something.com/repository/maven-central/'
}
maven {
url = 'http://nexus.something.com/repository/maven-releases/'
}
maven {
url = 'http://nexus.something.com/repository/maven-snapshots/'
}
}

Related

Retrieving dependencies from maven repository

So for my maven project I require jar dependencies that are available in public repositories and also jar dependencies that are on our company internal repository.
How do I configure my pom.xml so that I will retrieve the dependencies from public repositories and our company internal repository without synchronizing and uploading the stuff from public repos to the company internal repository.
In your settings.xml, which is usually in HOME_DIR/.m2 you need to add the company repository, maven central is included by default.
In the example below it will look for your artifact in each repo in order, starting with maven central.
<profile>
<id>extras</id>
<repositories>
<repository>
<id>release</id>
<name>libs-release</name>
<url>http://internal.corp:8091/artifactory/libs-release</url>
</repository>
<repository>
<id>snapshot</id>
<name>libs-snapshot</name>
<url>http://internal.corp:8091/artifactory/libs-snapshot-local</url>
</repository>
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://repo.springsource.org/libs-milestone</url>
</repository>
</repositories>
</profile>
I would also say that I set up our corporate repository so that is has a virtual repository to maven central. This means as people use artifacts they will be stored in the company repository. This is normal practice.

How to deploy to archiva using Jenkins and maven

I am trying to deploy to my archiva repo using Jenkins and maven. I am using the "post-build actions" option: "deploy artifacts to maven repository" and I have added the configuration plugin where I added a settings.xml and defined the server details (id, username, password). I also added this file to "build environment" settings where I provided the file as a configuration file.
The problem I am having is the error: not authorized , reasonphrase: unauthorized.
The username and password are for a user with role "repository manager" as the archiva doc instructs. I have set up the pom.xml as well, like the documentation instructs.
I notice that the first error is:
ERROR: failed to retrieve remote metadata someGroupId:someArtifactId:someVersion-SNAPSHOT/maven-metadata.xml
I don't understand where the error comes from and how to resolve it. Please help.
Some suggestions:
1.) Ensure that you have all your servers listed in your Maven settings.xml. This gets me sometimes.
2.) Ensure that your snapshot repo id matches the repo id defined within Archiva.
3.) Ensure that you have access to the snapshots repo, even as admin. Permissions can be revoked.
4.) Ensure that you have the right password.
5.) I've had a restart of Archiva fix this problem before.
6.) The following settings.xml config will allow you to deploy snapshots to a custom snapshots repo that's part of a repository group (i.e. - a snapshots repo for a particular team):
<mirror>
<id><repo_group_id></id>
<mirrorOf>*, !<team_snapshot_repo_id></mirrorOf>
<name>My Team's Maven Repository</name>
<url>http://<HOST>:<PORT>/archiva/repository/<repo_group_id>/</url>
</mirror>
7.) Here's what I add to my pom.xml if I want to deploy the artifact to my snapshots Maven repo:
<distributionManagement>
<repository>
<id>internal</id>
<url>http://HOST:PORT/archiva/repository/internal/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Archiva Managed Snapshot Repository</name>
<url>http://HOST:PORT/archiva/repository/snapshots/</url>
<layout>default</layout>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>snapshots</id>
<url>http://HOST:PORT/archiva/repository/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

Maven: Why is the -SNAPSHOT suffix missing from artifact file name?

My maven artifact is deployed to a Nexus snapshot repository. There, it is stored in the correct directory, but its filenames have the following pattern:
mylibrary-1.0-20130213.125827-2.jar
However, Maven fails to download that snapshot. According to the error log, Maven seems to expect the following file name:
mylibrary-1.0-SNAPSHOT.jar
These are the repository settings in my pom:
<repositories>
<repository>
<id>mycompany-all</id>
<url>https://servername/nexus/content/groups/mycompany/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>mycompany-releases</id>
<url>https://servername/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>mycompany-snapshots</id>
<url>https://servername/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
Note: the nexus group includes both the releases and snapshots repo.
I did not configure these repos in settings.xml - is that the problem? Or what else am I doing wrong?
The pattern you posted (mylibrary-1.0-20130213.125827-2.jar) is a unique snapshot version. Maven 3 forces you to use this type of artifact naming, but in Maven 2 it can be prevented with a statement such as:
<distributionManagement>
...
<snapshotRepository>
...
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
...
</distributionManagement>
To use a specific snapshot in your project, declare it as:
<dependency>
<groupId>com.foo</groupId>
<artifactId>mylibrary</artifactId>
<version>1.0-20130213.125827-2</version>
</dependency>
To use the latest known snapshot, declare it "old-style":
<dependency>
<groupId>com.foo</groupId>
<artifactId>mylibrary</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
You may find the answer to this similar question helpful as well.
I made it work by adding the repositories to the settings.xml like this:
<repositories>
<repository>
<id>mycompany-releases</id>
<url>https://servername/nexus/content/repositories/releases/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>mycompany-snapshots</id>
<url>https://servername/nexus/content/repositories/snapshots/</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
Then, the SNAPSHOT jar files were downloaded just fine. I suspect that when Maven knows it deals with a snapshot repo, it tries both with and without uniqueVersion (see Duncan Jones' answer).
Note that in our case these blocks had to be duplicated as pluginRepositories because we have custom Maven plugins.

How to configure maven project to deploy both snapshot and releases to Nexus?

How to configure maven project to deploy both snapshot and releases to Nexus?
<distributionManagement>
<repository>
<id>InternalReleases</id>
<name>Internal Releases</name>
<url>http://192.168.16.232:8081/nexus/content/repositories/releases/</url>
</repository>
<repository>
<id>InternalSnapshots</id>
<name>Internal Snapshots</name>
<url>http://192.168.16.232:8081/nexus/content/repositories/snapshots/</url>
</repository>
</distributionManagement>
This configuration creates error in Eclipse 3.8 with m2e 1.2
Project build error: Non-parseable POM D:\Workspaces\W\Parent\pom.xml: Duplicated tag: 'repository' (position: START_TAG
seen ...
I want the artifact deployed to the InternalSnapshots repository when the pom's version is suffixed with -SNAPSHOT and deployed to the InternalReleases repository when it is RELEASE. This should happen using the same pom.xml file and executing the same mvn deploy command.
You need to distinguish between the releases and snapshots repository. <distributionManagement> only allows one <repository> and one <snapshotRepository> child.
http://maven.apache.org/pom.html#Distribution_Management
Example of pom.xml configuration
<!-- http://maven.apache.org/pom.html#Distribution_Management -->
<distributionManagement>
<snapshotRepository>
<id>InternalSnapshots</id>
<name>Internal Snapshots</name>
<url>http://192.168.16.232:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>InternalReleases</id>
<name>Internal Releases</name>
<url>http://192.168.16.232:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
Snippets for .m2/settings.xml for default Nexus installation
<server>
<id>thirdparty</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>InternalReleases</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>InternalSnapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
You can do both.
Add the maven-release-plugin 2.5.3
Run the following:
mvn deploy clean:release release:prepare release:perform

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

Resources