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

强颜欢笑 提交于 2019-12-22 06:48:40

问题


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)


回答1:


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.




回答2:


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!" />



回答3:


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.




回答4:


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.




回答5:


You could also use the Ant TODO task.



来源:https://stackoverflow.com/questions/1636520/how-to-use-ant-to-check-for-tags-todo-etc-in-java-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!