Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare and initialize a variable in an Ant script?

Tags:

java

ant

In Ant, can I create a target that contains something like a variable that represents a path?

For example, something like the following pseudo target:

<target name="initPath">
    Path = "${basedir}/../../myProject/Project/"
</target>

Where Path is my variable and is initializated to specific value.

How can I do this?

like image 819
AndreaNobili Avatar asked Oct 21 '13 14:10

AndreaNobili


Video Answer


2 Answers

Ant build scripts are written in XML. To create a property has to be in XML style, so instead of this:

some_prop="some value"

It is this:

<property name="some_prop"  value="some value"/>

Properties can contain periods, and I recommend using them as name separators:

<property name="some.prop"  value="some value"/>

How do you declare a constant? Here:

<property name="some.prop"  value="some value"/>

That's because once a property is set, it cannot be changed.

This way, you can do something like this:

<property file="${basedir}/build.properties"/>
<property name="some.prop" value="some value"/>

Let's say that the build.properties file contains this line:

some.prop="Some other value"/>

Now, when you run your Ant build file, the value of some.prop will be "Some other value", and the <property name="some.prop" value="some value"/> won't change it. I could even do this:

$ ant -Dsome.prop="A completely different value"

And this value of the some.prop property will override what I have in my build.properties file and what I have in my Ant build file.

This is a very nice feature. It allows me to set a default value that developers can override:

<property name="copy.verbose"  value="false"/>
...
<copy todir="${copy.to.dir}"
    verbose="${copy.verbose}">
    <fileset dir="${copy.from.dir}"/>
</copy>

By default, when my copy task runs, it runs in non-verbose mode which is what I want. However, let's say I am having a few problems with my build, and I want to see exactly what is being copied, I could do this:

$ ant -Dcopy.verbose=true

And, now my copy tasks will show me all the files being copied.


A path is a way to declare something like $CLASSPATH or $PATH in the command line. You can predeclare a path with an id, and then use it later:

<javac destdir="${main.destdir}"
    srcdir="${main.srcdir}">
    <classpath>
        <fileset dir="${lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </classpath>
</javac>

Here I'm adding a classpath. This is using <fileset/> to create a classpath based upon all of the jars in my ${lib.dir} directory.

I can also do this:

<path id="main.classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar"/>
    </fileset>
</path>

<javac destdir="${main.destdir}"
    srcdir="${main.srcdir}"
    classpathref="main.classpath"/>

Here, I predeclare my main.classpath and then use that later in my <javac> task.

You should read up on Ant in the on line Ant Manual. There is a semi-decent introduction in the manual which might help clarify a few issues for you.

like image 147
David W. Avatar answered Sep 23 '22 22:09

David W.


Here is how you can define a property in Ant script.

Unfortunately it is not a variable, since they are immutable. You can set a value to it, but you cannot change it during your script execution.

Here you can see an example of assigning value to a property.

Update. You can use path task. For example:

    <path id="combinedPath">
        <path path="${toString:oldPath}"/>
        <path path="my.jar"/>
    </path>

    <path id="reanamePath">
        <path path="${toString:oldPath}"/>
    </path>

If you change path in one target you can definitely access it in another one.

like image 24
Ivan Mushketyk Avatar answered Sep 22 '22 22:09

Ivan Mushketyk