Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing command line arguments to Java via ant build script

On running the following command:

ant targetname -Dk1=v1 -Dk2=v2

I want the command line parameters passed down to java, like java whatever -Dk1=v1 -Dk2=v2.

I need to access these parameters from Java code with System.getProperty or System.getenv.

What do I need to write in my ant build script to make this happen?

Or should I take some other approach altogether?

like image 984
missingfaktor Avatar asked Jan 09 '13 14:01

missingfaktor


Video Answer


1 Answers

I'm not sure exactly how you want to pass these values, but there are several mechanisms:

  • Use <sysproperty> to pass system properties you need to set:
  • Use <arg> to pass command line arguments to your Java class
  • Use <jvmarg> to pass arguments to your Java command itself
  • If you fork your Java task, you can also set environment variables too. These are ignored if you don't fork the Java task

This:

 $ foo=bar; java -Xlingc com.example.foo.bar -Dsys1=fu -Dsys2=barfu -arg1 -arg2 bar

Becomes:

<java classname="com.example.foo.bar"
    fork="true">
    <env key="foo" value="bar"/>
    <sysproperty key="sys1" value="fu"/>
    <sysproperty key="sys2" value="barfu"/>
    <jvmarg value="-Xlingc"/>
    <arg value="-arg1"/>
    <arg value="-arg2"/>
    <arg value="bar"/>
</java>

Hope that example helps

like image 193
David W. Avatar answered Oct 02 '22 13:10

David W.