how to deploy my artifact on to my nexus? - maven

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

Related

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

How to see what Maven is sending to a server during deploy?

I'm trying to use Github's new Actions CI server to deploy packages to Github's new packages feature. It's not going well.
I think it's all set up correctly, but I get this error:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
(default-deploy) on project myproject: Failed to deploy artifacts: Could not
find artifact com.mycompany:myproject:pom:1.5 in github
(https://maven.pkg.github.com/mycompany/mycompany_repository) -> [Help 1]
This happens after it appears to upload that same pom successfully:
Uploading to github: https://maven.pkg.github.com/mycompany/mycompany_repository
/com/mycompany/myproject/1.5/myproject-1.5.pom
Progress (1): myproject-1.5.pom (4.1/6.1 kB)
Progress (1): myproject-1.5.pom (6.1 kB)
So, it looks to me like it is successfully uploading the pom, but then it fails to download the same pom a few seconds later.
I'm running the deploy with debug switches on: mvn -X -e deploy, but I can't see the exact http commands that Maven is sending to the server.
How do I debug this? Is there some Maven/Aether transport or something that will log what is going on under the covers?
In case anyone else lands here looking for a solution to OPs issue publishing to github, I had a similar issue and found that the URLs needed in settings.xml and pom.xml are inconsistent. In your settings.xml, the repo URL needs to be of the form https://maven.pkg.github.com/myuser/com/mycompany/mypackage, whereas in your project's pom file, it needs to be of the form https://maven.pkg.github.com/myuser/mypackage. So, for example, your settings.xml file in ~/.m2 would look something like this:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/myuser/com/mycompany/mypackage</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>myuser</username>
<password>mypersonalaccesstoken</password>
</server>
</servers>
</settings>
Whereas the pom.xml file in the root of your project would need to look like this:
<project>
...
<groupId>org.mycompany</groupId>
<artifactId>mypackage</artifactId>
<version>1.0.0</version>
...
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/myuser/mypackage</url>
</repository>
</distributionManagement>
...
</project>
Other than this minor (but crucial) detail, my steps were the same as those outlined here. This allowed me to publish my Maven package to github package registry.
You can enable debug logging in the workflows.
Just add the secret:
ACTIONS_RUNNER_DEBUG
And set to true
See a similar answer here
I just spend 3 hours debugging why the guide on the page did not work for me. If you are following the guide posted here 1.
OWNER is your github username, and REPOSITORY is - you guessed it, the repo name.
Just remember to use lowercase in both OWNER and REPOSITORY.
When generating the personal access token, make sure the scopes for the token are the repo:* scopes as well as the more obvious write:packages and read:packages scopes (do not disable the repo scopes)
Otherwise it does just that
The following solution works for me:
Create a repository for packages e.g. maven-packages
Add <server></server> settings under <servers> in settings.xml: (do this per id used below)
<server>
<id>github</id>
<username>YOUR GITHUB USERNAME</username>
<password>A GITHUB TOKEN YOU CREATE FOR PUBLISHING PACKAGES</password>
</server>
Do NOT add <activeProfiles>, <profile> or <repositories> to settings.xml (only add <server> elements) as this is redundant for publishing and I am adding them to consuming projects' maven.xml so no need for duplication.
Add repository/ies to distributionManagement in pom.xml as follows:
<distributionManagement>
<snapshotRepository>
<id>github-snapshot</id>
<name>GitHub snapshot</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
<repository>
<id>github-release</id>
<name>GitHub release</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
<uniqueVersion>false</uniqueVersion>
</repository>
</distributionManagement>
Where OWNER is the GitHub account your project is / projects are under and maven-packages is the repositories you want to publish you project(s) to.
This enables using a dedicated repository for listing packages instead of publishing each project's package to a different (its own) repository, making consumption of multiple packages from your GitHub account easier, as you only need to configure a single repository for these packages:
<repositories>
<repository>
<id>github</id>
<name>GitHub</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
</repository>
</repositories>
Note: in the <servers> section of your settings.xml define a <server> per id used in repositories and distributionManagement e.g. github-snapshot, github-release, github in the above examples.

Release a snapshot to nexus using maven 3.0.5

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/'
}
}

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>

Resources