Passing command line parameters to target that uses them in exec

ant
40,151

No.

You can not pass arguments that will be used inside a build file in that way. The ant bootstrap arg1 arg2 arg3 will be resolved as you are trying to call the following targets bootstrap, arg1, arg2, arg3 -- and, obviously, only the target bootstrap exists.

If you do want to pass arguments that will be used in the build file, you need to use the -DpropertyName=value format. For example:

ant bootstrap -Darg1=value1 -Darg2=value2 -Darg3=value3

For other ways, you can write embed script in the build file (like beanshell or javascript, with ant's script support libs) to process the arguments at first. For example, you can pass the arguments in this way:

ant bootstrap -Dargs=value1,value2,value3,...

and now you have a property named args with the value "value1,value2,value3,..." (for ... I mean that the user may type in more than 3 values). You can use beanshell to split the args to arg1, arg2 and arg3 by ,, and also do some checking...

<script language="beanshell" classpathref="classpath-that-includes-the-beanshell-lib">
    String[] args = project.getProperty("args").split(",");
    project.setUserProperty("arg1", args[0].trim());
    project.setUserProperty("arg2", args[1].trim());
    project.setUserProperty("arg3", args[2].trim());
</script>
Share:
40,151
peter cooke
Author by

peter cooke

trying to be a better programmer today than I was yesterday

Updated on April 16, 2021

Comments

  • peter cooke
    peter cooke about 3 years

    ant bootstrap arg1 arg2 arg3

    I need to echo "arg1 arg2 arg3" so that I can call a program with those arguments

    Searching the web the following should work but does not.

     <target name="bootstrap">
         <echo>${arg0} ${arg1} ${arg2} </echo>
         <!--exec executable="cmd">
            <arg value="${arg0}"/>
            <arg value="${arg1}"/>
            <arg value="${arg2}"/>
         </exec-->
     </target>
    

    Also, any thoughts on what if the user passes in 5 args or 1 arg. I need to fail it does not not have the right number of args.

  • peter cooke
    peter cooke almost 12 years
    thanks for clearing that up. but now I have to rethink my solution.