How to add jar operation using ant - java

I am trying to run a jar file and execute some unit tests. Currently (without ant) I do it like this
java -jar ./rest-test-runner-0.0-all.jar run HelpServiceUrlTest -config ./tools/rest-test-config/srv13/
That would run me a test class that was specified. How can I do exactly the same (or run all tests) using ant ?
I have this so far. It starts and ends with success but does not execute any test
<target name="run-tests" depends="prepare">
<java jar="rest-test-runner-0.0-all.jar"
fork="true"
failonerror="true"
maxmemory="128m">
<classpath>
<pathelement location="rest-test-runner-0.0-all.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
<jvmarg value="-Dconfig=srv10"/>
</java>

The following snippet will do exactly the same as the command line you provided
<target name="run-tests" depends="prepare">
<java jar="rest-test-runner-0.0-all.jar"
fork="true"
failonerror="true"
maxmemory="128m">
<classpath>
<pathelement location="rest-test-runner-0.0-all.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
<arg line="run HelpServiceUrlTest -config ./tools/rest-test-config/srv13/"/>
</java>

Related

Add library jar files in Java Task?

The spark-de.jar used other jar files as libraries. Assume running spark-de.jar requires lib1.jar and lib2.jar, how to set them in the following Java task of Ant?
<target name="makeQuery">
<java classname="sparkDemo.SparkTest" failonerror="true">
<classpath>
<pathelement location="${basedir}/spark-de.jar"/>
</classpath>
</java>
</target>
This should do the trick:
<target name="makeQuery">
<java classname="sparkDemo.SparkTest" failonerror="true">
<classpath>
<pathelement location="${basedir}/spark-de.jar"/>
<pathelement location="${basedir}/lib1.jar"/>
<pathelement location="${basedir}/lib2.jar"/>
</classpath>
</java>
</target>

Weird error in using Ant

This is my build.xml using ant.
<?xml version="1.0" encoding="UTF-8"?>
<project name="compile" default="css.concatenate" basedir=".">
<property name="charset" value="utf-8"/>
<!-- compile LESS -->
<target name="css.concatenate">
<concat destfile="${basedir}/src/main/webapp/cons/cons.less">
<fileset dir="${basedir}/src/main/webapp/less">
<include name="*.less"/>
</fileset>
</concat>
<echo>cons is done!</echo>
</target>
<target name="lessc" depends="css.concatenate">
<echo>now in compression</echo>
<java classname="CpLess" fork="true">
<arg value="${basedir}/src/main/webapp/cons"/><!-- input folder that contains less file -->
<arg value="${basedir}/src/main/webapp/css"/><!-- output folder -->
<classpath>
<pathelement location="${basedir}/lib/commons-logging.jar"/>
<pathelement location="${basedir}/lib/js.jar"/>
<pathelement location="${basedir}/lib/lesscss-engine.jar"/>
<pathelement location="${basedir}/lib/yuicompressor.jar"/>
<pathelement location="${basedir}/lib/zkjszips.jar"/>
<pathelement location="${basedir}/lib/zkless.jar"/>
<pathelement location="${basedir}/lib/zul.jar"/> <!-- only needed if using _zkmixins.less -->
</classpath>
</java>
</target>
<target name="rename" depends="lessc">
<echo>renaming...</echo>
<rename src="${basedir}/src/main/webapp/css/cons.css.dsp" dest="${basedir}/src/main/webapp/css/core.css"/>
<delete file="${basedir}/src/main/webapp/css/cons.css.dsp.src"/>
<echo>rename done</echo>
</target>
<target name="trasition" >
<echo> in trasition </echo>
</target>
</project>
When I run this, it comes into the error like this:
Buildfile: C:\Users\di_yu\workspace\lessCompiler\build.xml
css.concatenate:
[echo] cons is done!
lessc:
[echo] now in compression
[java] Compiling... C:\Users\di_yu\workspace\lessCompiler\src\main\webapp\cons\cons.less
css.concatenate:
[echo] cons is done!
css.concatenate:
[echo] cons is done!
lessc:
[echo] now in compression
[java] Compiling... C:\Users\di_yu\workspace\lessCompiler\src\main\webapp\cons\cons.less
rename:
[echo] renaming...
[rename] DEPRECATED - The rename task is deprecated. Use move instead.
[delete] Deleting: C:\Users\di_yu\workspace\lessCompiler\src\main\webapp\css\cons.css.dsp.src
[echo] rename done
trasition:
[echo] in trasition
BUILD FAILED
Target "precompile-templates" does not exist in the project "compile".
Total time: 4 seconds
As you can see, it says "precompile-templates" target is not exist. However, I didn't call this task at all. I just delete it from my build.xml and there are no other targets related to it. How does the error come? PS: I use eclipse's run as ANT to run this build.xml.
Did you change the target you want to run after you deleted the unnecessary targets from the file? It looks to me like the run configuration is still using the old target. Use
The problem is that you created a target "precompile-templates", you subsequently deleted it ... and Eclipse is "remembering" the now-obsolete target.
SUGGESTION:
1) "File > Save all", 2) "Project > Clean project" 3) "Run As, Ant build"
See if that works!
Actually, I found out the problem. I still use the old configuration to run the build.xml. The solution is like this:
right click on the build.xml
click 'run as...' then click the external configuration tool
delete the selected launch configurations.
Para aquel que le interese
buil.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="less.compile" default="css.concatenate" basedir=".">
<!-- compile LESS -->
<target name="css.concatenate">
<concat destfile="${basedir}/WebContent/cons/index.less">
<fileset dir="${basedir}/WebContent/less/import">
<include name="*.less"/>
</fileset>
</concat>
<echo>cons is done!</echo>
</target>
<target name="css.lessc">
<java classname="CpLess" fork="true">
<arg value="${basedir}/WebContent/cons"/><!-- output folder -->
<arg value="${basedir}/WebContent/less"/><!-- output folder -->
<classpath>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/commons- logging.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/js.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/lesscss-engine-1.3.3.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/yuicompressor-2.4.7.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zkjszips.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zkless.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zul.jar"/><!-- only needed if using _zkmixins.less -->
</classpath>
</java>
</target>
<target name="lessc">
<java classname="CpLess" fork="true">
<arg value="C:/WKSLuna000/PruebasZul/WebContent/lesssrc"/><!-- output folder -->
<arg value="C:/WKSLuna000/PruebasZul/WebContent/lessbuild"/><!-- output folder -->
<classpath>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/commons-logging.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/js.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/lesscss-engine-1.3.3.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/yuicompressor-2.4.7.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zkjszips.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zkless.jar"/>
<pathelement location="${basedir}/WebContent/WEB-INF/lib/zul.jar"/><!-- only needed if using _zkmixins.less -->
</classpath>
</java>
</target>
</project>
En la carpeta cons/
/zk/
_header.less
_zkminis.less
_skvariables.less
En la carpeta less/
/import/
0index.less
button.less
....
/zk/
_header.less
_zkminis.less
_skvariables.less
Archivo 0index.less:
#import "../bootstrap/less/variables.less";
#import "../bootstrap/less/mixins.less";
#import "zk/_header.less";

Prepend to ant classpath instead of append

Below is a sample ant file for the purpose of this question.
Target "test1" is called from "test" using antcall. Now, when I have some classes having same fully qualified class names, I want the classes from test1.jar to be loaded first but it looks like test1.jar is appended to the classpath and hence classes from test.jar are picked instead.
Is there any way to reset/clear the classpath for target test1 or prepend test1.jar to the classpath so that classes from it are loaded.
<project name="TestProj" basedir=".">
<target name="test">
<java inputstring="" fork="true" failonerror="true" dir=".">
<classpath>
<fileset dir="C:/bb/" includes="test.jar"/>
</classpath>
</java>
</presetdef>
<antcall target="test1"/>
</target>
<target name="test1">
<java inputstring="" fork="true" failonerror="true" dir=".">
<classpath>
<path location="c:/test1.jar"/>
</classpath>
<jvmarg line="-Xms64m -Xmx512m"/>
<jvmarg line="-Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true"/>
</java>
</presetdef>
</target>
</project>
Try the following:
<path id="test.classpath">
<fileset dir="C:/bb/" includes="test.jar"/>
<path refid="compile.classpath"/>
</path>
<path id="test1.classpath">
<pathelement location="c:/test1.jar"/>
</path>
<project name="TestProj" basedir=".">
<target name="test">
<java inputstring="" fork="true" failonerror="true" dir=".">
<classpath>
<path refid="test.classpath"/>
</classpath>
</java>
</presetdef>
<antcall target="test1"/>
</target>
<target name="test1">
<java inputstring="" fork="true" failonerror="true" dir=".">
<classpath>
<path refid="test1.classpath"/>
</classpath>
<jvmarg line="-Xms64m -Xmx512m"/>
<jvmarg line="-Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true"/>
</java>
</presetdef>
</target>

Regarding ant: how i can give few aruments to main function?

Here is my "run" in my ant build file:
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
I want to run it like this:
ant run -Darg0=First.txt -Darg1=Second.txt -Darg2=Third.txt -Darg3=Fourth.txt -Darg4=Fifth.txt
What changes should i make in my "run"?
Many thanks for help!
Here's the ant task to run any program (including, but not limited to Java programs):
<target name="run">
<exec executable="name-of-executable-file">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</exec>
</target>
Here's the task to run a Java program from a .jar file:
<target name="run-java">
<java executable="path for jar">
<arg value="${arg0}"/>
<arg value="${arg1}"/>
</java>
</target>
You can invoke either from the command line like this:
ant -Darg0=Hello -Darg1=World run
Updated task
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
<arg value="${arg0}" />
<arg value="${arg1}" />
</java>
</target>
Check out the <java> task documentation
Use nested <arg> and <jvmarg> elements to specify arguments for the
Java class and the forked VM respectively.
and the <arg> subtask

java ant buildfile for project with dependencies

I am new to ant, but am trying to create an ant script that builds my current proeject with another project as a dependency. I have the ant script building my current project, but am unsure how to add the other project to the classpath. Neither project get put into jar files at the moment.
My current portion of the build.xml file is
<target name="run" depends="compile">
<java classname="com.mypackage.Main">
<classpath>
<pathelement location="../project1/out"/>
<pathelement location="${bin}"/>
</classpath>
</java>
</target>
Thanks for your help!
I was able to do it using
<target name="run" depends="compile">
<java classname="com.mypackage.Main" fork="true">
<classpath>
<dirset dir="${other.dir}">
<include name="out/**"/>
</dirset>
<pathelement location="${bin}" />
</classpath>
</java>
</target>
Where ${other.dir} is the relative path to the root of the other project.

Categories