2011-11-24 82 views
10

我正在使用httpunit访问服务器。Maven + Surefire:代理配置

我需要为此(http和https)配置代理设置。

我在settings.xml文件中设置了配置,但是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文件中设置的代理设置?

+0

如何在'settings.xml'中将'http.proxyHost'作为'property'?我想现在你正在尝试使用'proxy'设置值。 – Raghuram

回答

1

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

+0

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> 
--> 
3

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

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

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

2)检测如果提供了代理服务器设置,并建立论据神火:

<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)使用准备在神火参数和故障保护插件:

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

享受:)