Only generate source files if definitions have changed ant - java

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.

Related

Ant exec command argument dealing with multiple files

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>

Display source .java files using less utility and ant

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>

How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse

I'm working on a small library for our in-company use, and have been heavily documenting it. Now I'm building my jar with the following code:
<project name="commonutils" default="compile" basedir=".">
<property name="src" location="src" />
<property name="build" location="buildDirecotry" />
<target name="compile">
<delete file="${ant.project.name}.jar" />
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" debug="on" target="1.5">
<classpath>
<pathelement location="lib/build/server.zip" />
<pathelement path="${java.class.path}/"/>
</classpath>
</javac>
<jar basedir="${build}" destfile="${ant.project.name}.jar" />
<delete dir="${build}" />
</target>
</project>
Which works fine, it builds my jar file with all the src files in it, but when I include the jar file in another project I no-longer have any of my javadoc comments. Using JDDecompiler I cannot see the comments in the class file, although I'm not sure if its the java compiler that's stripping them or JD.
My question is: How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse.
If you include the source files in the jar (each class and java file in the same package-directory) it should work.
<target name="jar.noCompile.src">
<jar destfile="${ant.project.name}.jar">
<fileset dir="${build}"/>
<fileset dir="${src}" includes="**/*.java"/>
</jar>
</target>
AFAIK the documentation is an Eclipse feature. You have to configure it manually. In your build generate the documentation (usually into folder 'javadoc') and package it with the JAR. Once someone wants to use your library, he/she has to go into Java Build Path select libraries, add yours, click next to it to open the tree node and then double click on Javadoc location to configure it.

JSP compilation and Weblogic 10.3.4

We are in the process of upgrading from BEA Weblogic and ALSB to Oracle Weblogic and OSB 10.3.4. One of the things that are failing is our ant task for compiling jsp pages for our WAR.
This is the part of the ant build file that is failing now:
<!-- Compile the JSP files -->
<path id="workshop.classpath">
<fileset dir="${env.WL_HOME}/workshop/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${env.ORACLE_HOME}/tools/eclipse_pkgs/1.1/pkgs/eclipse/plugins/com.bea.workshop.wls.ant_1.0.20.200802230117/workshop-wls-antlib.jar"/>
</path>
<taskdef
name="jspc"
classname="com.bea.workshop.wls.antlib.tasks.JspcTask" classpathref="workshop.classpath"/>
<path id="jspc.classpath">
<path refid="java.classpath"/>
<pathelement location="${env.ORACLE_HOME}/jdk150_11/lib/tools.jar" />
<pathelement location="${env.WL_HOME}/server/lib/weblogic.jar" />
</path>
<jspc source="#{war.staging.dir}" classpathref="jspc.classpath" failonerror="true" />
It cannot resolve the jar file with JspcTask since Workshop does not exist in 10.3.4. So the question is, how should this ant script be changed to work under 10.3.4?
The taskdef with the classname below works with WLS 10.3.3
<taskdef name="jspc"
classname="weblogic.ant.taskdefs.j2ee.Jspc" classpath="{ORACLE_MIDDLEWARE_HOME}\wlserver_10.3\server\lib\weblogic.jar">
</taskdef>
Check your weblogic.jar with winzip/winrar to see if the above classname exists in 10.3.4, and it should be fine.
Update:
You're getting the NullPointer because jspc does not allow a directory in the source attribute. It should be srcdir as per the docs, in fact I dont see a source attribute at all.
The jspc task is deprecated, so I think it's better you use the weblogic.appc like below
<target name="compileapp">
<java classname="weblogic.appc" fork="yes">
<arg line="-webapp ${src.gui}" />
<arg value="-compiler javac" />
<arg line="-classpath D:\Oracle\Middleware_Jdev11_1_1_4\wlserver_10.3\server\lib\weblogic.jar" />
<arg value="-depend" />
<arg value="-nowarn" />
</java>
</target>
Choose the attributes you want from this list, I havent tried this myself yet.

How to use wildcard in Ant's Available command

I'm using an Ant build script to collate my Eclipse-based application for distribution.
One step of the build is to check that the correct libraries are present in the build folders. I currently use the Ant command for this. Unfortunately, I have to amend the script each time I switch to a new Eclipse build (since the version numbers will have updated).
I don't need to check the version numbers, I just need to check that the file's there.
So, how do I check for:
org.eclipse.rcp_3.5.0.*
instead of:
org.eclipse.rcp_3.5.0.v20090519-9SA0FwxFv6x089WEf-TWh11
using Ant?
cheers,
Ian
You mean, something like (based on the pathconvert task, after this idea):
<target name="checkEclipseRcp">
<pathconvert property="foundRcp" setonempty="false" pathsep=" ">
<path>
<fileset dir="/folder/folder/eclipse"
includes="org.eclipse.rcp_3.5.0.*" />
</path>
</pathconvert>
</target>
<target name="process" depends="checkEclipseRcp" if="foundRcp">
<!-- do something -->
</target>
A slightly shorter and more straightforward approach with resourcecount condition:
<target name="checkEclipseRcp">
<condition property="foundRcp">
<resourcecount when="greater" count="0">
<fileset file="/folder/folder/eclipse/org.eclipse.rcp_3.5.0.*"/>
</resourcecount>
</condition>
</target>
<target name="process" depends="checkEclipseRcp" if="foundRcp">
<!-- do something -->
</target>
The pathconvert task is probably the preferred way to go in most cases. But it creates a little problem when the directory tree is very large and one uses the echoproperties task. With a very large directory tree, the string generated by pathconvert can be huge. Then echoproperties sprays the huge string, making the output more difficult to work with. I use a macrodef on Linux that creates a property set to "1" if there are files in the directory:
<macrodef name="chkDirContents" >
<attribute name="propertyName" />
<attribute name="dirPath" />
<attribute name="propertyFile" />
<sequential>
<exec executable="sh" dir="." failonerror="false" >
<arg value="-c" />
<arg value='fyles=`ls -1 #{dirPath} | head -1` ; if [ "$fyles" != "" ] ; then echo #{propertyName}=1 > #{propertyFile} ; fi' />
</exec>
</sequential>
</macrodef>
<target name="test" >
<tempfile destdir="." property="temp.file" deleteonexit="true" />
<chkDirContents propertyName="files.exist" dirPath="./target_dir" propertyFile="${temp.file}" />
<property file="${temp.file}" />
<echoproperties/>
</target>
Executing the "test" target will generate the following echoproperties line if there are files in the ./target_dir/ directory:
[echoproperties] files.exist=1
What "test" does:
It generates a temporary filename, ${temp.file}, that can later be used as a property file.
It then executes the macrodef, which calls the shell to check the contents of the dirPath directory. If there are any files or directories in dirPath, it assigns the propertyName property a value of 1 in the temporary file. It then reads the file and sets the property given in the file. If the file is empty, no property is defined.
Note that the temporary file could be reused for subsequent calls of the macrodef if desired. On the other hand, of course, once a property is set, it is immutable.

Categories

Resources