I'm trying to deploy artifacts to a protected by Basic Auth repository.
I specify
<distributionManagement>
<repository>
<id>some.repo</id>
<name>Some Repository</name>
<url>https://foo.bar</url>
</repository>
</distributionManagement>
in pom.xml
And
<servers>
<server>
<id>some.repo</id>
<username>user</username>
<password>{encoded password}</password>
</server>
</servers>
in settings.xml
But when I run mvn deploy it fails with the error: Not authorized , ReasonPhrase:. -> [Help 1]
If I use
<server>
<id>some.repo</id>
<configuration>
<httpHeaders>
<property>
<name>Authorization</name>
<value>Basic (hash)</value>
</property>
</httpHeaders>
</configuration>
</server>
instead - it works.
After inspection with Wireshark I discovered that in the first scenario (with login/password) Maven doesn't add Authorization header to the HTTP request.
Is it correct behavior? Am I missing something in the configuration?
Well, in this case, it was not Maven, but the server-side problem: server was not sending Connection: keep-alive header. With this header, the authorization flow works.
maven will only send username/password on requests where server responds with a
status code:401
header: WWW-Authenticate: Basic.
Related
I'm getting this error when I call any Maven command:
[ERROR] Plugin org.apache.maven.plugins:maven-enforcer-plugin:3.1.0 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-enforcer-plugin:jar:3.1.0: Could not transfer artifact org.apache.maven.plugins:maven-enforcer-plugin:pom:3.1.0 from/to central (https://repo.maven.apache.org/maven2): transfer failed for https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-enforcer-plugin/3.1.0/maven-enforcer-plugin-3.1.0.pom: Cannot assign requested address: connect -> [Help 1]
Things that I've already checked:
1. Windows Firewall is not blocking outbound/inbound requests
2. Not behind any proxy
3. Connected to the internet
4. Settings file credentials and repo url OK <settings>
<servers>
<server>
<id>maven-snapshots</id>
<username>dummy-username</username>
<password>dummy-pwd</password>
</server>
<server>
<id>maven-releases</id>
<username>dummy-username</username>
<password>dummy-pwd</password>
</server>
</servers>
<activeProfiles>
<activeProfile>local</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>local</id>
<repositories>
<repository>
<id>maven-releases</id>
<url>https://dev-repo.tech5-sa.com/repository/maven-releases/</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>
5. Can access repo using browser
6. Cleaned caches
This was also happening with Gradle - this: Cannot assign requested address: connect - , before I switch the project to be under Maven control, so I'm guessing, some configuration in the machine (Machine is new).
Java version: 11.0.16
Apache Maven 3.8.7
IntelliJ IDEA 2022.3.1
Build #IC-223.8214.52, built on December 20, 202
Can anyone give me some hints ?
Thank You for your time.
Leo
I am trying to publish third-party private package directly to my Azure Artifacts feed with mvn deploy:deploy-file command like this:
mvn deploy:deploy-file -Dpackaging="jar" -DrepositoryId="PXXXXX-inccoming" -Durl="https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1" -DgroupId="pl.group.id" -DartifactId="artifact" -Dversion="0.0.2" -Dfile="C:\path\pl\group\id\0.0.2\artifact-0.0.2.jar"
But I am getting strange error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project CAST_2015: Failed to deploy artifacts: Could not transfer artifact pl.group.id:artifact:jar:0.0.2 from/to PXXXXX-inccoming (https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1): Transfer failed for https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1/pl/group/id/artifact/0.0.2/artifact-0.0.2.jar 401 Unauthorized ProxyInfo{host='proxy-host', userName='null', port=8080, type='http', nonProxyHosts='null'}
The provided user has full access for packaging in Azure.
Both proxy and server settings are actually in my setting.xml file.
What am I doing wrong?
Azure Artifacts deploy packages
When we receiving a 401 it is because maven is sending the wrong login credentials, or no credentials at all.
To resolve this issue, please add the following configuration to the <servers>...</servers> configuration node in the maven setting configuration file:
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
Note:
The id configured in setting.xml must be the same as the id
configured in pom.xml.
If you try to publish something to a releases repository and that
version already exists in the repository
If it not work for you, please check below thread for some more details:
Why am I getting a "401 Unauthorized" error in Maven?
I have a Maven project and want to build it using mvn clean deploy so that the built artifact is deployed into a Nexus repository.
The access data (username and password) for that repository are stored in Jenkins credentials.
I want to call mvn deploy in Jenkins so that the credentials for that Nexus repository are read from Jenkins (not hardcoded in settings.xml).
How can I do it, if I cannot access settings.xml on the Jenkins server?
Update 1:
I created an entry in "Config File Management" (JENKINS_URL/configfiles/index) with following data:
Type: Maven settings.xml
Replace All: Yes
Server ID: myServer
Credentials: Credentials for the Nexus repository
Content:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servers>
<server>
<id>myServer</id>
<username>foo</username>
<password>bar</password>
</server>
</servers>
</settings>
myServer is also used in the pom.xml of the artifact I want to build:
<distributionManagement>
<repository>
<id>myServer</id>
<url>http://nexus.mycompany.com</url>
</repository>
</distributionManagement>
In the configuration of the job, I include those settings as shown below. Nexus settings.xml is the configuration from "Config File Management".
But it does not work -- I get "Forbidden" error when the build attempts to deploy artifacts to Nexus.
Update 2: When I run mvn -X deploy locally with the same credentials as in Jenkins (stored in my local settings.xml), I see following output:
[DEBUG] Failed to decrypt password for server XXX release repository: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
at org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher.decrypt(DefaultSecDispatcher.java:121)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:107)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:63)
at org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory.newRepositorySession(DefaultRepositorySystemSessionFactory.java:165)
However, the password in settings.xml is not encrypted at all:
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>NEXUS_REPOSITORY_SNAPSHOTS</id>
<username>user</username>
<password>password</password>
</server>
</servers>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
I found a report about similar error here.
You can use the Jenkins config-file-provider plugin ( link: https://plugins.jenkins.io/config-file-provider/ ) to create one or several Maven settings.xml files.
NOTE: Really this helps also if you need differents settings.xml for
different projects.
Then on the maven deploy step of your Jenkins project you can select to point to one of the settings.xml files defined ( instead of pointing to the general Jenkins server /.m2/settings.xml )
I have used this article (https://gist.github.com/fernandezpablo85/03cf8b0cd2e7d8527063) for creating a custom maven repo, and it works. But now I have a problem with a private repo. How can I specify credentials for the private repo?
You can set in .m2/settings.xml file
Like This:
<settings>
<servers>
<server>
<id>private-repo</id>
<username>xyz</username>
<password>${pass}</password>
</server>
</servers>
</settings>
And in pom.xml:
<project>
...
<repositories>
<repository>
<id>private-repo</id>
<url>${private-repo.url}</url>
</repository>
</repositories>
...
</project>
A settings.xml file is usually found in a couple of places:
Global settings in Mavens home directory: ${maven.home}/conf/settings.xml
User settings in the user’s home: ${user.home}/.m2/settings.xml
If both files exist, their contents are merged. Configurations from the user settings take precedence.
If your settings doesn't apply you can try to override the default settings.xml file location by adding --settings flag to your mvn ... command.
sources:
https://www.baeldung.com/maven-settings-xml#settings-level
https://www.baeldung.com/maven-settings-xml#3-override-the-default-location
I have the following config in my /users//settings.xml.
Under PluginsGroup:
<pluginGroup>org.apache.tomcat.maven</pluginGroup>
Under servers:
<server>
<id>localhost</id>
<username>tom</username>
<password>cat</password>
</server>
Under profiles:
<plugin>
<configuration>
<server>localhost</server>
<url>http://localhost:8080/manager/text</url>
<path>/</path>
</configuration>
</plugin>
I ran the maven command with -X options, and I see the following:
[DEBUG] Reading global settings from C:\Tools\apache-maven-3.1.1\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\<user>\.m2\settings.xml
I have the manager-script role in tomcat users xml file.
When I manually verify the access through browser, it works fine. But when I run mvn tomcat7:deploy I get the following:
Receiving response: HTTP/1.1 401 Unauthorized
User name and password are correct. I verified it manually. What else could be wrong?
Googling did not help much.
Edit: More details from Log.
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Cache-Control: private
Expires: Wed, 31 Dec 1969 18:00:00 CST
WWW-Authenticate: Basic realm="Tomcat Manager Application"
Set-Cookie: JSESSIONID=7320C7326C51E8F7FF9F4D23A39E6971; Path=/manager/; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Thu, 24 Apr 2014 02:56:27 GMT
Cookie accepted: "[version: 0][name: JSESSIONID][value: 7320C7326C51E8F7FF9F4D23A39E6971]
[domain: localhost][path: /manager/][expiry: null]".
Connection can be kept alive indefinitely
Authentication required
localhost:8080 requested authentication
Authorization challenge processed
Authentication failed
Clearing cached auth scheme for http://localhost:8080
Edit:
Well, this is what I found out. When I pass username and password from mvn command, its working fine. But its not picking up from settings.xml.
Does it ring any bell? I tried moving the config stuff to MAVEN_HOME/config/settings.xml as well. (Deleted the user settings.xml). But still the same.
Any settings cache?
Edit:
I turned on the debugger with -X. Even though I deleted /user/.m2/settings.xml, it says its reading that file.
[DEBUG] Reading global settings from C:\Tools\apache-maven-3.1.1\bin\..\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\<user>\.m2\settings.xml
How to flush?
The pluging configuration should be like that:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<server>text</server>
<url>http://localhost:8080/manager/text</url>
</configuration>
</plugin>
This should work
I am guessing that the application you want to deploy is named text. If it is not replace text with your web application name.
Also I would try removing the last <path>/</path> element to see if this helps or not...