Set a system property with ant
Asked Answered
B

4

13

I have an ant script which has a taskdef and the task creates an https internet connection and somethin with that SSL stuff is wrong. Thus I want to set the system property javax.net.debug=all to get some more information.

In java I would do this using the -D option, but in ant this is used for ant properties which is not the same as a system property.

If this wouldn't be a taskdef but instead a java task, I could use the sysproperty property, but it is no java-task.

Googling for this is frustratingly complicated because ant properties and system properties in ant are so similar that most search results are about the other (or about the java-task).

Obviously I am not the only one with the problem, but other people's questions that I have found (like here) are unanswered or went for hack (like here).

Bunnie answered 15/1, 2014 at 20:54 Comment(0)
B
13

One way to set such a property is the ANT_OPTS system variable. You have to be very carefully to not simply skim over answers on google that state that options can be set that way, because it sounds so much like not what it does:

The documentation says:

ANT_OPTS - command-line arguments that should be passed to the JVM. For example, you can define system properties or set the maximum Java heap size here.

Who what have expected that? ANT_OPTS are options for the JVM and not for ant like the name suggests. The var which is used for ant options is called ANT_ARGS.

Now I can launch ant like this: ANT_OPTS="-Djavax.net.debug=all" ant myTarget and can see tons of log output.

(However this leaves the question open whether such a variable can be set using XML).

Bunnie answered 15/1, 2014 at 20:54 Comment(1)
Thanks, this info helpeld me a lot as I tried to figure out why jenkins takes a "wrong" value if defined it in Build -> Invoke Ant -> Properties e.g. user.name = prod. When defined in Java Options (uses ANT_OPS) as -Duser.name=prod everything works expected. System property user.name got's overwritten.Piquet
I
1

You can declare system properties in the xml with <sysproperty key="key" value="value"/>.

http://www.java2s.com/Code/Java/Ant/SetsystempropertiesinAntbuildscript.htm

Inform answered 8/5, 2014 at 21:1 Comment(1)
This can only be used on java tasks. See the third paragraph in my question which states: "If this wouldn't be a taskdef but instead a java task, I could use the sysproperty property, but it is no java-task."Bunnie
K
0

You can use scripting:

<script language="javascript">      
    java.lang.System.setProperty('myKey', 'myValue');
</script>
Kenway answered 1/2, 2021 at 18:36 Comment(0)
C
0

Both junit and java supports jvmarg

Run:

<project name="java-ant project" default="run">
    <target name="run">
        <java classname = "com.example.Hello">
            <classpath path="test"></classpath>
        </java>
        <jvmarg value="-Djavax.net.debug=all"/>
    </target>
</project>

Test:

<project name="java-ant project" default="run">
    <junit fork="true" forkmode="once" showoutput="true" printsummary="on">
        <jvmarg line="-XX:MaxPermSize=256m -Xms1024M" />
    </target>
</project>

Reference: Java Task | Junit Task

Cyrillus answered 19/6 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.