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
Related
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.
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 have been struggling with this for two days now. I created a very simple HelloWorld class to test if I can get this working but I was not able to.
I get Error- Could not find or load main class...
It works from Eclipse or run task from the script. But double-clicking .jar or running it from CMD gives me the error. What are some possible reasons for this error? Class-path? environmental variables? directory structure? Please help!
package com.hellojava;
public class HelloWorld {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
My build.xml
<project name="TestProject" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.hellojava.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
run:
[java] Hello World!
main:
BUILD SUCCESSFUL
Total time: 5 seconds
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Main-Class: com.hellojava.HelloWorld
**Edited:
java -jar TestProject.jar works but javaw -jar TestProject.jar does not. However, I solved the problem- see answer I posted.
The usual syntax to run a jar file is java -jar TestPractice.jar.
If this fails, some analysis is required.
Open your jar file with WinZip, and extract to a temporary directory.
Check that HelloWorld.class file is present, and in the correct directory.
Check that there is a META-INF/MANIFEST.MF file, and it contains a line reading
Main-Class: com.hellojava.HelloWorld I don't see anything in your Ant script that would generate this file.
If all that is OK, your program will run.
For more information, take a look at how to create a bundled runnable jar using Ant
Problems Solved!
I had to change the registry to a correct Java version that I am using. It was set to the previous version of Java that was in my comp.
I can run .jar files through cmd, but I cannot double click them
Also, I noticed that javaw -jar file.jar does not work for me while java -jar file.jar works.
I changed the program that opens .jar files by "open with" and select java instead of javaw. This now solves the problem- I do have one more problem though. I wonder why javaw does not work but for now I'm happy this is working :)
I had the same problem. Application worked fine on my PC but on some PCs I get the same error. Try to upadte Java runtime on machines where your app donesn't work. For example I worked with JRE7 and on PC where app was not working there was Java 1.6 installed.
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 am trying to use xjc compiler from ant. Builds successfully but nothing gets generated.
My ant script is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project name="AutomateWithAnt" basedir=".">
<property file="build.properties"/>
<path id="compile.classpath">
<fileset dir="${lib.includes}" includes="*.jar"></fileset>
</path>
<target name="init" description="create java class">
</target>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask" classpathref="compile.classpath"/>
<!-- Generates the source code from the test.xsd schema using jaxb -->
<target name="option-generate" description="Generates the source code" depends="init">
<xjc schema="test.xsd" destdir="${generated-src.dir}" package="${generated-src.dir}">
<arg value="-Xcommons-lang" />
<arg value="-Xcommons-lang:ToStringStyle=SHORT_PREFIX_STYLE" />
<produces dir="${generated-src.dir}" includes="**/*.java" />
</xjc>
</target>
</project>
my build.properties is:
lib.includes=lib/
generated-src.dir=/
I am using java 1.6 and I have used jaxb-sjc.jar.
You've defined 2 Ant targets (init and option-generate), but neither of them will be invoked unless you specify which one to run.
You either need to specify it on the command line, e.g.
ant option-generate
or add a default target to the <project> element, e.g.
<project name="AutomateWithAnt" basedir="." default="option-generate">
Incidentally, your init target is empty, and therefore pointless.