How to Configure Maven to work with NTLM proxies

I recently installed Maven at a client site that had a NTLM proxy, and ran into issues because it was failing to download maven dependencies.

I was following the instructions provided by Maven to correctly setup my proxy settings, without any luck!

I kept getting the following error when attempting to run mvn install goal to set up the project:

C:Dev\MavenTest> mvn clean install
[ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.4.1 or one of its dependencies could not be resolved: 
Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1: 
Could not transfer artifact org.apache.maven.plugins:maven-clean-plugin:pom:2.4.1 from/to central (http://repo.maven.apache.org/maven2):
Not authorized by proxy, ReasonPhrase:Proxy Authentication Required (
ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. ). -> [Help 1]
After few hours of Googling and trial and error, I finally found two methods of getting Maven to work with NTLM proxies.

Step #1: Add proxy settings to your Maven configuration file

  1. Open the Maven configuration file at %M2_HOME%/conf/settings.xml. For Linux, this is typically found at
     
    ${user.home}/.m2/settings.xml)

    For Windows, this can be found in one of the following locations:
# In Win XP, Vista
C:\Documents and Settings\username\.m2\settings.xml 
 
# In Windows 7/8
C:\users\username\.m2\settings.xml
  1. Find the section within settings.xml file that defines the proxies node. This should look something like:
    <proxies>
    <!-- proxy
    | Specification for one proxy, to be used in connecting to the network. 
    |
    <proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>http</protocol>
    ...
    </proxy>
    -->
    </proxies>
  2. Comment out the proxy node and enter your proxy information. Afterwords, the proxies node should look similar to:
    <proxies>
    <proxy>
    <!-- id field is optional -->
    <id>something-descriptive-to-identify-your-proxy</id>
    <!-- Tells Maven to use this proxy. Specify
    'false' if you want to turn it off. -->
    <active>true</active>
    <protocol>http</protocol>
    <!-- ENTER YOUR PROXY HOST NAME -->
    <host>company.proxy.host.com</host>
    <!-- ENTER YOUR PROXY HOST'S PORT NUMBER -->
    <port>8080</port>
    <!-- ENTER YOUR USER NAME TO LOGIN TO THE HOST -->
    <username>myUserName</username>
    <!-- ENTER YOUR PASSWORD ASSOCIATED WITH THE USERNAME -->
    <password>myPassword</password>
    <!-- Any URLs that you want to filter out
    (e.g. local repository?).
    Otherwise just keep this as it is -->
    <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    </proxies>
     
    IMPORTANT: Ensure that you change the host, port, username andpassword fields to match your environment/proxy setup.

    If the above settings doesn’t work, then you might want to change the username field to include the domain that the user belongs to. e.g.    

<username>myDomain\myUserName</username> 

Step #2: Add wagon-http-lightweight extension

Wagon HTTP lightweight library allows us to overcome authentication limitations in Maven (3.0.4) when working with NTLM proxies. We can follow the steps below to add the Wagon HTTP lightweight library as a Maven extension:

  1. Download the wagon-http-lightweight-2.2.jar from http://repo.maven.apache.org/maven2/org/apache/maven/wagon/wagon-http-lightweight/2.2/wagon-http-lightweight-2.2.jar
  2. Copy the wagon-http-lightweight-2.2.jar to %M2_HOME%/lib/ext folder.

EXAMPLE POM.XML TO TEST THE SOLUTION

To test our approach, first create a simple Maven project with the following pom.xml:

 
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thira.testmavenplugindl</groupId>
<artifactId>test-maven-plugin-dl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Test Maven Plugin Download Issue</name>
<description>Example pom file to test Maven dependency download with NTLM proxies</description>
</project>

The run the Maven goal described below. This should execute successfully anddownload all Maven dependencies:

 mvn clean install

Check your local repository directory (as defined in settings.xml file) to ensure all the dependencies are correctly downloaded.

If this approache still not work for your case, then you can refer below link to set the java proxy directly for Maven.

http://www.cnblogs.com/princessd8251/p/3819327.html

 

备注: 这种方法配置的proxy 只对多数插件有效,笔者遇到过一个问题就是在生产 site文件的时候,

负责生产issue报表的插件org.apache.maven.plugins:maven-changes-plugin:2.9 不主动去读这个配置,后面还是在MVN.bat里面配置的代理解决问题。

但是有些插件比如org.apache.maven.plugins:maven-site-plugin:3.3 在执行site:deploy的时候,只会查找settings.xml里面的proxy设置,会忽略掉MVN.bat里的设置.

所以最好的办法还是MVN.BAT和settings.xml里面都设置.

settings.xml 配置

    <proxies>
        <proxy>
            <active>true</active>
            <protocol>https</protocol>
            <host>xxxxx</host>
            <port>8080</port>
            <username>dummy\dummy</username>
            <password>dummy</password>
        </proxy>
    </proxies>

mvn.bat 配置
"-Dhttps.proxyHost=xxxxx" "-Dhttps.proxyPort=8080" "-Dhttps.proxyUser=dummy\dummy" "-Dhttps.proxyPassword=dummy"

 

posted @ 2014-07-01 17:21  princessd8251  阅读(1896)  评论(0编辑  收藏  举报