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>
Related
I am learning how to use ant as well as making a .xml file.
I want to create a target, name="display" for instance, that implements the 'less' utility to display the source .java files in the current folder srcdir=".".
The call on the command line is pretty much ant display.
Thank you.
Quick solution:
<target name="display">
<concat>
<fileset dir="${src}" includes="**/*.java"/>
</concat>
</target>
EDIT: if you want to use less try the following
<target name="display">
<concat destfile="java.concat">
<fileset dir="${src}" includes="**/*.java"/>
</concat>
<exec executable="less">
<arg value="java.concat" />
</exec>
</target>
I run a script that generates some java code based on definition files. I want to avoid running this task if the definition files have not changed.
<target name="generate" depends="init">
<exec executable="${codeGenTool-path}">
<arg value="${definitionFolder}" />
<arg value="${generatedFolder}" />
</exec>
</target>
I looked at http://ant.apache.org/manual/Tasks/uptodate.html but It seems like I must have a single target file to compare to. The code generation tool creates a folder containing many source files.
This is a good use case for the outofdate task from ant-contrib:
<outofdate>
<sourcefiles>
<fileset dir="${definitionFolder}" />
</sourcefiles>
<targetfiles>
<fileset dir="${generatedFolder}" />
</targetfiles>
<sequential>
<exec executable="${codeGenTool-path}">
<arg value="${definitionFolder}" />
<arg value="${generatedFolder}" />
</exec>
</sequential>
</outofdate>
This will check every file under the definitionFolder against every file under the generatedFolder - you might want to constrain the filesets more tightly, e.g. with includes="**/*.def" or whatever is the relevant file extension.
Alternatively, if you want to avoid "third party" tasks then you could use a dependset task to check the target files against the source ones.
<target name="generate" depends="check.generate, do.generate" />
<target name="check.generate">
<dependset>
<srcfileset dir="${definitionFolder}" />
<targetfileset dir="${generatedFolder}" />
</dependset>
<condition property="gen.required">
<resourcecount count="0" when="equal">
<fileset dir="${generatedFolder}" />
</resourcecount>
</condition>
</target>
<target name="do.generate" if="gen.required">
<exec ....>
</target>
The dependset task deletes all the target files if any of them are older than any of the source files, so we can make do.generate conditional - it will run if there are no files in the generatedFolder, which will be the case when either it's never been run before, or the generated files were out of date.
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've followed http://www.eclipse.org/equinox/documents/quickstart-framework.php but it seems to be old and not valid.
There is no such bundles as described org.eclipse.update.configurator_3.2.100.jar
I tried with the org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503, but doesn't work.
Anyone can tell me how to make Equinox auto load the bundles inside plugins folder?
Simplest approach would be to use Apache Felix File Install. It works just fine with Equinox, you only need to put File Install configuration parameters into the configuration/config.ini. Note though that if you launch Equinox via launcher JAR or via binary, the working directory would be parent of configuration/ or plugins/ directory.
Excerpt from our project config.ini:
# Start File Install itself
osgi.bundles=reference\:file\:org.apache.felix.fileinstall_3.1.0.jar#1\:start
# The name of the directory to watch
felix.fileinstall.dir=./plugins
# A regular expression to be used to filter file names
# We have all bundles in plugins/ directory, this regexp
# forbids monitoring bundles that are started via osgi.bundles property
felix.fileinstall.filter=^(?!org.apache.felix.fileinstall|org.eclipse.osgi).*
# Determines if File Install waits felix.fileinstall.poll milliseconds before doing an initial scan or not.
felix.fileinstall.noInitialDelay=true
# Not sure why we have this...
felix.fileinstall.start.level=2
Other possible solution would be to use Eclipse P2. It is much more advanced and powerful, though I find it quite difficult to use.
Good thing is that if your application is agnostic to the way bundles are provisioned (and it should be this way), you can always change your mind later.
Here is the fragment from my automated eclipse installer written in ant.
This installs all features from the custom update site. The code is 'as is', but I sure would have liked to have something like this to guide me when I wrote it.
This script also uses antcontrib extension to ant. Antcontrib tasks are have 'ac:' namespace prefix
Hope this helps.
<property name="real.eclipse.home" location="${eclipse.home}/eclipse"/>
<property file="${real.eclipse.home}/configuration/config.ini" prefix="ECLIPSE_CONFIG"/>
<property name="eclipse-plugins.dir" location="${real.eclipse.home}/plugins"/>
<path id="newest.equinox.launcher-library.path.id">
<dirset dir="${eclipse-plugins.dir}">
<include name="org.eclipse.equinox.launcher.*"/>
</dirset>
</path>
<property name="equinox.launcher-library.full-path" refid="newest.equinox.launcher-library.path.id"/>
<basename property="equinox.launcher-library.dir" file="${equinox.launcher-library.full-path}"/>
<echo message="equinox.launcher-library.dir='${equinox.launcher-library.dir}'"/>
<path id="newest.equinox.launcher.path.id">
<fileset dir="${eclipse-plugins.dir}">
<include name="org.eclipse.equinox.launcher_*.jar"/>
</fileset>
</path>
<property name="equinox.launcher.jar" refid="newest.equinox.launcher.path.id"/>
<basename property="equinox.launcher.jar.basename" file="${equinox.launcher.jar}"/>
<echo message="equinox.launcher.jar='${equinox.launcher.jar}'"/>
<java jar="${equinox.launcher.jar}"
fork="true"
failonerror="true"
>
<arg value="-consolelog"/>
<arg value="-application"/>
<arg value="org.eclipse.equinox.p2.director"/>
<arg value="-repository"/>
<arg value="http://${repository.server}/custom-update-site"/>
<arg value="-list"/>
<redirector
logError="true"
outputproperty="features.list"
>
<outputfilterchain>
<linecontains>
<contains value="feature.group="/>
</linecontains>
<replaceregex pattern="(.*feature\.group)=.*$" replace="\1"/>
</outputfilterchain>
</redirector>
</java>
<ac:for list="${features.list}" delimiter="${line.separator}" trim="true" param="feature">
<sequential>
<ac:if>
<isset property="feature.comma.list"/>
<then>
<ac:var name="feature.comma.list" value="${feature.comma.list},#{feature}"/>
</then>
<else>
<property name="feature.comma.list" value="#{feature}"/>
</else>
</ac:if>
</sequential>
</ac:for>
<echo message="Found following features to install"/>
<echo message="${features.list}"/>
<java jar="${equinox.launcher.jar}"
fork="true"
failonerror="true"
>
<arg value="-consolelog"/>
<arg value="-application"/>
<arg value="org.eclipse.equinox.p2.director"/>
<arg value="-repository"/>
<arg value="http://${repository.server}/custom-update-site"/>
<arg value="-destination"/>
<arg file="${real.eclipse.home}"/>
<arg value="-installIU"/>
<arg value="${feature.comma.list}"/>
<arg value="-profile"/>
<arg value="${ECLIPSE_CONFIG.eclipse.p2.profile}"/>
</java>
P.S. For its usefulness and complexity Eclipse P2 is surely one of the most underdocumented features.
In your eclipse installation folder you have the file bundles.info, for example:
eclipse-3.6.1/configuration/org.eclipse.equinox.simpleconfigurator/bundles.info
You can modify the file to add any bundle you want, and also the start level. But the simplest method of adding bundles to an eclipse installation is to add them to the "dropins" folder. This will lead to an automatic modification of the bundle.info file.