Javadoc: Treat warnings as errors? - ant

Is it possible to treat warnings as errors when using Javadoc? In particular, I am calling Javadoc from Ant and have failonerror="true" set on my <javadoc> task, but I cannot seem to trigger this. Even though javadoc is generating warnings, I am still getting BUILD SUCCESSFUL with an exit code of 0 when Ant completes.
I would expect to be able to add something to the additionalparam attribute of the <javadoc> task to force a failure for Javadoc warnings.

I know this is old but it might still be helpful for someone looking for the answer like I was. If it doesn't work change the
<contains text="warnings"/>
to the text you see with your output.
<target name="javadoc">
<delete dir="${jDocDirectory}"/>
<mkdir dir="${jDocDirectory}"/>
<record name="javadocLog" action="start"/>
<javadoc (settings, blah blah) />
<record name="javadocLog" action="stop"/>
<condition property="javadoc.warnings">
<isfileselected file="javadocLog">
<contains text="warnings"/>
</isfileselected>
</condition>
<fail if="javadoc.warnings">Javadoc warnings!</fail>
</target>
edit:
If you have one warning this will not work, to fix for ALL warnings you must change this:
<contains text="warnings"/>

Ant 1.9.4 now has failonwarning="true"
http://ant.apache.org/manual/Tasks/javadoc.html

I don't know about a JavaDoc parameter, I would recommend using a tool like checkstyle in your ant build for things like this. You can set it up to fail on Javadoc warnings.

Related

In ant, how does one create and use a 'library' of targets?

To eliminate redundancy in my ant build.xml files, I decided out-factor the repeated targets into mytargets.xml file, publish it to to the artifact repository, and then import it in the following way:
<import>
<url url="http://mycompany.com/artifacts/mycompany.com/mytargets/1.2.3/mytargets-1.2.3.xml"/>
</import>
There are two things I don't like about this approach:
mytargets-1.2.3.xml never appears anywhere on the disk where I can easily look at it.
I absolutely need access to http://mycompany.com/artifacts in order to do anything in the project---it completely undermines offline work.
So, I tried creating a setup target to fetch a local copy of mytargets.xml and adjusted my <import> to use this local copy.
<import file="${basedir}/antlib/mytargets/mytargets.xml"/>
However, as you have probably guessed, I cannot even execute my setup my target after adjusting my <import> in this way because the file does not yet exist:
Buildfile: /home/me/myproject/build.xml
BUILD FAILED
/home/me/myproject/build.xml:265: Cannot find /home/me/myproject/antlib/mytargets/mytargets.xml imported from /home/me/myproject/build.xml
Adding optional="true" to the <import> only defers the problem to the first target that depends upon mytargets.xml.
I looked at https://ant.apache.org/manual/Types/antlib.html, but this approach does not appears to permit you to define a <target>.
So, how does someone share bits of ant XML across multiple projects? Am I already doing it the 'one true way'? Or, is there a better way?
If you're mainly just trying to avoid download the remote copy when you have a local copy already available, you can try something like this:
<condition property="mytargets.xml.available">
<available file="${basedir}/antlib/mytargets/mytargets.xml" />
</condition>
<target name="setup" unless="mytargets.xml.available">
<get
src="http://mycompany.com/artifacts/mycompany.com/mytargets/1.2.3/mytargets-1.2.3.xml"
dest="${basedir}/antlib/mytargets"
/>
</target>
<target name="main" depends="setup">
<import file="${basedir}/antlib/mytargets/mytargets.xml" />
...
</target>
So, it seems to me that <target> is inherently local and not intended for reuse. On the other hand, <macrodef> appears intended for reuse.
Here is the 'library', mymacros.xml:
<?xml version="1.0"?>
<antlib>
<macrodef name="mymacro">
...
</macrodef>
</antlib>
Here is the client, myproject/build.xml:
<?xml version="1.0"?>
<project name="myproject">
<target name="mytarget">
<mymacro/>
</target>
<taskdef file="mymacros.xml"/>
</project>
Unlike <import> and <include>, <taskdef> will not cause the build to fail immediately if mymacros.xml is missing, which gives you the opportunity to download it.

Mark the build result as Failed OR Successful

I want to mark the build as successful when the file exist(when I mark ) OR failed when file does not exist (when I mark var name="build-result" value="FAILED" />).
I want to update build status after reading build-result OR by using "build.status" which I am updating depending on build-result as
<var name="build-result" value="SUCCEEDED" />
<echo message="Sending mail status- ${build-result}"/>
<propertyfile file="${build-status.file}">
<entry key="build.status" value="${build-result}" />
</propertyfile>
Currently I am doing in the way:
<isset variable="build-result" value="FAILED" />
</condition>
</fail>
But is giving the message as: BUILD FAILED
D:\projects\Self\AntExample\build.xml:51: isset doesn't support the "variable" attribute
which I want should be like "Build Failed as File Does not Exist"
Always refer to the on line documentation. <isset> takes the parameter property and not variable. Ant doesn't have the concept of variables -- just properties. What the <var/> task does is that it allows you to change a property once it is set. You should not be using <var> except in rare circumstances. As it says on the <var> task's page: In general, use of this task is DISCOURAGED, and the standard Ant Property should be used if possible.
In fact, now that you have <local/> in the list of standard Ant tasks, I find that you no longer really need <var/>.
If you want to fail your build if a file does not exist, look at the <available> test:
<fail message="This build is an utter and complete failure".>
<condition>
<not>
<available file="${result.file}" type="file"/>
</not>
</condition>
</fail>
<echo message="This build is a smashing success! We're so proud!"/>
The <fail> task will execute if the file ${result.file} is not found. Otherwise, the <echo> message will print.
<isset> accepts only one attribute- property and checks whether the specified property has been set or not (i.e. has been provided a value, including null)
See HERE for <isset>
what you may use is <equals> which compares the values of two arguments:
<equals arg1="${build-result}" arg2="FAILED">
See HERE for <equals>
as you've mentioned: I want to mark the build as successful when the file exist you may also want to look at the <available> task,
<available property="result" file="your_file_location" />
See HERE for <available>

How to p4add or p4edit, as appropriate, a file?

I'm using the Perforce tasks for Ant and would like to a file if it's not already known to Perforce or it if it is.
I can:
<p4add changelist='${p4.change}' failonerror='false'>
<fileset file='my-file'/>
</p4add>
<p4edit changelist='${p4.change}'>
<fileset file='my-file'/>
</p4edit>
Is there a better way?
<trycatch property='my-file-is-new'><try>
<p4edit view='my-file'/>
</try><catch>
<touch file='my-file'/>
<p4add>
<fileset file='my-file'/>
</p4add>
</catch></trycatch>
<if><isset property="my-file-is-new"/><then>
<echo message="p4 added my-file"/>
</then><else>
<echo message="p4 edited my-file"/>
</else></if>
The p4fstat task can filter based on a file's status in the repository. It seems like a cleaner approach but may take more lines of Ant script.

ANT: How to read property setted in a foreach loop

Dear, I currently face some problem to retrieve the value of a property setted in a foreach loop. Maybe one of you could help me...
The purpose is to check if one file of a folder has been modified since the corresponding jar has been generated. This way I know if I have to generate the jar again.
What I do is to go through the folder with a foreach loop and if one file match my test, set a property to true.
The problem is that my variable doesn't seems to exist after my loop... Here is a simplified code example that has the same problem:
<target name="target">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${lib.dir}/ant-contrib.jar"></taskdef>
<foreach target="setVar" param="var" list="a,b"/>
<echo>myreturn in target: ${env.myreturn}</echo>
<property name="env.myreturn" value="c"/>
<echo>myreturn in second: ${env.myreturn}</echo>
</target>
<target name="setVar">
<property name="env.myreturn" value="${var}"/>
<echo>myreturn in setVar: ${env.myreturn}</echo>
</target>
The result of this code is:
target:
setVar:
[echo] myreturn in setVar: a
setVar:
[echo] myreturn in setVar: b
[echo] myreturn in target: ${env.myreturn}
[echo] myreturn in second: c
BUILD SUCCESSFUL
It seems that the variable is correctly set as it could be printed in the "setVar" target but no way to retrieve value from the calling target.
I also know it's not possible to assign a value to a property twice. But the problem doesn't even occurs... When it'll be the case I could add a check on the value of the property before to assign it to be sure it is not already initialized...
Do you have a clue on the way I can solve my problem ???
Many thanks in advance for your help :)
Try <for> task from ant-contrib instead of <foreach>. The <for> task takes advantage of Ant macro facility that came later. It works faster and is more flexible than the older <foreach> task. You are in the same project context when using <for>. That means properties set in the loop will be visible outside of the loop. Of course, normal rules for properties apply... you only get to set it once... unless you use <var> task from ant-contrib to overwrite or unset previously set properties.
Ah the joys of Ant hacking.
Not sure about your foreach problem, but can you not use the uptodate task for your requirement?
Even if I don't need it anymore thanks to sudocode, I found a solution for my question. Maybe it could be useful for someone else...
A collegue talked about the "antcallback" target of ant-contrib: it allows to return a result from a called target to the calling one. With a combination of "for" target and "antcallback" it is possible to do what I wanted to do:
<target name="target">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${lib.dir}/ant-contrib.jar"></taskdef>
<for param="file">
<path>
<fileset dir="../myDirectory" includes="**/*" />
</path>
<sequential>
<antcallback target="setVar" return="retValue">
<param name="file" value="#{file}"/>
</antcallback>
</sequential>
</for>
<echo>result: ${retValue}</echo>
</target>
<target name="setVar">
<property name="retValue" value="${file}"/>
</target>
"file" contains the name of the file in the directory. It is given to the called target as parameter with value "#{file}" ('#' necessary due to "for" target implementation).
At the end of the main target, ${retValue} contains the first value setted by the "setVar" target. No error is thrown when trying to set it multiple times, so it's not necessary to check if variable has already been instantiated before to set it in "setVar" target.
The <foreach> task uses the same logic as <antcall> under the covers, and any proprrties set inside a target invoked by <antcall> do not have scope beyond the execution of that target.
In other words, the env.myreturn property that you define in the setVar target is lost as soon as execution of that target completes.
This sort of scripting really isn't what Ant is designed for. The Ant-contrib library tries to patch up the holes, but it's still bending it way out of shape.
If you need to write such scripts, and want to use Ant tasks to achieve them, have a look at Gradle instead. It's a rather lovely blend of Groovy (for scripting) and Ant (for the tasks).
The other approaches here (<for>, <var>, <groovy>properties.put(....)</groovy>, <property>, <antcallback>) did not work with ANT 1.9.4, so I used the file system similar to this (pseudocode):
<target name="outer">
<for> <antcall target="inner" /> </for>
<loadproperties srcfile="tmpfile.properties" />
<echo message="${outerprop}" />
</target>
<target name="inner">
<!-- did not work: -->
<!--
<property name="outerprop" value="true" />
<var name="outerprop" value="true" />
<groovy>properties.put('outerprop','true')</groovy>
<antcallback target="setouterprop" />
-->
<echo message="outerprop=true" file="tmpfile.properties" />
</target>
Maybe the other approaches did not work because of my <antcall>, but I need it here. (outerprop is initially unset)

Make Ant's delete task fail when a directory exists and is not deleted but not when it doesn't exist at all

I have the following clean function in my build script and I'd like to know how I can improve it.
<target name="clean" description="Clean output directories.">
<!-- Must not fail on error because it fails if directories don't exist.
Is there really no better way to do this? -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${main.build.directory}" />
<fileset dir="dist" />
<fileset dir="${documentation.build.directory}" />
<fileset dir="/build-testing" />
</delete>
</target>
Specifically regarding my comment, I'm unhappy with the fact that I can't run this on a fresh box because the directory structure hasn't been set up yet by the other targets. We run the build in such a way that it entirely recreates the structures necessary for testing and deployment every time to avoid stale class files and such. With the way that delete currently is set up, a failure to delete a file does not fail the build and I'd like it to. I don't want it to fail the build if the file doesn't exist though. If it doesn't exist then what I'm asking it to do has already happened.
Thoughts?
via Michael's answer, which was 90% of what I needed but not quite all the way there.
The actual solution that I ended up with because of your answers is the following:
<target name="clean" description="Clean output directories.">
<!-- Must not fail on error because it fails if directories don't exist.
Is there really no better way to do this? -->
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${main.build.directory}" />
...
</delete>
<available
file="${main.build.directory}"
type="dir"
property="delete-main-failure" /> ...
<condition property="delete-failure">
<and>
<isset property="delete-main-failure" /> ...
</and>
</condition>
<fail
if="delete-failure"
message="Unable to delete previous build's directories." />
</target>
This meets my criteria that the code attempts to delete it and then fails if it still exists. It's super ugly though. The default behavior of the delete task strikes me as very odd. I suppose the rationale is that if you try to delete something and it isn't there then something must be wrong but it seems to me that the normal case would be that if it's not there you don't care because it's gone already while the odd case is that you needed it to be there but now it shouldn't be anymore at this specific stage in the build.
I came here to ask the same question... it doesn't look like there is an elegant way to solve this. When I want to keep the code clean, I do it this way:
<mkdir dir="${main.build.directory}" />
<delete dir="${main.build.directory}" failonerror="true" />
I didn't think the delete task had an "if" property. Will have to check that out.

Resources