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.
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);
}
}
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) );
}
}