is it possible to access from the ant script of the nbi (installer project) to variables defined in the platform.properties file, like the nbjdk.active which is setted when in a project the Java platform is changed?
The goal is from the ant script select one of the packaged jre (32 or 64) in function of this variable.
Thanks in advance.
Edit:
this is the build script fragment from when I try to access this variables:
<target name="-generate-bundles">
<for-each property="platform" list="${target.platforms}" separator=" ">
<condition property="bundle.extention.${platform}" value="exe">
<contains string="${platform}" substring="windows"/>
</condition>
<condition property="bundle.extention.${platform}" value="sh">
<or>
<contains string="${platform}" substring="linux"/>
<contains string="${platform}" substring="solaris"/>
</or>
</condition>
<condition property="bundle.extention.${platform}" value="zip">
<contains string="${platform}" substring="macosx"/>
</condition>
<set property="bundle.extention" source="bundle.extention.${platform}"/>
<create-bundle root="${output.dir}/registry-temp"
platform="${platform}"
target="${bundles.release.dir}/${bundle.files.prefix}-${platform}.${bundle.extention}">
<component uid="${main.product.uid}" version="1.0.0.0.0"/>
<!-- HERE I WANT TO CHECK THE VARIABLE AND SELECT ONE OF THE PACKED JRE -->
<!--<property name="nbi.bundled.jvm.file" value="D:\packed\jre1.8.0_65_32bits\jre.exe"/>-->
<property name="nbi.bundled.jvm.file" value="D:\packed\jre1.8.0_25_64bits\jre.exe"/>
</create-bundle>
<echo>************************</echo>
<echo>********* OS: ${platform}</echo>
<echo>********* Arch: ${os.arch}</echo>
<echo>********* JDK in NB: ${jdk.home}</echo>
<echo>********* JDK in platform.properties: HERE I TRY TO ACCESS VARIABLE</echo>
<echo>************************</echo>
<if property="bundle.extention" value="zip">
<antcall target="zip-to-tgz">
<param name="input.file" value="${bundles.release.dir}/${bundle.files.prefix}-${platform}.zip"/>
<param name="output.file" value="${bundles.release.dir}/${bundle.files.prefix}-${platform}.tgz"/>
</antcall>
<delete file="${bundles.release.dir}/${bundle.files.prefix}-${platform}.zip"/>
</if>
</for-each>
<echo>Installer(s) for [${target.platforms}] are available at ${bundles.release.dir}</echo>
</target>
and this is the variable in platform.properties file:
nbjdk.active=JDK_1.8.0_65-32bits
Here is the property file contents which are going to be accessed in the build script:
Below build script example shows how to access property abc which is from test.properties file.
All you need is to load the property file before accessing it as shown in the script using, of course, change the property file path as per your environment.
<property file="D:/Temp/test.properties"/>
Then use ${abc} whereever its value is needed as shown in the echo task below.
test.properties contents
abc=123
def=234
build.xml
<project name="imported" basedir="." default="test">
<property file="D:/Temp/test.properties"/>
<target name="test" description="check if the property can be retrieved from file">
<echo message="Property abc's value from file is ${abc}"/>
</target>
</project>
Output
test:
[echo] Property abc's value from file is 123
BUILD SUCCESSFUL
Total time: 0 seconds
[Finished in 4.7s]
Hope this is helpful.
Related
I was trying to run an Ant script in Eclipse, but I am getting the below error:
BUILD FAILED
C:\Users\name\workspace\Client\build\Build_Local.xml:111: Error running C:\Program Files\Java\jdk1.6.0_45\jre\bin\javac compiler.
My java home is pointed to jdk and I edited the java path in windows>preferences>installed jre as well as in run>external tool>config>jre. Both locations are pointed to jdk. Also I am posting my build file(This is not the exact file, I have edited a lot before posting)
<?xml version="1.0"?>
<project name="projectName" default="main" basedir="C:\path\build">
<property name="LocalHome" value="C:\proName" />
<property name="home" value="${LocalHome}"/>
<property name="java.home" value="C:\Program Files \Java\jdk1.6.0_45"/>
<property name="CLIENT_JAR" value="client.jar"/>
<target name="main" description=": This is the default target.">
<antcall target="name"/>
</target>
<target name="name">
<javac failonerror="true" srcdir="${home}/source" destdir="path to class file"
executable="${java.home}\bin\javac" fork="true" debug="on" encoding="UTF-8"
source="1.6" target="1.6" bootclasspath="${java.home}/jre/lib/rt.jar"
classpath=""/>
</project>
See here I mentioned the javac path to "C:\Program Files \Java\jdk1.6.0_45\bin\javac". Then why does Ant choose javac from C:\Program Files\Java\jdk1.6.0_45\jre\bin\javac (see the build error)
It worked when I directly used java home path(C:\Program Files\Java\jdk1.6.0_45) in place of java.home
${java.home} points to C:\Program Files\Java\jdk1.6.0_45\jre. So you should be able to just remove that from bootclasspath:
bootclasspath="${java.home}/lib/rt.jar"
If you put the following line below a target, you'll see it print out the full path with jre appended:
<echo level="info" message="java.home = ${java.home}"/>
I want to set an env variable inside my build.xml target
<target name="run-tenantManagement" depends="jar">
<property name="SIMV3.1" value="${SIMV3.1}" />
//now here i want to do something like setenv SIMV3.1 true
</target>
and Inside my java code, I want to access it using :
if("true".equals(System.getenv("SIMV3.1")){
//do something
}
Kindly suggest. I have tried many things but none of them worked.Also, there is no main() method as the framework is testng based and test cases are invoked using testNG.
How are you running your program? If it is using exec with fork, then you can pass new environment to it
https://ant.apache.org/manual/Tasks/exec.html.
Example from the page..
<exec executable="emacs">
<env key="DISPLAY" value=":1.0"/>
</exec>
Consider following build.xml file
<?xml version="1.0"?>
<project name="MyProject" default="myjava" basedir=".">
<target name="myjava">
<!--default , if nothing comes from command line -->
<property name="SIMV3.1" value="mydefaultvalue"/>
<echo message="Value of SIMV3.1=${SIMV3.1}"/>
<java fork="true" classname="EnvPrint">
<env key="SIMV3.1" value="${SIMV3.1}"/>
</java>
</target>
</project>
and small java program
public class EnvPrint {
public static void main(String[] args) {
System.out.println(System.getenv("SIMV3.1"));
}
}
With out any command line:
$ ant
Buildfile: C:\build.xml
myjava:
[echo] Value of SIMV3.1=mydefaultvalue
[java] mydefaultvalue
With some arguments from command line:
$ ant -DSIMV3.1=commandlineenv
Buildfile: C:\build.xml
myjava:
[echo] Value of SIMV3.1=commandlineenv
[java] commandlineenv
Immutability: In ant, properties are immutable:
<property name="env.foo" value="your value goes here"/>
won't work.
Mutability: But variables are mutable, so this works:
<variable name="env.foo" value="your value goes here"/>
Modified Code :
<target name="run-tenantManagement" depends="jar">
<variable name="env.SIMV3.1" value="${SIMV3.1}"/>
</target>
Yes you can do this. Place your variable in a build.properties file and reference it in your build.xml. Then you can pass the variable... But I think it would be much better to use Maven Profiles if you need to have better control over multiple environment configurations.
build.properties
var=${val};
build.xml
<property file="build.properties"/>
<property name="var" value="${val}"/>
<target name="init">
<echo>${var}</echo>
</target>
CLI
ant -Dvar=value
My project has a jar in classpath with some utilities in it. When I run the program using ant on Red Hat linux, it's unable to read the system properties using System.getProperty(key), but it works perfectly fine when ran on Ubuntu.
Any suggestion to why this might be happening is appreciated.
ant target
<target name="test">
<property environment="env"/>
<echo message="PATH_ROOT set => ${env.PATH_ROOT}" />
<echo message="CUSTOM_REPORT_PATH set => ${env.CUSTOM_REPORT_PATH}" />
<testng classpathref="compile.classpath" haltOnfailure="false" outputDir="${testng.output.dir}/xxx">
<sysproperty key="PATH_ROOT" value="${env.PATH_ROOT}"/>
<sysproperty key="CUSTOM_REPORT_PATH" value="${env.CUSTOM_REPORT_PATH}"/>
<xmlfileset dir="src/com/xxx" includes="suite.xml" />
</testng>
</target>
================
Guys it was a silly mistake. The framework was not executing the file which had the statements to fetch system variables as the listener was missing in testng suite which is a must for testng to invoke that
I have a property file (x.props) that contains properties required for an Ant build:
x.props:
prop1=something
prop2=somethingElse
set in build.xml:
<property file="x.props" />
The properties contained in x.props are then used in a path task:
<path id="project.class.path">
<pathelement location="${prop1}/important.jar"/>
<pathelement location="${prop2}/anotherImportant.jar"/>
</path>
Before compilation starts. I don't explicitly create these properties in build.xml
The x.props file can be different for each local user, but for server builds it doesn't change.
I created an x.props.template file to address the problem of users accidentally checking in or updating this file (but they need to be able to check it out). I want to be able to create an x.props file from the x.props.template (I set x.props as svn:ignore).
Users can save the template file as x.props and edit it and - theoretically, the server Ant build.xml can copy the template file to the server x.props if it doesn't exist.
But that's not happening. I tried using a target:
<target name="x-props-file" unless="prop1" description="create a
local.properties file if it doesn't exist">
<copy file="x.props.template" tofile="x.props" />
</target>
But it's called AFTER the path is set.
I tried creating the file as a condition:
<condition available="x.props">
<not>
<copy file="x.props.template" tofile="x.props" />
</not>
</condition>
But 'not' and 'condition' don't work with copy.
loadProperties will fail if the file isn't found - but it won't copy a file.
Ideally I'd like to be able to copy the new x.props right after the initial file load fails.
Is there any way around this?
ant version 1.1
java 7
Thanks
This would probably be simplest with ant-contrib's <if> task:
<if>
<not>
<available file="x.props" />
</not>
<then>
<copy file="x.props.template" tofile="x.props" />
</then>
</if>
Edit: I just rediscovered this answer that I gave years ago after a comment was added, and realized that there's a much better solution that doesn't rely on ant-contrib. It's probably way too late to be of much use to the original poster, but I figured I'd offer it up anyway in case it helps someone:
<property file="x.props" />
<property name="prop1" value="something" />
<property name="prop1" value="somethingElse" />
<path id="project.class.path">
<pathelement location="${prop1}/important.jar"/>
<pathelement location="${prop2}/anotherImportant.jar"/>
</path>
No need for an extra template file. Just attempt to load the property file first and set your defaults immediately after that, making use of Ant's inherent property immutability.
Conversely, if you really want to use a separate properties file to hold your default values, just do the same thing as above but replace the individual property assignments with another property file load:
<property file="x.props" />
<property file="default.props" />
I have problem testing file existence with ant. I want to check if files exist in target test, and if not, I want download files in target download. But the download target will be executed always (if files exist or not). Can anybody show what is wrong?
<!-- Test lib files if exist -->
<target name="test">
<condition property="is.resource.exists" value="true" else="false">
<and>
<resourceexists>
<file file="${lib}/jdom-2.0.5.jar" />
</resourceexists>
<resourceexists>
<file file="${lib}/miglayout-4.0-swing.jar" />
</resourceexists>
</and>
</condition>
</target>
<!-- Download lib files if not exist -->
<target name="download" if="is.resource.exists" depends="test">
<exec dir="${lib}" executable="${lib}/get-libs.sh" />
</target>
A <target> with an if attribute will execute if the property in the if attribute exists. Similarly, a <target> with an unless attribute will execute if the property in the unless attribute doesn't exist. It doesn't matter what the value of the property is: true, false, kumquat, or whatever.
Replace the if="is.resource.exists" with unless="is.resource.exists" and you should be good.