2

I am new to Ant and any help will be appreciated.

What I want to do is:

When I am invoking an Ant target, I am doing :

ant -DSIMV3.1=true run-tenantManagement

Now Inside build.xml, I have:

<target name="run-tenantManagement" depends="jar">
   <property name="SIMV3.1" value="${SIMV3.1}" />
    ...
</target>

Now I want the value of the property SIMV3.1 to be visible inside my java code. Because in my java code, I want to set a condition that:

if(SIMV3.1==true){
//do something
}else{
//do something else
}

Kindly help.

1
  • Do you want the variable to be available at compilation time or at runtime? That is, does run-tenantManagement use <java> or <javac>? Commented Dec 30, 2014 at 5:52

2 Answers 2

3

Another approach that's more persistent as far as the property value in your target build (i.e. the resulting jar file) is to have ant write the property value to a resource file that gets included in the target jar file. This gives the property value that gets set during the build a better lifespan (for lack of a better word) so that it can be made available in the project's (i.e. build's) products. For instance, an example ant file may look like:

<project name="sandbox" basedir="." default="build">
    <condition property="SIMV3.1" value="${SIMV3.1}" else="false">
        <isset property="SIMV3.1"/>
    </condition>

    <target name="clean">
        <delete verbose="${verbose}" dir="bin" failonerror="false"/>
        <delete verbose="${verbose}" file="lib/sandbox.jar" failonerror="false"/>
    </target>

    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin"/>
    </target>

    <target name="resources">
        <echo file="bin/sandbox/resources/SIMV3.1">${SIMV3.1}</echo>
    </target>

    <target name="build" depends="compile, resources">
        <mkdir dir="lib"/>
        <jar destfile="lib/sandbox.jar" basedir="bin">
            <manifest>
                <attribute name="Main-Class" value="sandbox.Sandbox"/>
            </manifest>
        </jar>
   </target>
</project>

The condition task defaults the property value to false if it's not provided and the resources tasks writes the property value to the resource file that will be included in the jar file (in the build task). Therefore building like this:

ant -DSIMV3.1=true

will result in a "true" value being written to the SIMV3.1 file in the resources package. Invoking ant without specifying the property will result in a default property of false being written to that resource file.

In the code this resource can be accessed via InputStream by reading the first (and only) line from the file and parsing that line using the Boolean class like so:

package sandbox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Sandbox {

    public static void main(String[] args) {

        String resource = "/sandbox/resources/SIMV3.1";
        InputStream stream = Sandbox.class.getResourceAsStream(resource);
        InputStreamReader reader = new InputStreamReader(stream);
        BufferedReader buffer = new BufferedReader(reader);
        boolean simv3_1 = false;

        try {

            simv3_1 = Boolean.parseBoolean(buffer.readLine());
        }
        catch (IOException e) {

            e.printStackTrace();
        }

        System.out.println("SIMV3.1 = " + simv3_1);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can make use of sysproperty in java ant task to set the property.

<target name="run-tenantManagement" depends="jar">
    <java classname="props"    fork="true">
        <sysproperty key="SIMV3.1"
             value="${SIMV3.1}"
             />
    </java>
</target>

This can be read in java class as below,

import java.util.Properties;
public class props {
    public static void main( String[] args )
    {
        String     testProp = "SIMV3.1";
        Properties sysProps = System.getProperties();
        System.out.println( "Value of " + testProp + " is " +
                        sysProps.getProperty(testProp) );
    }
 }

6 Comments

Is it necessary that we print its value in main() ? If I print it it some constructor/other method, its coming out to be null
Nope, I used main only as an example. Are you sure you are accessing the property in the same java class as mentioned in classname attribute in java task ( <java classname= "props" ..) ?
I missed this exception.I am getting Exception in thread "main" java.lang.NoClassDefFoundError: com/oracle/common/Constants [java] Caused by: java.lang.ClassNotFoundException: com.oracle.common.Constants
That is self explanatory, you have missed to include classpath to find Constants class. You can create classpath and refer it in the java task. PS: This exception is not relevant to this thread. Please stick to the issue the thread is meant for. You can ask separate question if you are stuck and need help
I have resolved the classpath issue but now I am getting Exception in thread "main" java.lang.NoSuchMethodError: main So, why is it looking for main method?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.