【问题标题】:Maven + Surefire: proxy configurationMaven + Surefire:代理配置
【发布时间】:2011-11-24 18:12:41
【问题描述】:

我正在使用 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 怎么样?我猜目前您正在尝试使用proxy 设置值。

标签: java maven proxy maven-surefire-plugin http-unit


【解决方案1】:

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

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

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>

享受:)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Surefire 故意“释放”系统属性 - 它假装为您的测试提供干净的环境。
    【解决方案3】:

    编辑 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>
    -->
    

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 1970-01-01
      • 2010-11-19
      • 2012-10-27
      • 2016-11-21
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多