Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ant to check for tags (TODO: etc) in java source

it's common to see something like this in code, hopefully only during development:

//XXX: not in production!
String password = "hello"; // getActualPassword(...);
...
catch(Exception e) { /* TODO: Auto-generated catch block*/ }

I would like ant to be able to a) warn (on TODO: / FIXME: tags) or fail (on XXX: or simmilar)
The build server is linux, home grown and based on ant. Would need to work at least on linux if not on windows.

We also use perforce if an alternative is to block file commits.
We also use eclipse, however I don't think you can make it a fatal error. (yes, there's the tasks view, however I would like to be able to elevate certain tags to build-breakers)

like image 495
Paul Adamson Avatar asked Oct 28 '09 10:10

Paul Adamson


4 Answers

Maybe you can use Checkstyle. I think there is a check for TODO comments and checkstyle can be run as an Ant task so you might achieve what you want.

like image 68
jassuncao Avatar answered Oct 19 '22 23:10

jassuncao


You can use ant conditions for these checks:

<condition property="isSourceFileOK">
    <not>
        <isfileselected file="${source}">
            <contains text="TODO" casesensitive="yes"/>
        </isfileselected>
    </not>
</condition>
<fail unless="isSourceFileOK" message="Source contains TODO!" />
like image 40
rsp Avatar answered Oct 20 '22 00:10

rsp


As for the Perforce variant, you will likely want to write a trigger for that. See the perforce docu about triggers for more information. In your case, you'd write a 'change-content' trigger in order to see the file-content on the Perforce server before file-commit.

Within the trigger you can use p4 files //depot/...@4711 to get a list of files of the change (in this case 4711, but is handed over on the command line to the trigger. For each of the files you'd use p4 print -q //depot/path/to/file@4711 to get the content of the file and scan this for your keywords (TODO/XXX). You could print a warning on stdout in case of TODO and exit with code 0, so that the commit succeeds and exit with code 1 in the case of XXX so that the commit fails.

like image 30
jhwist Avatar answered Oct 20 '22 00:10

jhwist


First, jassuncao is correct; Checkstyle does what you are asking, according to the docs here. At the risk of incurring "don't reinvent the wheel" wrath, I might also suggest that what you are wanting to accomplish is a nice problem for someone who wants to learn how to write Ant tasks.

like image 45
joel.neely Avatar answered Oct 19 '22 23:10

joel.neely