Maven + Surefire: 代理配置

10

我正在使用httpunit访问服务器。

我需要配置代理设置,包括http和https。

我在settings.xml文件中设置了配置,但是surefire似乎忽略了它!?

我希望尽可能减少重复配置。

在surefire插件配置中,我尝试过:

<systemPropertyVariables>
    <http.proxyHost>${http.proxyHost}</http.proxyHost>
</systemPropertyVariables>

<argLine>-Dhttp.proxyHost=${http.proxyHost}</argLine>

<argLine>-Dhttp.proxyHost=${settings.proxies[protocol=http].host}</argLine>

还有其他几种组合。

我在单元测试中使用以下代码打印系统属性:

for (String propertyName : new TreeSet<String>(System.getProperties().stringPropertyNames())){
        System.out.println(propertyName + ": " + System.getProperty(propertyName));
    }

到目前为止唯一有效的是明确指定数值,例如:

<systemPropertyVariables>
    <http.proxyHost>myProxy</http.proxyHost>
</systemPropertyVariables>
或者
<argLine>-Dhttp.proxyHost=myProxy</argLine>

但正如我所说,如果可能的话,我不想重复配置。

我该如何在单元测试中使用在settings.xml文件中设置的代理设置?


settings.xml中将http.proxyHost作为一个property属性怎么样?我猜目前你正在尝试使用代理设置值。 - Raghuram
3个回答

3

当需要时,我通过系统属性向Maven提供所有代理相关的设置,并进行一些调整以在运行时检测这些设置是否存在于我的父POM中来解决此问题。

1)在需要代理设置的环境中,创建Maven的RC文件("~/.mavenrc""%PROFILE%\mavenrc_pre.bat"),在其中添加MAVEN_OPTS。例如:

set MAVEN_OPTS=-Dhttp.proxyHost=10.0.1.250 -Dhttp.proxyPort=3128 -Dhttp.nonProxyHosts="localhost|*.local|*.mylab.com"

2) 检测代理设置是否已提供,并为Surefire构建参数:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>execute</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <source>
            <![CDATA[
                // Collect proxy settings to use in Surefire and Failsafe plugins
                def settings = "";
                System.properties.each { k,v ->
                    if ( k.startsWith("http.") || k.startsWith("https.") )
                    {
                        // We have to escape pipe char in 'nonProxyHosts' on Windows
                        if (System.properties['os.name'].toLowerCase().contains('windows'))
                            v = v.replaceAll( "\\|", "^|" );
                        settings += "-D$k=$v ";
                    }
                }
                project.properties["proxy.settings"] = settings;
            ]]>
        </source>
    </configuration>
</plugin>

3) 在Surefire和Failsafe插件中使用准备好的参数:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <argLine>${proxy.settings}</argLine>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <argLine>${proxy.settings}</argLine>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
    </configuration>
</plugin>

祝您愉快 :)


1

Maven Surefire插件的forkMode默认为“once”。我建议将其设置为“never”,然后再次尝试运行构建。我的理论是,由于Surefire插件正在分叉新的JVM,因此您正在失去系统属性。


Surefire故意“丢失”系统属性 - 它假装为您的测试提供干净的环境。 - Anton

0

编辑Maven的settings.xml文件以添加代理对我来说很有效。在Ubuntu和AWS Linux中,路径为/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven/conf

<!-- proxy
 | Specification for one proxy, to be used in connecting to the network.
 |
<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>
-->

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接