I'm using httpunit to access a server.
I need to configure the proxy settings for this (http and https).
I set the configuration in the settings.xml file, but surefire seems to ignore it!?
I want to avoid to duplicate the configuration as much as possible.
In the surefire plugin configuration I tried:
<systemPropertyVariables>
<http.proxyHost>${http.proxyHost}</http.proxyHost>
</systemPropertyVariables>
and
<argLine>-Dhttp.proxyHost=${http.proxyHost}</argLine>
and
<argLine>-Dhttp.proxyHost=${settings.proxies[protocol=http].host}</argLine>
and several other combinations.
I print the system properties in the unit test with:
for (String propertyName : new TreeSet<String>(System.getProperties().stringPropertyNames())){
System.out.println(propertyName + ": " + System.getProperty(propertyName));
}
The only thing which worked so far are explicit values such as:
<systemPropertyVariables>
<http.proxyHost>myProxy</http.proxyHost>
</systemPropertyVariables>
or
<argLine>-Dhttp.proxyHost=myProxy</argLine>
But as I said, I don't want to duplicate the configuration, if possible.
How can I use the proxy settings set in the settings.xml file in unit tests?
I solved that by providing all proxy-related settings to Maven via system properties when needed, plus some tweaks to detect in runtime if those settings present in my parent POM.
1) In environments where proxy settings are required, create RC file for Maven ("~/.mavenrc"
or "%PROFILE%\mavenrc_pre.bat"
) with MAVEN_OPTS
inside. For example:
set MAVEN_OPTS=-Dhttp.proxyHost=10.0.1.250 -Dhttp.proxyPort=3128 -Dhttp.nonProxyHosts="localhost|*.local|*.mylab.com"
2) Detect if proxy settings were provided and build arguments for 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) Use prepared arguments in Surefire and Failsafe plugins:
<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>
Enjoy :)
Maven Surefire plugin's forkMode defaults to "once". I would suggest setting this to "never" and then trying to run the build again. My theory here is that you are losing the system property because the Surefire plugin is forking a new JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With