Binding nexus credentials to maven deploy job in jenkins build - maven

I have a maven pom project with defined distributionManagement section in pom.xml
<project>
...
<distributionManagement>
<repository>
<id>my-repo</id>
<url>http://nexus.my.local/repository/my-private</url>
</repository>
</distributionManagement>
</project>
in the settings.xml file I have set up the servers section
<servers>
<server>
<id>my-repo</id>
<username>${env.CI_DEPLOY_USERNAME}</username>
<password>${env.CI_DEPLOY_PASSWORD}</password>
</server>
</servers>
in jenkins, I binded the credentials to these variables
But doing mvn clean deploy gives me following error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project XY:
Failed to deploy artifacts:
Could not transfer artifact XY:pom:1.0.0 from/to my-repo (http://nexus.my.local/repository/my-private):
Failed to transfer file: http://nexus.my.local/repository/my-private/XY-1.0.0.pom.
Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
When I echoed the variable ${CI_DEPLOY_USERNAME} in shell before build, it gave me **** output -> I think that is ok.
Where else should I provide the variables? Is my project's settings.xml get used in maven deploy command?

Using environment variables as properties in Maven requires that they're prefixed with env., so it should be:
<servers>
<server>
<id>my-repo</id>
<username>${env.CI_DEPLOY_USERNAME}</username>
<password>${env.CI_DEPLOY_PASSWORD}</password>
</server>
</servers>
Reference:
http://maven.apache.org/pom.html#Properties

Related

Maven fails to publish artifact to AWS S3

I am trying to upload a jar to S3 via gitlab CI pipeline.
On deploy stage a run a job with a script.
- mvn -s ../aws-settings.xml deploy
my aws-settings.xml file looks like this (AWS access keys are set in gitlab CI as environment variables)
<settings>
<servers>
<server>
<id>artifact-s3-repo</id>
<username>${env.AWS_ACCESS_KEY_ID}</username>
<password>${env.AWS_SECRET_ACCESS_KEY}</password>
<configuration>
<region>${env.AWS_DEFAULT_REGION}</region>
</configuration>
</server>
</servers>
</settings>
and in my pom.xml I set distribution managment as
<distributionManagement>
<repository>
<id>artifact-s3-repo</id>
<url>s3://<myartifactbucketname>/</url>
</repository>
</distributionManagement>
my pipeline fails on deploy stage with the following output
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project <my-project-name>: Failed to deploy artifacts/metadata: Cannot access s3://<myartifactbucketname>/ with type default using the available connector factories: BasicRepositoryConnectorFactory: Cannot access s3://<myartifactbucketname>/ using the registered transporter factories: WagonTransporterFactory: java.util.NoSuchElementException
I need some hints to configure the connection to aws properly (without adding additional plugins if possible). Thank you in advance.
I didn't manage to publish artifact to S3 via maven deploy , but I implemented AWS CLI commands in my pipeline script

nexus-staging-maven-plugin returns 401 without information

I am working on getting Anonimatron available as libary in the central Maven Repository. In order to do that I followed the documentation in the sonatype documentation. The nexus staging plugin gives me a 401 error when running mvn deploy and I can't figure out why because it does not tell me what is going on.
I have a working account at oss.sonatype.org, and I have created a user access token there. The access token is stored in the servers section of my maven ~/.m2/settings.xml like so (token removed for reasons):
<servers>
<server>
<id>ossrh</id>
<username>USER_TOKEN</username>
<password>USER_PASSPHRASE</password>
</server>
</servers>
Then, in my maven https://github.com/realrolfje/anonimatron/blob/feature/anonimatron-as-library/pom.xml file I added the nexus staging plugin:
<distributionManagement>
<snapshotRepository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>sonatype-release</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
</distributionManagement>
[...]
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
When I run mvn deploy, I get the error:
[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy (injected-nexus-deploy) on project anonimatron: Failed to deploy artifacts: Could not transfer artifact com.rolfje.anonimatron:anonimatron:jar:javadoc:1.9.3-20180512.204932-1 from/to sonatype-snapshots (https://oss.sonatype.org/content/repositories/snapshots): Failed to transfer file: https://oss.sonatype.org/content/repositories/snapshots/com/rolfje/anonimatron/anonimatron/1.9.3-SNAPSHOT/anonimatron-1.9.3-20180512.204932-1-javadoc.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
To check wether my token is indeed valid, I tried uploading with curl:
curl -u USER_TOKEN:USER_PASSPHRASE \
https://oss.sonatype.org/content/repositories/snapshots/com/rolfje/anonimatron/anonimatron/1.9.3-SNAPSHOT/anonimatron-1.9.3-20180512.093802-1-javadoc.jar \
--request PUT --data target/anonimatron-1.9.3-SNAPSHOT.jar
This succeeds and I can see the uploaded jar appear in the Nexus Repository Manager.
I have executed the same steps as in this question but since I have not uploaded anything yet, version numbers can not collide, although I did do a manual javadoc upload at the end. Since that is a snapshot, and it worked, I think my problem is with the nexus-staging-maven-plugin.
What I am searching for is a way to debug the nexus-staging-maven-plugin so that I know why it is failing. It does not tell me which credentials it is using, wether it loaded those from the settings.xml file, and what exact steps it is taking, even if I run mvn with -e and -X flags.
Executing a deploy with the standard maven-deploy plugin, as #khmarbaise mentioned, did seem to work. I reported this problem to sonatype as part of issues
OSSRH-39766 and OSSRH-39777.
Joel regenerated my permissions and now the nexus release plugin seems to be working. Note that this could also be caused by the maven-deploy which might have "primed" something.
All in all, the nexus release plugin could do with some improvements on the logging, particularly:
What profile is used to execute the steps
What step (exactly) is being executed
If there is a 401, tell which userid was used to execute that step and from which environment that was fetched (command line, settings.xml, etc).
I see some people struggling with the nexus plugin, while the plugin should actually make life easier.
Thanks to SonaType for the fast and helpful responses!
Add the following in your ~/.m2/settings.xml file:
<servers>
<server>
<id>ossrh</id>
<username>{YOUR_JIRA_USERNAME}</username>
<password>{YOUR_JIRA_PASSWORD}</password>
</server>
</servers>
Also, update your pom.xml:
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
Make sure repository id and server id matches.
In this example, the id is "ossrh".
Also, note that the username and password should be same as your jira login. That's it mvn clean deploy will now upload your jar to sonatype repoditory. Happy Deploying!!

why can't i upload artifact to archiva (Unauthorized), but i can download from it with admin user?

I'm working with archiva for about a year now,
I upload my jars manually through archiva GUI,and it's working fine.
Now I want to upload an artifact with maven deploy,the problem is that I get 401-Unauthorized.
bare in mind that:
1.) I can download from this repository with no problem .
2.) I use admin user .
3.) I can upload with this user manually.
this is the log that I get:
[com:apinterface.parent] Downloading: http://xx.xx.xx.xx:9080/archiva/repository/snapshots/com/apinterface.parent/1.0-SNAPSHOT/maven-metadata.xml
[11:43:39][Step 1/3] [INFO] ------------------------------------------------------------------------
[11:43:39][Step 1/3] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project apinterface.parent: Failed to deploy artifacts: Could not transfer artifact com:apinterface.parent:pom:1.0-20140924.084338-1 from/to snapshots (http://xx.xx.xx.xx:9080/archiva/repository/snapshots): Failed to transfer file: http://xx.xx.xx.xx:9080/archiva/repository/snapshots/com/apinterface.parent/1.0-SNAPSHOT/apinterface.parent-1.0-20140924.084338-1.pom. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
This should be because you haven't configured Maven to use the Archiva Username/Password for uploading artifacts during the deploy phase.
I assume you have already configured the distributionManagement section in your pom file.
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>http://xx.xx.xx.xx:9080/archiva/repository/snapshots/</url>
</snapshotRepository>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://xx.xx.xx.xx:9080/archiva/repository/releases</url>
</repository>
</distributionManagement>
You should have servers with matching ids in your Maven Settings file (like in the sample below) to configure the username/password which maven should use while uploading(deploy) the artifacts
<settings>
<servers>
<server>
<id>snapshots</id>
<username>archiva-user</username>
<password>archiva-pwd</password>
</server>
<server>
<id>releases</id>
<username>archiva-user</username>
<password>archiva-pwd</password>
</server>
</servers>
</settings>
Optionally, you can give free access for the download by adding the Guest user access to the repositories at:
Manage -> Users (Tab)
Edit the roles and in "Repository Manager Repository Observer" select the free access repositories

Maven: Release:perform fails, but deploy works.

My pom file looks like this :
<distributionManagement>
<repository>
<id>maven-s3-release-repo</id>
<name>S3 Release Repository</name>
<!-- http://bigpetstore.s3.amazonaws.com/maven/ -->
<url>s3://bigpetstore/maven</url>
</repository>
</distributionManagement>
When I run , "mvn deploy", I get my project published to the correct repository.
However, if I run "release:prepare" followed by "release:perform", I get the following error:
[INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-
plugin:2.7:deploy (default-deploy) on project BigPetStorePro: Deployment failed:
repository element was not specified in the POM inside distributionManagement element or
in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
So, my question is: Why is it that "mvn deploy" is able to easily deploy my contents, however, mvn release:prepare fails to see the contents of the distributionManagement?
Clearly, the tag is present.

Maven - repositories tag preventing deployment

I have a project POM which specifies a repositories tag which points to a sandbox location.
<repositories>
<repository>
<id>mysandbox</id>
<name>Sandbox</name>
<url>http://myTestingSite.com/repositories/sandbox/</url>
</repository>
</repositories>
This works fine in Eclipse and resolves all dependencies however when i attempt to deploy i get the following exception.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy (default-depl
oy) on project myweb-web: Deployment failed: repository element was not specified in the POM insid
e distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help
1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plug
ins:maven-deploy-plugin:2.5:deploy (default-deploy) on project myweb-web: Deployment failed: repos
itory element was not specified in the POM inside distributionManagement element or in -DaltDeployme
ntRepository=id::layout::url parameter
Wrapping this in a Distributionmanagement element doesn't make any sense because I'm using the repository for dependency resolution and not deployment.
The error says you either don't have a distributionManagement element or the element isn't correct.
So this has nothing to do with the content of <repositories>. Just create a correct distributionManagement element and it will work.
Note that the broken element might be in the parent POM. Run mvn help:effective-pom to see the complete POM as Maven sees it.
The repository that you have defined originally is only taken into consideration to download dependencies, not to upload them (as you mentioned).
What you need to add is a repository inside distribution management
<distributionManagement>
<repository>
<id>id</id>
<name>name</name>
<url>nexus_url</url>
</repository>
</distributionManagement>
And if your nexus is secured, you'll also need to define a server section for the password.
<servers>
<server>
<id>id</id>
<username>username</username>
<password>password</password>
</server>
</servers>

Resources