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

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

Related

Binding nexus credentials to maven deploy job in jenkins build

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

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!!

Github as Maven Repository

I am attempting to use a private Github repository as a Maven repository, following the instructions on this page: Hosting a Maven Repository in Github
I am able to deploy properly, and I can see the artifacts (jars, poms, etc.) deploying to the Github repo.
What is not working is using this repository to pull dependencies from. In the log, I can see the pom attempting to be downloaded, but it is failing.
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/ with username=randymay, password=***
Downloading: https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/org/hibernate/hibernate/3.2.7.patched/hibernate-3.2.7.patched.pom
[WARNING] The POM for org.hibernate:hibernate:jar:3.2.7.patched is missing, no dependency information available
If I use that URL in a curl request (with Basic Authentication), I am able to download the pom.
curl -v https://randymay:*****#raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/org/hibernate/hibernate/3.2.7.patched/hibernate-3.2.7.patched.pom
Further in the log, it is actually attempting to download the jar:
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/ with username=randymay, password=***
Downloading: https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/org/hibernate/hibernate/3.2.7.patched/hibernate-3.2.7.patched.jar
Could not find artifact org.hibernate:hibernate:jar:3.2.7.patched in github (https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/)
Here is the repositories section of my pom.xml:
<repositories>
<!-- Repositories -->
<repository>
<id>github</id>
<url>https://raw.githubusercontent.com/ORGANIZATION/mvn-repo/master/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
</repositories>
Here is my server entry in settings.xml:
<server>
<id>github</id>
<username>randymay</username>
<password>*****</password>
</server>
Please note that in the referenced article, they used the 'raw.github.com' domain. I tried that at first, and received the same problem. When I used curl to connect to that domain, I received a 'moved permanently' error. I am using 'raw.githubusercontent.com' as that is the url that I am able to successfully use curl with. If this is not correct, please let me know.
Any help would be much appreciated. Thanks in advance.
After much investigating, I found the solution. I needed to provide the Base 64 encoded credentials so Apache Wagon puts them in the header of the request to Github. This is what Github requires (and potentially any Maven repository that is behind a firewall).
Note that the encoded string is the combination of user name and password, separated by a colon.
This is the updated entry in my settings.xml:
<server>
<id>github</id>
<username>randymay</username>
<password>*****</password>
<configuration>
<httpHeaders>
<property>
<name>Authorization</name>
<!-- Base64-encoded "<username>:<password>" -->
<value>Basic *******************</value>
</property>
</httpHeaders>
</configuration>
</server>

Local Nexus Repository Acting Like A Proxy For Some Maven Artifacts

I am using my own version of the Nexus web app repository installed on my local machine. I have Nexus configured with only one repository, the one where I store my snapshots:
http://localhost:8081/nexus/content/repositories/MySnapshots/
Note that after the Nexus installation I removed all the default repositories and added just my own. (Perhaps this was a bad idea?)
When I do a mvn clean install I noticed that some of the 3rd party artifacts are downloading straight from the remote repository. For example, here is one of the output lines from the build:
Downloading: http://repo.maven.apache.org/maven2/com/sun/org/apache/xml/internal/resolver/...
The strange thing is that I see other artifacts are going through my local Nexus to ultimately get to the artifact:
Downloading: http://localhost:8081/nexus/content/repositories/MySnapshots/org/apache/maven/wagon/wagon-provider-api...
Notice how the first part of the download url is my local repository but everything after MySnapshots is from apache.org.
It's almost like my Nexus repository is acting like a proxy to maven.apache.org for some artifact downloads but for others it goes straight to the source.
Can anyone tell me why this is happening?
I would't be bothered so much by this if all my builds succeeded all the time but sometimes, when I am compile large projects, I get build failures due to not being able to find an artifact.
For example, when I try to build another project that depends on eclipse jdt stuff I get the following error:
Downloading: http://localhost:8081/nexus/content/repositories/MySnapshots/eclipse/jdt/core/eclipse.jdt.core
Could not find artifact eclipse.jdt.core:eclipse.jdt.core
I am not sure if this means that my Nexus is not configured properly or if there really is no artifact eclipse.jdt.com. If the downloads were not going through my local Nexus repository I would then investigate the pom/settings.xml files. Instead this makes me wonder if it's due to my Nexus configuration.
If you would like to see my settings.xml for Maven and my pom file for the project I am building when I see this you can view them here:
settings.xml: http://pastebin.com/NvLr5bEA
pom.xml: http://pastebin.com/PJ0P3RaK
If you like to use the local nexus as a proxy as usual than you have to configure 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>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>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
The tricky thing is the mirror thing which reroutes every call to the configured nexus instance.
Things you mentioned like eclipse parts can be problematic, cause only a few artifacts are available via maven central. Furthermore you should leave the defaults like maven central, release repository and the snapshots repository unchanged, cause these are the repository you need.
I don't think its a proxy issue , upto my understanding for the first case when it is downloading from Maven Central Repo , it might be possible that same artifact is not available in your nexus repository , that's why it is going to Maven Central Repo.
In the second case it is available in your nexus so reactor didn't try to download it from Maven Central Repo.

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