Ciobanu Andrei
Ciobanu Andrei

Reputation: 325

Ant task that contains java call, receiving arguments that will be set like system property parameters

I'm sorry for the title, but I can't figure out how can describing the problem in one sentence.

I have the next build.xml code:

<project name="Project" default="configure-and-run" basedir=".">
        <target name="run">
                <java classname="Main">
                        <classpath location="."/>
                        <sysproperty key="key1" value="value1"/>
                </java>
        </target>

        <target name="configure-and-run">
                <antcall target="run">
                        <param name="key2" value="value2"/>
                </antcall>
        </target>
</project>

In this case the pair key1->value1 can be obtained under java code with:

System.getProperty("key1");

My question is: how can give, or more precisely, how can I get the parameters on the "run" target, and give these to the java ant task?

In the above example, after the Main class is started, I wish I have possibility to get the "value2" with:

System.getProperty("key2");

Thanks in advance.

Meanwhile, I found a workaround:

My parent ant task has a list of parameters. Let me to copy and modify the above code:

<project name="Project" default="configure-and-run" basedir=".">
    <target name="run">
        <java classname="Main">
            <classpath location="."/>
            <sysproperty key="${prop1key}" value="${prop1value}"/>
            ...
            <sysproperty key="${propNkey}" value="${propNvalue}"/>
        </java>
    </target>
    <target name="configure-and-run">
        <antcall target="run">
            <param name="prop1key" value="myKey"/>
            <param name="prop1value" value="myValue"/>
        </antcall>
    </target>
</project>

The number of parameters is variable, depending on your needs.

I hope this thing will help someone else just as it helped me.

Have a nice day.

Upvotes: 2

Views: 538

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

The following Ant script uses the <syspropertyset> nested element of <java> to pass additional properties into the Java program.

<target name="run">
    <java classname="Main">
        <classpath location="."/>
        <sysproperty key="key1" value="value1"/>
        <syspropertyset refid="additional-java-sysproperties"/>
    </java>
</target>

<target name="configure-and-run">
    <property name="key2" value="value2"/>
    <propertyset id="additional-java-sysproperties">
        <propertyref name="key2"/>
    </propertyset>

    <antcall target="run">
        <reference refid="additional-java-sysproperties"/>
    </antcall>
</target>

Upvotes: 1

Related Questions