问题
Our shop has multiple build and test environments (Jenkins, Ant 1.9.6) set up on Java 6, 7 and 8. On Java 7 only, in order for our tests to run successfully, we need to add the JVM argument -XX:-UseSplitVerifier
.
I can't use a condition
inside junit
, so I set a boolean property in the init
task:
<condition property="usesplitverifier">
<equals arg1="${ant.java.version}" arg2="1.7" />
</condition>
In my test
task, I need to add jvmarg
to junit
only if that property is true
:
<echo message="usesplitverifier: ${usesplitverifier}" />
<junit printsummary="yes" haltonfailure="no" fork="yes" forkmode="once" tempdir="${build.dir}">
<jvmarg if="usesplitverifier" value="-XX:-UseSplitVerifier" />
...
</junit>
The output is:
[echo] usesplitverifier: true
BUILD FAILED
C:\workspace\util\build.xml:141: jvmarg doesn't support the "if" attribute
I'm trying to avoid adding ant-contrib to our build/test environment. Also, I'd like to avoid adding more targets to the build script (I'm trying to simplify the build process). Is there another approach that might work here?
回答1:
While I was writing this question, I found the solution in another question that was closed as being too vague. I decided to post this question in order to provide a specific use case.
The if/unless namespaces may be used in any Ant command. In my case, I needed to add the if
namespace to the project:
<project name="my-project" xmlns:if="ant:if" ...>
And then put the conditional on jvmarg
as follows:
<junit printsummary="yes" haltonfailure="no" fork="yes" forkmode="once" tempdir="${build.dir}">
<jvmarg if:set="usesplitverifier" value="-XX:-UseSplitVerifier" />
...
</junit>
The reason if:set
works in this case is that if ${ant.java.version}
is not 1.7
, the property remains unset.
Caveats (acceptable in my case):
- The
if/unless
namespaces are only available since Ant 1.9.1. - Prior to Ant 1.6,
${ant.java.version}
returns the version of the JVM that Ant was compiled with.
来源:https://stackoverflow.com/questions/35393545/how-can-i-provide-a-conditional-jvmarg-for-the-ant-junit-command