Change maven default repository - maven

in my company we have a proxy that inhibits downloading jars so evrey time i download a jar using maven i discover that it was corrupted.but i can download jars from https servers.
My problem is that evrey time i use maven it always downloading from this repo http://repo1.maven.org/maven2/org/apache/maven
i don't find it in setting.xml so i can change it with https://search.maven.org/
How can i change it
Thanks in advance

I don't think pointing to https://search.maven.org is going to solve your issue. That is just a human consumable (yes along with bookmarkable and searchable-by-REST) search location.
That said, the definition of central is in the Maven super POM that is part of the Maven installation. You can mirror this repository if you wish to point your Maven installation to a different central repository.

The most important step you should go is to use a Repository Manager and use this instead using direct connection to internet. There different solutions available like Nexus, Artifactory, Archiva. The problem your are describing are typical problems within commerial environments and of course the solution is to use a repository manager.

You can solve the jar download issue by defining proxy settings in settings.xml. My company also used a proxy to communicate with outside world.Having said that, I also learnt that there is no need to define specific remote repository in settings.xml bcos maven by default searches in its own repository which mostly has all the opensource jars. Try below configuration in your setting.xml and issue will be solved.
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
</proxy>

Related

How to solve :Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 [duplicate]

org.apache.maven.plugin.PluginResolutionException: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:129)
at org.eclipse.m2e.core.internal.project.registry.EclipsePluginDependenciesResolver.resolve(EclipsePluginDependenciesResolver.java:48)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getPluginDescriptor(DefaultMavenPluginManager.java:142)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getMojoDescriptor(DefaultMavenPluginManager.java:261)
at org.apache.maven.plugin.DefaultBuildPluginManager.getMojoDescriptor(DefaultBuildPluginManager.java:185)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.setupMojoExecution(DefaultLifecycleExecutionPlanCalculator.java:152)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.setupMojoExecution(MavenImpl.java:386)
at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.setupMojoExecution(ProjectRegistryManager.java:865)
at org.eclipse.m2e.core.internal.project.registry.MavenProjectFacade.getMojoExecution(MavenProjectFacade.java:355)
at org.eclipse.m2e.core.project.configurator.AbstractCustomizableLifecycleMapping.getBuildParticipants(AbstractCustomizableLifecycleMapping.java:66)
at org.eclipse.m2e.core.project.configurator.AbstractLifecycleMapping.configure(AbstractLifecycleMapping.java:87)
at org.eclipse.m2e.core.internal.project.ProjectConfigurationManager.updateProjectConfiguration(ProjectConfigurationManager.java:414)
at org.eclipse.m2e.core.internal.project.ProjectConfigurationManager.updateProjectConfiguration(ProjectConfigurationManager.java:351)
at org.eclipse.m2e.core.ui.internal.UpdateMavenProjectJob.runInWorkspace(UpdateMavenProjectJob.java:74)
at org.eclipse.core.internal.resources.InternalWorkspaceJob.run(InternalWorkspaceJob.java:38)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: org.sonatype.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:296)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:186)
at org.sonatype.aether.impl.internal.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:279)
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:115)
I have read where many people were able to solve this by:
Deleting the folder from the local repository and letting it re-download it (this did not work)
By configuring eclipse to target your maven installation instead of the embedded one as described here (this did not work)
Could not calculate build plan :artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 is not available in the local repository
I am new to Maven so please excuse any of my ignorance.
This project is working on another machine, and just pulled it down from the repository on this one, with the same version of eclipse and m2e plugin installed. I have been fooling with this for over 10 hours now and it is driving me nuts (Maven has been nothing but headaches for me so far...)
EDITS
After looking closer I did notice that it is not downloading the .jar files into the local repository... I am not sure if that is something obvious...
I am not given the option to add Maven Dependencies to the build path.
I had the exact same problem.
[ERROR] Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.5: Failure to find org.apache.maven.plugins:maven-resources-plugin:pom:2.5 in http://repo.maven.apache.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 -> [Help 1]
...
Had maven 3.0.5, eclipse Kepler with JBoss Dev Studio 7 installed. Computer sitting on internal network with proxy to the internet. Here's what I did.
0. Check the maven repositiory server is up
1. Check Proxy is set up and working
First I thought it was a proxy problem, I made sure that maven settings.xml contained the proxy settings (settings.xml can exist in two places one in MAVEN_HOME. The other in %userprofile%.m2\ with the later having higher precedence):
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>optional-proxyuser</username>
<password>optional-proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
and checked that the proxy is working by trying to telnet to it:
telnet [proxy] [port number]
2. Check not Eclipse Issue
ran 'mvn compile' at command line level outside of eclipse - same issue.
If 'mvn compile' worked. But it doesn't work using the maven plugin in eclipse, see Maven plugin not using eclipse's proxy settings
3. Check not Cache Issue
Deleted all contents in my local maven repository. (Default location: ~/.m2/repository) And then reran maven - same issue came up.
4. What worked for me
Automatically download & install missing plugin:
By declaring the missing plugin in the POM file build section for pluginManagement Maven will automatically retrieve the required plugin. In the POM file, add this code for the version of the plugin you require:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Manually install missing plugin:
I went to http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-resources-plugin/2.5 and downloaded maven-resources-plugin-2.5.jar and maven-resources-plugin-2.5.pom . Copied it directly into the maven repository into the correct folder ( ~/.m2/repository/org/apache/maven/plugins/maven-resources-plugin/2.5) and reran 'mvn compile'. This solved the problem.
Edit1
Following this I had another two problem with 'mvn install':
The POM for org.apache.maven.plugins:maven-surefire-plugin:jar:2.10 is missing, no dependency information available
The POM for org.apache.maven.plugins:maven-install-plugin:jar:2.3.1 is missing, no dependency information available
I approached this problem the same way as above, downloading from http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin/2.10 and http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-install-plugin/2.3.1
I had the exact same problem and since I read somewhere that the error was caused by a cached file, I fixed it by deleting all the files under the .m2 repository folder.
The next time I built the project I had to download all the dependencies again but it was worth it - 0 errors!!
Couple of things to try:
Doublecheck the location of the local artifact repo configured in your settings.xml file (at the following location {your home folder}/.m2/settings.xml). Are you sure the local repo is where you think it is? (Yes, a mistake I've made in the past...)
Remove entire contents of artifact repo on the new build machine (or at least anything related to Maven). You mentioned doing some artifact repo cleanup but I'm not sure what directory(ies) you removed. I've run into weird issues like these when a jar was corrupted.
Make sure you have enough disk space/quota for the local artifact repo. I have run into weird issues when I didn't have a large enough quota to hold all the artifacts, likely caused by partially downloaded jar files.
Try running with plain Maven on the command line; take Eclipse and m2e out of the equation. mvn -U dependency:resolve should do it. The -U forces Maven to download no matter what your repository update policies are. Add -X to get detailed debug logging.
Copy settings.xml from MAVEN_HOME\conf\ to USER_HOME.m2. Add proxies (if needed) in case you are behind a proxy server.
Follow easy steps to resolved the below issue:
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact description for org.apache.maven.plugins:maven-ar-plugin:ar:2.4 in eclipse
Solution:
Step1:
Step2:
Issue solved ☺
i faced the same issue while using eclipse kepler and maven version 3.2,
while building the project, it showed me the same error in eclipse
there are two versions (2.5 and 2.6) of plugin under
.m2/repository/org/apache/maven/plugins/maven-resources-plugin/
i removed 2.5 version then it worked for me
My problem was the location of the config file.
In eclipse settings (Windows->preferences->maven->User Settings) the default config file for maven points to C:\users\*yourUser*\.m2\settings.xml. If you unzip maven and install it in a folder of your choice the file will be inside *yourMavenInstallDir*/conf/, thus probably not where eclipse thinks (mine was not). If this is the case maven won't load correctly. You just need to set the "User Settings" path to point to the right file.
It appears that there can be a lot of different causes for this issue. I experienced it after installing a new version of Eclipse (Luna). Command-line maven worked fine, but Eclipse had build issues.
I use a Certificate Authority in my JRE. This is important because this provides my authentication when downloading Maven resources. Even though my project was pointing to the appropriate JRE inside of Eclipse - Eclipse was running using a different JRE (this is apparent looking at the Java process properties in Windows task manager). My solution was to add the following in my eclipse.ini
and explicitly define the JRE I want to use.
-vm
C:\Program Files\Java\jdk1.7.0_51\bin\javaw.exe
You should check Force Update Snapshots/Releases when updating project with maven.It worked for me :-).
Try to delete all dirs in /usr/share/maven-repo - of course then maven will die so you must re-install and try again. In my case re-install from maven ver.3. to maven2 with deleting all repositories helped.
I tried by deleting all from .m2 but that didn't help.
Some files where missing at your local repository. Usually under ${user.home}/.m2/repository/
Neets answer solves the problem. However if you dont want do download all the dependencies to your local repository again you could add the missing dependency to a project of yours and compile it.
Use the maven repository website to find the dependency.
In your case http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-resources-plugin/2.5 was missing.
Copy the listed XML to the pom.xml file of your project. In this case
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
</dependency>
Run mvn compile in the root folder of the pom.xml. Maven will download all missing dependencies. After the download you can remove the added dependency.
Now you should be able to import the maven project or update the project without the error.
What I found out is that while m2e is looking for v2.5 by default, my local repo has 2.6 and no 2.5.
Without going into investigation of how this came about
simply adding the dependency to pom solved the problem
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</dependency>
This can be removed after running a build once
his issue is happening due to change of protocol from http to https for central repository. please refer following link for more details. https://support.sonatype.com/hc/en-us/articles/360041287334-Central-501-HTTPS-Required
In order to fix the problem, copy following into your pom.ml file. This will set the repository url to use https.
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
A more subtle reason for this could be a Settings.xml file which has a space in the first line before the doctype
Hopefully I'm not late for the party.
Encountered this using Eclipse Kepler and Maven 3.1.
The solution is to use a JDK and not a JRE for your Eclipse project. Make sure to try maven clean and test from eclipse just to download missing jars.
I had the same problem but with an other cause. The solution was to deactivate Avira Browser Protection (in german Browser-Schutz). I took the solusion from m2e cannot transfer metadata from nexus, but maven command line can. It can be activated again ones maven has the needed plugin.
Most people will tell you to check your proxy settings or delete and re-add artifacts, but I will stay away from that and give another suggestion in case that doesn't turn out to be your problem. It could be your mirror settings.
If you use maven at the office then there's a good chance maven is configured to look for your company's internal maven repository. If you're doing some work from home and you are not connected to the network this could be the problem. An obvious solution might be VPN to the office to get visibility to this repo. Another way around this is to add another mirror site to your /User/.m2/settings.xml file so if it fails to find it on your office network it will try public repo.
<mirror>
<id>Central</id>
<url>http://repo1.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
<!-- United States, St. Louis-->
</mirror>
For other maven repositories take a look here: http://docs.codehaus.org/display/MAVENUSER/Mirrors+Repositories
In my case I'm using an external maven installation with m2e. I've added my proxy settings to the external maven installation's settings.xml file. These settings haven't been used by m2e even after I've set the external maven installation as default maven installation.
To solve the problem I've configured the global maven settings file within eclipse to be the settings.xml file from my external maven installation.
Now eclipse can download the required artifacts.
After entering your proxy settings in settings.xml
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
| -->
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>DOMAIN\YOURID</username>
<password>123456</password>
<host>proxy.company.com</host>
<port>8080</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
Check whether the below tag is having the value false in settings.xml
<offline>false</offline>
This helped me.
In addition to what #JackDev replies, what also solved my problem was to
1) Install the jdk under directory with no spaces:
C:/Java
Instead of
C:/Program Files/Java
This is a known issue in Windows. I fixed JAVA_HOME as well
2) Install maven as in Java case, under C:/Maven. Fixed the M2_HOME accordingly.
3) I java 7 and java 8 on my laptop. So I defined the jvm using eclipse.ini. This is not a mandatory step if you don't have -vm entry in your eclipse.ini. I updated:
C:/Java/jdk1.7.0_79/jre/bin/javaw.exe
Instead of:
C:/Java/jdk1.7.0_79/bin/javaw.exe
Good luck
If you've configured a repository in your maven's settings.xml, check if you've access to it.
When I had this problem, there were enterprise repositories configured in settings.xml but I was out of the company.
JackDev's option 3 works for me after I changed the default repository to another folder.
Below is what I see after M2E plugin automatically download the maven-resources-plugin-2.6. Maybe this could give you some hint if you want to take the manual approach. The necessary files can be downloaded from here: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/
If you have a proxy, you also have to clear SOCKS in
Window > Preferences > Network Connections.
I was getting the same issue.
I just installed the m2e (Maven2Eclipse)plugin from below site:
http://www.eclipse.org/m2e/
Eclipse>Help>Install New Software>Available Software Sites>Add
Name: m2e (any name is OK)
Location:m2e - http://download.eclipse.org/technology/m2e/releases/
Under Install Window> Work with:
Select this new location and Add all the plugins that appear. Eclipse restart and it was running properly with no previous errors.
If your working at a company, they may be preventing you from downloading outside software and installing it. You may need to install the plugins manually or repoint to an internal mirror repository.
Try downloading a different version of maven.
I had the same problem with maven 3.5.2 , I solved my problem just downloading maven 3.0.4
Very old stuff.
Got it solved fixing the localRepository in settings.xml.
This file was copied from my other computer and the path of the .m2 repository wasn't the same.
<?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">
<localRepository>C:\Users\foo\.m2</localRepository>
</settings>
I could solve the issue with the following steps
Install Maven separately
https://www.mkyong.com/maven/how-to-install-maven-in-windows/
Set the external Maven installation in Eclipse
3. Set the proxy in settings.xml in Maven installation
(C:\path\apache-maven-3.6.0\conf)
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>optional-proxyuser</username>
<password>optional-proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
Update the Maven User Settings
Update Maven project
I have shifted my project to a different machine, copied all my maven libraries from old machine to new machine, did Right click on my project >> Maven >> Update Project. And then built my project. In addition to this, I have also done this one step which is shown in screenshot. And that's all it worked!!
Go to Window --> Preferences --> Maven --> User Setting, make sure you have these settings..
Also Right click on your project --> Properties --> Maven, and make sure you have the path here to maven repository..
I am facing the same issue and none of above works, like by updating the MVN also same error, by building is also same, entered details in settings.xml though even same issue.
After that again I tried and did something different which did not did before and it works.
Its simple, I clicked the force update while updating the Mvn project.
By right clicking on the pom file, there is option under Maven,
"Update Project" and it open up one popup to select update option.
PLEASE MAKE SURE FORCE UPDATE IS CHECKED, by default is unchecked. And
bingo, that works like charm!
To solve this issue I tried below method :
(I was working on eclipse IDE)
Go to location : C:\User\local.m2\setting.xml
Open setting.xml file in any editor.
Comment the proxy setting like I did(see below setting.xml file).
Then go to eclipse & perform Maven Update from :right clicking on your solution folder(Your Project)->select Maven->Update Project.
Hope it will work for you as like me.
<?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">
<!--
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
-->
</settings>

Maven Archetype generate proxy issue, searching for remote catalog

Maven seem to be having a bug related to proxy when fetching archetype from maven's remote catalog.
My computer is behind a proxy server. I am trying to generate a project from maven command line.
D:\test>mvn archetype:generate -DarchetypeArtifactId=scala-archetype-simple -DinteractiveMode=false -DgroupId=com.sparktest -DartifactId=myspark -DarchetypeGroupId=net.alchim31.maven -DarchetypeVersion=1.6 -DarchetypeCatalog=remote -e -X
[DEBUG] Searching for remote catalog: http://repo.maven.apache.org/maven2/archetype-catalog.xml
[DEBUG] Archetype org.apache.maven.archetypes:scala-archetype-simple:1.6 doesn't exist
org.apache.maven.archetype.downloader.DownloadNotFoundException:
Requested org.apache.maven.archetypes:scala-archetype-simple:jar:1.6 download does not exist.
Below Eclipse Bugs are related, however the issue seems to be with maven rather than Eclipse.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=390183#add_comment
Please note there is no issue working on existing maven project. The dependencies get downloaded without any issue once maven proxy is configured in settings.xml. The issue comes when generating a project using archetype:generate and using maven remote archetype-catalog.
Maven archetype-catelog can be opened in a browser without any issue
http://repo.maven.apache.org/maven2/archetype-catalog.xml
It's seems that scala-archetype-simple archetype with groupId org.apache.maven.archetypes doesn't actually exist.
You should try one of these :
net.alchim31.maven
org.scala-tools.archetypes
I had this exact issue and the following worked for me: have two proxies, one for http and one for https.
<proxy>
<id>my-proxy-http</id>
<active>true</active>
<protocol>http</protocol>
<host>xx.xx.x.xxx</host>
<port>8080</port>
<username>myusername</username>
<password>mypassword</password>
</proxy>
<proxy>
<id>my-proxy-https</id>
<active>true</active>
<protocol>https</protocol>
<host>xx.xx.x.xxx</host>
<port>8080</port>
<username>myusername</username>
<password>mypassword</password>
</proxy>

Using a Windows mapped drive as an artifact/component repository

I would like to avoid using a full-fledged component repository such as Nexus/Artifactory as remote repository and use a mapped Windows drive instead with such:
\\drive\path\to\repository\
I am not sure how to proceed. Do I need the Wagon plugin for that?
Ideally, I would need to leave the POMs alone and modify the settings.xml only. Is this possible?
Just add the path to your settings.xml to use a remote file system as your local repository instead of under your .m2 directory:
<localRepository>\\drive\path\to\repository\</localRepository>
Of course, this will slow down your builds considerably, and if others are also sharing it, then you will find builds failing due to conflicts when multiple users access the build artifacts simultaneously.
Are you sure it's worth it when it would probably take less than an hour to set up a proper proxy/repository?
Yes you can add mirrors to your settings file and it accepts other protocols than http. It is recommended to use file://. I tried this and it worked
<settings>
<mirrors>
<mirror>
<id>mac</id>
<url>file:////server/folder/etc/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>

Including QRGen in pom.xml

I'm developing app in spring + hibernate in maven and I need to make QR codes so
I've found this tutorial http://viralpatel.net/blogs/create-qr-codes-java-servlet-qr-code-java/
In that tutorial following libs are in use:
xzing and qrgen (net.glxn.qrgen)
I've added in pom.xml dependency on xzing and its running but qrgen needs his own repo so
I've included repository like on that page: http://kenglxn.github.com/QRGen/ :
but it seems that http://kenglxn.github.com/QRGen/repository is down
Anyone knows new (or exact) repo address or could tell me what I'm doing wrong
version 1.2 is now available from maven central http://search.maven.org/#artifactdetails%7Cnet.glxn%7Cqrgen%7C1.2%7Cjar
please use:
<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.2</version>
</dependency>
Basically you have a third party JAR that is not available in a (public) repository. Best would be if you had a company wide repository that you use as proxy. It is quite easy to set up Nexus for this task. Then you can import the JAR to that repository (e.g. Nexus has a default "3rd party library" repository setting). If you don't have such a repository you need to import the JAR to your local repository but this restricts successfull builds to your machine - obviously not what you want when you use Maven. See here for the syntax for a local import.
Of course you could set up a network drive as a company wide repo, some think this is easier than setting up Nexus or any other repo server. Trust me, it is not worth it, do it the right way from the beginning.

How do I use Maven through a proxy?

I want to share my experience of using maven through a proxy.
You would most likely face exceptions and messages like:
repository metadata for: 'org.apache.maven.plugins' could not be retrieved from
repository: central due to an error: Error transferring file: Connection refused: connect
or
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-
plugin:2.5: Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its
dependencies could not be resolved: Failed to read artifact descriptor for
org.apache.maven.plugins:maven-clean-plugin:jar:2.5
How to configure Maven to use proxy server?
For details of setting up a proxy for Maven, see the mini guide.
Essentially you need to ensure the proxies section in either the global settings ([maven install]/conf/settings.xml), or user settings (${user.home}/.m2/settings.xml) is configured correctly. It is better to do this in your user settings to avoid storing the password in plain text in a public location.
Maven 2.1 introduced password encryption, but I've not got round to checking if the encryption applies for the proxy settings as well as repository passwords (don't see why it wouldn't though).
For info, there is a commented-out proxy configuration in your settings.xml and instructions on how to modify it.
From the mini-guide, your settings should 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">
[...]
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
</proxy>
</proxies>
[...]
</settings>
How to use a socks proxy?
Set up a SSH tunnel to a server somewhere:
ssh -D $PORT $USER#$SERVER
Linux (bash):
export MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=$PORT"
Windows:
set MAVEN_OPTS="-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=$PORT"
Also note that some plugins (remote-resources comes to mind) use a really old library that only accepts proxy configuration through MAVEN_OPTS;
-Dhttp.proxyHost=<host> -Dhttp.proxyPort=<port> -Dhttps.proxyHost=<host> -Dhttps.proxyPort=<port>
You might be stuck on auth for this one.
I also had this problem, and I solved it by editing the settings.xml file in my .m2 folder.
My settings.xml is like this now:
<settings>
<proxies>
<proxy>
<id>genproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxyHost</host>
<port>3128</port>
<username>username</username>
<password>password</password>
</proxy>
</proxies>
</settings>
To set Maven Proxy :
Edit the proxies session in your ~/.m2/settings.xml file. If you cant find the file, create one.
<settings>
<proxies>
<proxy>
<id>httpproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>your-proxy-host</host>
<port>your-proxy-port</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
<proxy>
<id>httpsproxy</id>
<active>true</active>
<protocol>https</protocol>
<host>your-proxy-host</host>
<port>your-proxy-port</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
</settings>
or
Edit the proxies session in your {M2_HOME}/conf/settings.xml
Thanks #krosenvold.
If the settings file changes don't work, try this in the command prompt having the POM file.
mvn install -Dhttp.proxyHost=abcproxy -Dhttp.proxyPort=8080 -Dhttps.proxyHost=abcproxy -Dhttps.proxyPort=8080
This has helped me immediately after a password change.
Those are caused most likely by 2 issues:
You need to add proxy configuration to your settings.xml. Here's a trick in your username field. Make sure it looks like domain\username. Setting domain there and putting this exact slash is important '\'. You might want to use <![CDATA[]]> tag if your password contains non xml-friendly characters.
I've noticed maven 2.2.0 does not work sometimes through a proxy at all, where 2.2.1 works perfectly fine.
If some of those are omitted - maven could fail with random error messages.
Just hope I've saved somebody from googling around this issue for 6 hours, like I did.
Just to add my own experiences with this: my company's proxy is http://webproxy.intra.companyname.com:3128. For maven to work via this proxy, the settings have to be exactly like this
<settings>
<proxies>
<proxy>
<id>default</id>
<active>true</active>
<protocol>http</protocol>
<host>webproxy.intra.companyname.com</host>
<port>3128</port>
</proxy>
</proxies>
</settings>
Unlike some other proxy-configuration files, the protocol here describes how to connect to the proxy server, not which kinds of protocol should be proxied. The http part of the target has to be split off from the hostname, else it won't work.
I run cntlm localy, configured with NTLMv2 password hashes to authenticate with the corporate proxy, and use
export MAVEN_OPTS="-DproxyHost=127.0.0.1 -DproxyPort=3128"
to use that proxy from maven. Of course the proxy you use should support cntlm/NTLMv2.
And to add to this topic, here're my experiences below... Really odd and time consuming so I thought it was worth adding.
I've had a similar problem trying to built the portlet-bridge on Windows, getting the following errors:
Downloading: http://repo1.maven.org/maven2/org/apache/portals/bridges-pom/1.0/bridges-pom-1.0.pom
[DEBUG] Reading resolution tracking file C:\Documents and Settings\myuser\.m2\repository\org\apache\portals\bridges-pom\1.0\bridges-pom-1.0.pom.lastUpdated
[DEBUG] Writing resolution tracking file C:\Documents and Settings\myuser\.m2\repository\org\apache\portals\bridges-pom\1.0\bridges-pom-1.0.pom.lastUpdated
[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM: Could not transfer artifact
org.apache.portals:bridges-pom:pom:1.0 from/to central (http://repo1.maven.org/maven2): Error transferring file: repo1.maven.org and 'parent.relativePath' points at wrong local
POM # line 23, column 11
...
[ERROR] The project org.apache.portals.bridges:portals-bridges-common:2.0 (H:\path_to_project\portals-bridges-common-2.0\pom.xml) has 1 error
[ERROR] Non-resolvable parent POM: Could not transfer artifact org.apache.portals:bridges-pom:pom:1.0 from/to central (http://repo1.maven.org/maven2):
Error transferring file: repo1.maven.org and 'parent.relativePath' points at wrong local POM # line 23, column 11: Unknown host repo1.maven.org -> [Help 2]
...
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
I tried a couple of things, following a bit of surfing:
Tried to set the parent.relativePath as empty so that maven didn't think the parent was local. This is as per the suggestion on SO at Hudson build fail: Non-resolvable parent POM and in this nabble forum. This had no effect.
I also tried ensuring the repository was explicitly listed in my settings.xml but this had no effect either.
I then ensured mvn was forced to lookup the repository, rather than rely on it's own history, as discussed in this blog by Sarthon. Unfortunately, this wasn't the issue either.
In some desperation, I then revisited my MAVEN_OPTS to ensure I wasn't falling foul of my proxy settings. These were correct, albeit with the value unquoted:
set MAVEN_OPTS= -Dhttp.proxyHost=myproxy.mycompany.com -Dhttp.proxyPort=8080 -Xmx256m
So, finally, I moved the proxy config into my settings.xml and this worked:
<proxies>
<proxy>
<id>genproxy</id>
<active>true</active>
<protocol>http</protocol>
<!--username>proxyuser</username-->
<!--password>proxypass</password-->
<host>myproxy.mycompany.com</host>
<port>8080</port>
<nonProxyHosts>*.mycompany.com|127.0.0.1</nonProxyHosts>
</proxy>
</proxies>
Really not sure why my original MAVEN_OPTS wasn't working (quotes?) while the settings.xml config did work. I'd like to reverse the fix and check each step again but have wasted too much time. Will report back as and when.
I know this is not really an answer to the question, but it might be worth knowing for someone searching this post. It is also possible to install a Maven repository proxy like nexus.
Your maven would be configured to contact the local Nexus proxy, and Nexus would then retrieve (and cache) the artifacts. It can be configured through a web interface and has support for (http) proxies).
This can be an advantage, especially in a company setting, as artefacts are locally available and can be downloaded fast, and you are not that dependent on the availability of external Maven repositories anymore.
To link back to the question; with Nexus there is a nice GUI for the proxy configuration, and it needs to be done on one place only, and not for every developer.
if you are new to proxy setup for Maven In my case first go and check your Home Folder weather there is .m2 folder and in it there should be a file named settings.xml if not create it , and paste this and change host and port,then if needed change the nonProxyHosts
Home Folder - C:\Users\ {UserName}
<settings>
<proxies>
<proxy>
<id>httpproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>your-proxy-host</host>
<port>your-proxy-port</port>
<nonProxyHosts>localhost</nonProxyHosts>
</proxy>
<proxy>
<id>httpsproxy</id>
<active>true</active>
<protocol>https</protocol>
<host>your-proxy-host</host>
<port>your-proxy-port</port>
<nonProxyHosts>localhost</nonProxyHosts>
</proxy>
</proxies>
</settings>
If any case this does not success go and do the changes in this location of Home Folder
/conf/settings.xml
I am using Eclipse as my IDE
Hope this will help !!
Note:
To remove the proxy just move settings.xml to some where else
If maven works through proxy but not some of the plugins it is invoking, try setting JAVA_TOOL_OPTIONS as well with -Dhttp*.proxy* settings.
If you have already JAVA_OPTS just do
export JAVA_TOOL_OPTIONS=$JAVA_OPTS
Except for techniques mentioned above, with some effort, you can run maven through proxy using jproxyloader library (there is example on page how to do this: http://jproxyloader.sourceforge.net/). This allows set up socks proxy only for downloading artifacts.
In solution mentioned by duanni (setting -DsocksProxyHost) there is one problem. If you have integration tests running against local database (or another tests connecting to url which should not go via proxy). These tests will stop working because connections to database will also be directed to proxy. With help of jProxyLoader you can set up proxy only for nexus host. Additionally if you want you can pass connections to database through another proxy.
Some times you need to add other <proxy></proxy> tags, and specify the https in the protocol tags: <protocol>https</protocol>
The above postings helped in resolving my problem. In addition to the above I had to make the following changes to make it work :
Modified Maven's JRE net settings(\jre\lib\net.properties) to use system proxy setting.
https.proxyHost=proxy DNS
https.proxyPort=proxy port
Included proxy server settings in settings.xml. I did not provide username and password settings as to use NTLM authentication.
Maven provides a built-in method of doing this, via a file called settings.xml, and this has been covered in other answers. However, it is customary, particularly in Linux, for command-line tools to automatically use the proxy specified by the environment variable https_proxy.
To follow the Don't repeat yourself principle (which is intended to help you avoid mistakes), it would be nice if mvn could automatically work with that too.
Here's a shell script that makes the necessary conversions:
#! /usr/bin/env bash
function javaproxy {
## using "Shell Parameter Expansion"
request_scheme=$1 ; proxy=$2
notscheme=$(echo ${proxy#*://}) ## parse
scheme=$(echo ${proxy%${notscheme}}) ## remove
scheme=$(echo ${scheme%://}) ## strip
hostport=$(echo ${proxy#*//*}) ## parse
host=$(echo ${hostport%:*}) ## parse
port=$(echo ${hostport#${host}}) ## remove
port=$(echo ${port#:}) ## strip
scheme=$(echo ${scheme:-http}) ## default
host=$(echo ${host:-localhost}) ## default
port=$(echo ${port:-8080}) ## default
echo -n " -D${request_scheme}.proxyHost=${host}"
echo -n " -D${request_scheme}.proxyPort=${port}"
}
JTO=""
if [ $http_proxy ] ; then
JTO="${JTO}$(javaproxy http ${http_proxy})"
fi
if [ $https_proxy ] ; then
JTO="${JTO}$(javaproxy https ${https_proxy})"
fi
if [ $no_proxy ] ; then
JTO="${JTO} -Dhttp.nonProxyHosts=$(echo \"${no_proxy}\"|tr ',' '|')"
fi
export JAVA_TOOL_OPTIONS=${JTO}
echo "JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}"
mvn_friendliness_options+=--update-snapshots
mvn ${mvn_friendliness_options} $#
You might name this something like proxied_mvn and run it as:
$ https_proxy=http://localhost:58080 ./proxied_mvn clean package
Alternatively, you could just move the environment setup into your startup scripts.
gotchas
There are many things that can go wrong when trying to configure Maven for access to a Nexus through a proxy. Hopefully, this script will help with some of the most finicky issues, but others remain:
Nexus credentials available and correct (only if required)
Check with mvn help:effective-settings
Maven caching: "resolution will not be reattempted"
mvn clean package --update-snapshots
Maven wall-of-text output -- you have to look closely at the output to make sure errors messages aren't subtly different between runs
Older versions of Java may require _JAVA_OPTIONS instead of JAVA_TOOL_OPTIONS.
epilogue
There is more than one kind of Proxy. Correspondingly, there is more than one way that this question has been interpreted -- contributing to the large number of disparate answers here.
I have explicitly addressed the case of a (forward HTTP/HTTPS) web proxy server, which is used to access the internet from within a company network (for some companies). This may be notably distinct from a SOCKS proxy, which has also been addressed in some answers here.
Oh by the way, since it uses JAVA_TOOL_OPTIONS, this solutions can be applied to running your other Java applications inside a proxy too.
Parting hint... My example above uses http://localhost:58080. This is because I've set up port-forwarding from my CLIENT_PROXY=localhost:58080 to the actual network proxy by using WSL on my remote-access client to run:
ssh $PROXY_CLIENT -R $CLIENT_PROXY:$SERVER_PROXY

Resources