I have a compiler (and language) I am building that is normally invoked thus:
java -jar nc.jar \
-p some/referenced/package.nc \
-p framework.nc \
source1.ns source2.ns sourceN.ns \
-o output/package.nc
I'd like to include a task in my ANT build file that invokes the compiler to compile the standard library and all test cases, but specifying each separate compiler invocation as a <java> task is painful:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<arg path="../nframework/build/n.core.nc"/>
<arg path="../nframework/n/debug/DebugPrint.ns"/>
<arg path="../nframework/n/debug/Trace.ns"/>
<arg value="-o"/>
<arg path="../nframework/build/n.debug.nc"/>
</java>
<!-- More compile steps -->
</target>
I would like to create an ANT task of some sort that can simplify this into something like:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<nc output="../nframework/build/n.debug.nc">
<link-package path="../nframework/build/n.core.nc"/>
<src>
<fileset dir="../nframework/n/debug" includes="**/*.ns"/>
</src>
</nc>
<!-- More compile steps -->
</target>
To this end, I tried macrodef:
<macrodef name="nc">
<attribute name="output"/>
<element name="link-package"/>
<element name="src"/>
<sequential>
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<!-- This doesn't do what I want -->
<link-package/>
<!-- Neither does this -->
<src/>
<arg value="-o"/>
<arg path="#{output}"/>
</java>
</sequential>
</macrodef>
I've tried several variations on the above, but each errors out with something like:
/home/jwarner/code/nlang/nc/build.xml:55: java doesn't support nested "fileset" element.
Is there a way to do this without extending ANT itself? Alternatively, would it be fairly easy to add an ant task to my compiler? I'm not terribly picky about the syntax of the final <nc> task.
I have had a similar problem in the past where the out-of-the-box Ant tasks didn't quite do what I wanted them to do. I found that it was very easy to write my own Ant task.
The documentation is concise but does a good job of explaining what you need to do.
http://ant.apache.org/manual/develop.html#writingowntask
Related
I am using ant exec command to implement the less utility to view the source code of a bunch of .java files. (I know that there are other ways to do this like using concat)
So the call ant view works if I specify only one file:
<target name="view">
<exec executable="less" dir=".">
<arg value="Main.java"/>
</exec>
</target>
But if I change my code to <arg value="*.java"/> to view all files, it actually searches for a file named *.java.
Apparently I can put a bunch of arg's for each file, but is there a way to do this with one arg ?
The * glob is expanded by the shell on Unix-likes, that's why less doesn't do it itself.
Apart from <exec> there is <apply> which works on a resource collection:
<apply executable="less" dir="." parallel="true" relative="true">
<fileset dir="." includes="*.java"/>
</apply>
You can use foreach which requires ant-contrib
<target name="view">
<foreach target="call-less" param="file">
<fileset dir="${src}" includes="**/*.java" />
</foreach>
</target>
<target name="call-less">
<exec executable="less">
<arg value="${file}" />
</exec>
</target>
I am using ANT to precompile handblebars in build time. I follow the way here http://blog.selvakn.in/2012/05/precompiling-handlerbars-tempates-with.html. And for only one target, it works very well. But then I try to invoke the target twice like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project basedir="." default="echoIt" name="precompile">
<property name="charset" value="utf-8"/>
<target name="echoIt">
</target>
<target name="precompile-templates0" depends="echoIt">
<property name="outputJS" value="../../jsp/jsp_1/js/templates0.js"/>
<property name="templatesPath" value="../../jsp/jsp_1/js/tmpl"/>
<java dir="${basedir}" failonerror="true" fork="true" jar="../../lib/js.jar">
<arg value="../../otherFiles/lib/rhino-handlebars-compiler.js"/>
<arg value="--handlebars"/>
<arg value="../../otherFiles/third-party/handlebars-v1.3.0.js"/>
<arg value="--templates"/>
<arg value="${templatesPath}"/>
<arg value="--output"/>
<arg value="${outputJS}"/>
</java>
<echo>Template Precompiled to web/js/compiled-templates.js</echo>
<echo> is now ready to compress....</echo>
</target>
<target name="precompile-templates1" depends="echoIt">
<property name="outputJS" value="../../jsp/jsp_2/js/templates1.js"/>
<property name="templatesPath" value="../../jsp/jsp_2/js/tmpl"/>
<echo> is now precompiling the second one </echo>
<java dir="${basedir}" failonerror="true" fork="true" jar="../../lib/js.jar">
<arg value="../../otherFiles/lib/rhino-handlebars-compiler.js"/>
<arg value="--handlebars"/>
<arg value="../../otherFiles/third-party/handlebars-v1.3.0.js"/>
<arg value="--templates"/>
<arg value="${templatesPath}"/>
<arg value="--output"/>
<arg value="${outputJS}"/>
</java>
<echo>Template Precompiled to web/js/compiled-templates.js</echo>
<echo> is now ready to compress....</echo>
</target></project>
The output on the console is like this:
Buildfile: F:\AffirmedNetworks\ServerAutomation\cache\subBuildTarget\precompileTarget.xml
echoIt:
precompile-templates0:
[echo] Template Precompiled to web/js/compiled-templates.js
[echo] is now ready to compress....
echoIt:
precompile-templates1:
[echo] is now precompiling the second one
[echo] Template Precompiled to web/js/compiled-templates.js
[echo] is now ready to compress....
BUILD SUCCESSFUL
Total time: 2 seconds
As you can see, the build is successful. However when I check the outputs in the expected directory, only a ‘templates0.js’ is generated in the path:jsp/jsp_1/js/. There is nothing in the path: jsp/jsp_2/js/. However, it should have on named 'templates1.js' in this folder. This is really weird. No errors occurred and the first on is generated successfully, but the second one disappeared. Can someone give me some help on this? Thanks!
the issue is in your re-use of the <property> task!
Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables. i.e. once you assign a value to an identifier using <property>, it cannot be changed. See HERE
although, you set the values to outputJS and templatesPath within both targets separately, maybe assuming/expecting local scope, that is not the case in ant.
reason: In general properties are of global scope, i.e. once they have been defined they are available for any task or target invoked subsequently
exception: it is not possible to set a property in a child build process created via the ant, antcall or subant tasks and make it available to the calling build process
therefore, the values for outputJS and templatesPath, as defined in the target precompile-templates0, are retained throughout the build.
to solve your issue, you may do either of the following:
change property names, for eg to outputJS1 and templatesPath1 in the target precompile-templates1
(if you're using Ant 1.8.0 or above) use the <local> task instead of the <property> task for declaring outputJS and templatesPath. See HERE for this.
use the <var> task of ant-contrib. See ant-contrib and var task.
I would like to start Server and the ProxyServer class simultaneously, using Ant tag, is it possible to run the wo classes?
Here is the code I tried but Ant only starts the Server class and does not do anything there after, not sure if there is away in ant to achieve this.
Appreciate your help.
<target name="pxyServer" depends="server">
<echo>Executing Target - Run ProxyServer</echo>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</target>
<target name="server">
<echo>Executing Target - RunServer</echo>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
</target>
Your targets are executed sequentially, and since the first one keeps running, the second one never gets the chance to start.
For parallel execution, you can use ant's "parallel" task:
http://ant.apache.org/manual/Tasks/parallel.html
Your modified script should probably look something like this:
<target name="startServerAndProxy">
<echo>Running server and proxy...</echo>
<parallel>
<java classname="pxy.Server">
<classpath path="staging" />
</java>
<java classname="pxy.ProxyServer">
<classpath path="staging" />
</java>
</parallel>
</target>
(Of course, if you're trying to start some third application in parallel, a client for example, then you should also include that one in the "parallel".)
UPDATE:
To start the server and the proxy each in its own console, I don't know if it can be done with the "java" Ant task, but I just tested that it can be done with "exec":
<target name="doit">
<parallel>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.Server"/>
</exec>
<exec executable="cmd" dir="staging">
<arg line="/k start java.exe pxy.ProxyServer"/>
</exec>
</parallel>
</target>
On running the following command:
ant targetname -Dk1=v1 -Dk2=v2
I want the command line parameters passed down to java, like java whatever -Dk1=v1 -Dk2=v2.
I need to access these parameters from Java code with System.getProperty or System.getenv.
What do I need to write in my ant build script to make this happen?
Or should I take some other approach altogether?
I'm not sure exactly how you want to pass these values, but there are several mechanisms:
Use <sysproperty> to pass system properties you need to set:
Use <arg> to pass command line arguments to your Java class
Use <jvmarg> to pass arguments to your Java command itself
If you fork your Java task, you can also set environment variables too. These are ignored if you don't fork the Java task
This:
$ foo=bar; java -Xlingc com.example.foo.bar -Dsys1=fu -Dsys2=barfu -arg1 -arg2 bar
Becomes:
<java classname="com.example.foo.bar"
fork="true">
<env key="foo" value="bar"/>
<sysproperty key="sys1" value="fu"/>
<sysproperty key="sys2" value="barfu"/>
<jvmarg value="-Xlingc"/>
<arg value="-arg1"/>
<arg value="-arg2"/>
<arg value="bar"/>
</java>
Hope that example helps
Not good in Ant Script but I do something like below :
<target name="execute">
<echo> Running MyClass ......... </echo>
<java classname="pkg.MyClass" classpathref="libs">
<arg value="val1" /> <!-- command line args -->
<arg value="val2" />
<arg value="val3" />
<env key="k1" value="v1" /> <!-- set environmental value -->
</java>
</target>
If you are using Eclipse, you will get suggestions in popup under java tag. I got few more like : <sysproperty/>, <syspropertyset></syspropertyset>, <jvmarg/>
Use the nested <arg> elements in your <java> task:
<java classname="test.Main">
<arg value="${k1}"/>
<arg value="${k2}"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
I am attempting to write unit tests against a series of RMI interfaces / implementations and am relying on Ant + Junit to facilitate these tests. The main problem is that I need to have the rmiregistry start up at the beginning of my Junit task and close down at the end of execution whether that be failure,error, or success. Ant script:
<project>
<target name="test">
<javac srcdir="Test/src" destdir="Test/bin" />
<junit fork="true" printsummary="true">
<batchtest>
<fileset dir="Test/test">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
Basically, just set haltonerror="false" and failonerror="false" for the junit task.
This way junit won't fail due to tests. Starting and stopping is done via the exec task. I used taskkill here to kill the rmiregistry.exe.
<exec executable="start"> <--your start command here -->
<arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures">
...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
<arg value="/IM"/>
<arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>
Another option is to use try/catch from ant-contrib, but I don't think this is needed here.