mave 3.2 not able to access local nexus instance return 502 code - maven

While accessing local nexus repository which is in local host the return code is 502.
pom.xml looks something like this
<repositories>
<repository>
<id>nexus</id>
<name>nexusPublic</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
</repositories>

To OP and anyone who may have got here through google (just like me):
I had the same issue, the project I entered used some artifact from the repository on our server (via Archiva). I was able to reach the jar from browser, but Maven was not able to find it.
The problem was that my company uses proxy and it was impossible to connect to company's server through it's own proxy. I added <nonProxyHosts>servername.dev.company.com</nonProxyHosts> to my Maven configuration (.m2/settings.xml).
According to this answer, in your case it would be <nonProxyHosts>*localhost*</nonProxyHosts>.

Question is already answered but let me write full xml configuration file:
.m2/settings.xml
<settings>
<proxies>
<proxy>
<id>proxy_http</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy host</host>
<port>porxy port</port>
<nonProxyHosts>local.net|host_1|host_2|host_3</nonProxyHosts>
</proxy>
</proxies>
</settings>

Related

401 unauthorized from maven when publishing to gitlab artifactory

I am facing an issue when trying to publish an artifact in private gitlab repository. I am using maven and I authenticated using personal access token. When I run mvn deploy -s ~/.m2/settings.xml I get the following error Failed to deploy artifacts: Could not transfer artifact ... 401 Unauthorized
My settings.xml file looks like this.
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>personal-token</name>
<value>mytoken</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
I've also tried changing it to
<servers>
<server>
<id>gitlab-maven</id>
<username>username</username>
<password>pass</password>
</server>
</servers>
but that didn't help. And here is my pom publishing part
<repositories>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.mycompany.com/api/v4/projects/92/packages/maven</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>https://gitlab.mycompany.com/api/v4/projects/92/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>https://gitlab.mycompany.com/api/v4/projects/92/packages/maven</url>
</snapshotRepository>
</distributionManagement>
Is there anything that I'm missing? Thank you in advance.
Fixed this by changing property in the settings file to Private-Token (I was using actual name of the token previously)
I had the same problem but for a different reason.
my idea (intellij with built in maven plugin) was using different settings file (C:\Users\YOUR_USER_NAME\.m2\settings.xml) from the one i configured the server (local maven installation on a different path).
to fix this go to: File -> Settings (on windows Ctrl + Alt + s) -> Build, Execution, Deployment -> Build Tools -> Maven -> User settings file.
mark the check box for Override and direct to the correct path.
hopefully it will save someone's time...
In case this might help someone:
Once you reach the "mvn deploy" step in the gitlab docs and find yourself struggling with 401 Unauthorized error, running the deploy command with -s or --settings flag like so:
mvn deploy -s settings.xml (the argument to the -s flag is the path to your project settings.xml file)
Solves the issue (at least for me) by using the user specified settings file instead of the default one stored in .m2 folder.
(I found solution in this video)

How to let Maven download from Nexus but bypass it for external jars

I understand Nexus is supposed to download the external dependencies, but the company Nexus server (over which I have no control) cannot access the internet or download external jars.
As a workaround I have 2x settings.xml files which I manually switch. One settings.xml use the corporate Nexus server for internal jars and the other uses the company proxy to download external dependencies from the default Maven repositories. Once all the dependencies are in my local Maven repo compilings works.
settings.xml for Nexus (excluding proxies)
<settings>
<mirrors>
<mirror>
<id>some-repo-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://brokennexus/repository/maven-public/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>some-repo-mirror</id>
<username>me</username>
<password>secret</password>
</server>
</servers>
</settings>
settings.xml for internet (excluding mirrors and servers)
<settings>
<proxies>
<proxy>
<id>company-proxy-http</id>
<active>true</active>
<protocol>http</protocol>
<username>me</username>
<password>secret</password>
<host>proxy.server</host>
<port>8080</port>
</proxy>
<proxy>
<id>company-proxy-https</id>
<active>true</active>
<protocol>https</protocol>
<username>me</username>
<password>secret</password>
<host>proxy.server</host>
<port>8080</port>
</proxy>
</proxies>
</settings>
Both files works on their own, but I cannot figure out how to combine them into one.
Is there a way to configure maven in such a way that if the company Nexus cannot resolve a dependency it would fall back and attempt to download from an external repo through the company's internet proxy or the other way around so that I do not have to switch between the two settings.xml files?
You can configure the proxy in your "normal" settings.xml and exclude your Nexus as nonProxyHost:
https://maven.apache.org/guides/mini/guide-proxies.html
Furthermore, you need to change the mirror configuration <mirrorOf>*</mirrorOf> to something like <mirrorOf>*,!central</mirrorOf> to be able to connect MavenCentral (and other repositories directly).
Of course, the preferred solution would be to talk to the admin if they can add the external repositories to the company Nexus.

Repository Authentication with Basic Auth only works when embedded in URL

I have a private maven repository. Publishing on this repository with authentication works well. But when I try use the repository to resolve dependencies, the authentication credentials defined in the settings.xml are not applied. Only way to make it work is to provide the credentials in the repository URL defined in the pom.xml
Is this a bug or did I miss something during setup?
Snippet from the pom.xml
<repositories>
<repository>
<id>myServer</id>
<name>My Servers Name</name>
<url>https://someHost/repository/maven-public/</url>
</repository>
</repositories>
Snippet from the settings.xml
<servers>
<server>
<id>myServer</id>
<username>myUser</username>
<password>myPass</password>
</server>
</servers>
I use Apache Maven 3.0.5 (Red Hat 3.0.5-17) and on the server's side I see that no credentials are applied, so a 401 is responded.
The above setup does work if I remove the server-setup from settings.xml and add the credentials myUser:myPass to the URL defined in the pom.xml.
Finally it appeared that I had a typo in the auth-credentials so, all works as expected.

maven repository proxy confusion

I am using maven 3.0 ( with nexus setup ) for building my projects and am getting build failures :
Caused by: org.sonatype.aether.transfer.ArtifactNotFoundException: Could not find artifact directory:apacheds-core:jar:${apacheds_version} in central (http://localhost:8081/nexus/content/repositories/central)
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:945)
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:940)
My basic confusion is : When maven sees a dependency in the pom.xml , how does it go about looking for artifacts in remote repositories ?
My current understanding is :
It will first look in the local repo ( .m2/repository ).
If it does not find there , then it will try to search the repository specified in settings.xml under repository tag.
Question : Does it try all the repositories mentioned . or Just the first one ? Below I have mentioned 5 repos : does maven search all these one by one or just the first one ?
<repositories>
<repository>
<id>central</id>
<url>http://localhost:8081/nexus/content/repositories/central</url>
</repository>
<repository>
<id>remote</id>
<url>http://localhost:8081/nexus/content/repositories/remote-proxy-nexus-central</url>
</repository>
<repository>
<id>thirdParty</id>
<url>http://localhost:8081/nexus/content/repositories/thirdparty</url>
</repository>
<repository>
<id>codehaus</id>
<url>http://localhost:8081/nexus/content/repositories/codehaus-snapshots</url>
</repository>
<repository>
<id>public</id>
<url>http://localhost:8081/nexus/content/groups/public</url>
</repository>
</repositories>
My last confusion is about proxies section in the settings.xml. What are these locations :
<proxy>
<id>remote-proxy-nexus-central</id>
<active>true</active>
<protocol>http</protocol>
<host>repo1.maven.org/maven2</host>
<port>8080</port>
</proxy>
I can tell you we use a local Nexus and have all our users have the following in their settings.xml:
<mirror>
<id>our-mirror</id>
<name>Org Public Mirror</name>
<url>http://host/nexus/content/groups/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
This causes any call by maven to go to Nexus to get a dependency. You are right about maven first looking in local .m2.
Nexus proxies many repositories and has a union of them all (for the maven processes calling it).
This means that a developer's local maven knows only of a single repository: Nexus. Nexus will serve all the needed dependencies id they are in one of its proxied/hosted repositories.
As for Proxy, we have an organization proxy, but the Nexus is in the org (it has the proxy configured to allow access to the outer world), so maven does not need this specific configuration.
I hope this gives you some information to get started.
I strongly urge you to look into Nexus/Maven related configurations at: http://www.sonatype.org/
The correct setup for using Maven with Nexus is documented in the book Repository Management with Nexus. The sample settings.xml is here. Read the description and note that you need to add the overrides for the central repository to enable snapshots.
Once you have done that you do NOT configured a proxy in your settings.xml since it will be available in your local network without a proxy (typically). Instead you configure the proxy settings in Nexus so that it in turn can get out to the repositories like Central that you are proxying. The global proxy configuration is documented here and if required you can also configure specifics per proxy repository e.g. if you need a username/password for a repository you are proxying because it is private..

Maven Could not calculate build plan:Failure to transfer org.apache.maven.plugins

When ever I try to update Maven dependencies I get an error like - Could not calculate build plan:null. At first I used to get the error like
Could not calculate build plan: Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from/to central (http://repo1.maven.org/maven2): ConnectException
But, when I have made changes to settings.xml file and added repository and plugin repository in settings.xml like
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
</repository>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
<snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots>
</pluginRepository>
The previous error was removed, but now I get the following error: Could not calculate build plan:null
how to get rid of this error!! and also how to get rid of this warning
Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result. I tried to quick fix it, but whenever I reopen the project it again appears back. Any help would be appreciated.
I was able to solve this issue by enabling the proxy in settings.xml file located at config. At first tried different solutions by adding repository and pluginRepository in settings.xml file as mentioned in above question. But the actual problem was with the proxy configuration. I have tried the solution given by #akb at maven in 5 min not working. And the solution that worked out for me was Solution #1 as my organization's proxy setup was not auto-configured. Just included following data in settings.xml and do mvn clean, and update dependencies it worked.
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<host>webproxy</host>
<port>8080</port>
<username>proxyuser</username>
<password>proxypass</password>
</proxy>
</proxies>

Resources