如何使用 ant 检查 java 源代码中的标签(TODO:等)

发布于 2024-08-10 01:23:56 字数 438 浏览 11 评论 0原文

在代码中经常会看到这样的东西,希望仅在开发过程中:

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

我希望 ant 能够 a) 警告(在 TODO: / FIXME: 标签上)或失败(在 XXX: 或类似的情况下)
构建服务器是Linux,自制的,基于ant。如果不能在 Windows 上运行,至少需要在 Linux 上运行。

如果替代方案是阻止文件提交,我们也会使用 perforce。
我们也使用 eclipse,但是我不认为你可以让它成为致命错误。 (是的,有任务视图,但是我希望能够将某些标签提升为构建破坏者)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞取 2024-08-17 01:23:56

也许你可以使用 Checkstyle
我认为有一个对 TODO 注释的检查,并且 checkstyle 可以作为 Ant 任务运行,这样你就可以实现你想要的。

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.

最初的梦 2024-08-17 01:23:56

您可以使用 ant 条件进行这些检查:

<condition property="isSourceFileOK">
    <not>
        <isfileselected file="${source}">
            <contains text="TODO" casesensitive="yes"/>
        </isfileselected>
    </not>
</condition>
<fail unless="isSourceFileOK" message="Source contains TODO!" />

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!" />
稚然 2024-08-17 01:23:56

至于 Perforce 变体,您可能需要为此编写一个触发器。有关更多信息,请参阅有关触发器的 perforce 文档信息。在您的情况下,您需要编写一个“更改内容”触发器,以便在文件提交之前查看 Perforce 服务器上的文件内容。

在触发器中,您可以使用 p4 files //depot/...@4711 来获取更改的文件列表(在本例中为 4711,但在命令行上移交给触发器)对于每个文件,您可以使用 p4 print -q //depot/path/to/file@4711 获取文件内容并扫描其中的关键字 (TODO/XXX)如果是 TODO,则可以在 stdout 上打印一条警告,并以代码 0 退出,以便提交成功;如果是 XXX,则可以以代码 1 退出,以便提交失败。

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.

动听の歌 2024-08-17 01:23:56

首先, jassuncao 是正确的;根据文档 Checkstyle 执行您所要求的操作 此处。冒着招致“不要重新发明轮子”的愤怒的风险,我也可能建议您想要完成的事情对于想要学习如何编写 Ant 任务的人来说是一个很好的问题。

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.

故事未完 2024-08-17 01:23:56

您还可以使用 Ant TODO 任务。

You could also use the Ant TODO task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文