在构建时在项目的所有文件中强制执行编码标准
我知道 Stackoverflow 中也有一些类似的问题,但它们要么与 .Net 相关,要么没有任何对我们有帮助的答案。
案例如下:我们和一些朋友正在启动一个开源项目。在为希望成功的项目奠定基础时,出现了一个问题:如何强制执行项目的代码约定?
原因是,作为一个开源项目,如果人们开始按照自己的喜好重新格式化代码,则补丁将因格式更改而变得混乱,从而隐藏补丁的真正“价值”。因此,我们希望有一些东西可以强制用户遵守特定的格式,如果他们不这样做,就会破坏构建。
该项目使用 Struts 2 + Spring + Hibernate,使用 Maven 2(正在考虑迁移到 Maven 3)。我们知道我们可以使用“CheckStyle”来测试 Java 文件,但这留下了一些问题,希望有人能够回答:
- 有一些工具可以检查 XML 和 SQL 文件的样式,如果它们不遵守,就会破坏构建规则?
- 有一些工具可以自动将源文件(Java、XML、SQL)重新格式化为所需的约定吗? ti 可以以某种方式与 Maven 集成吗?
我们可以找到 Jalopy for Java 文件,但我们更喜欢免费工具(据我们所知,最新版本是付费工具)。我们仍然找不到 SQL/XML 的任何内容。
更新:只是为了澄清:我不是在询问 PMD、Checkstyle 或这些工具。我要求的工具能够:
- 自动将 SQL、XML 和/或 Java 文件格式化为所需的编码约定(4 个空格等)
- 检测格式错误的 SQL 和 XML 文件并因此破坏构建。
I know there are some similar questions around in Stackoverflow, but they were either .Net related or didn't have any answer that helped us.
The case is as follows: with some friends we are starting an open source project. While setting the foundations of the hopefully successful project, a question arose: how to enforce the code conventions of the project?
The reasoning is that being an open source project, if people starts reformatting the code as they like, the patches will become cluttered with changes due to formatting that will hide the real "value" of the patch. So we want something that forces users to abide to a specific formatting, breaking the build if they don't.
The project is using Struts 2 + Spring + Hibernate, using Maven 2 (thinking on moving to Maven 3). We know we can use "CheckStyle" to test the Java files, but this leaves some questions open that hopefully someone can answer:
- There is some tool to check the style of XML and SQL files, breaking the build if they don't abide to the rules?
- There is some tool that automatically reformats the source files (Java, XML, SQL) to the desired convention? Can ti be integrated with Maven somehow?
We could find Jalopy for Java files, but we would prefer a free tool (as far as we know, the latest version is paid one). And we still couldn't find anything for SQL/XML.
UPDATE: just to make it clear: I'm not asking about PMD, Checkstyle or these tools. I'm asking for tools that:
- Automatically format SQL, XML and/or Java files to a desired coding convention (4 spaces, etc)
- Detect SQL and XML files with the wrong format and break a build because of that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用于 java 的标准工具是
所有这些工具都能够在达到某个阈值时使 Maven 构建失败。
但问题当然是:谁运行这些工具,何时运行?
我的建议是获得一个像 hudson 这样的持续集成服务器,并将其配置为运行 maven checkstyle、findbugs 和 pmd目标(Hudson 有插件来显示所有这些工具的图表),并且您将始终看到谁失败了构建,因为您可以看到谁在两个构建之间提交了哪些内容。
The standard tools I would use for java are
All of these are capable of failing a Maven build when a certain threshold was reached.
But the question is of course: who runs those tools, and when?
My suggestion would be to get a Continuous Integration Server like hudson and configure it to run the maven checkstyle, findbugs and pmd goals (hudson has plugins to display graphs for all these tools), and you will always see who failed the build, because you can see who committed what between two builds.
在我看来,您正在沿着创建开源项目然后向所有人授予提交访问权限的路线。我建议不要这样做,无论你尝试采取什么措施,总会有某个愚蠢的人出现并把整个事情搞砸。
相反,我建议对提交访问采取不同的方法,并仅将其分发给那些证明他们关注您制定的规则和限制的人。他们可以生成补丁,将其提交给您,最终,如果它们足够定期且值得信赖,您就可以授予提交访问权限。
另一点,像 Hudson 这样的东西可能在这里有用(它也有一个 checkstyle 插件)。它不会阻止人们提交垃圾代码,但它可以创建一个构建并经常运行 checkstyle,在邮件上通知人们列出是否有人搞砸了代码(要么因为它无法构建,要么因为有人破坏了检查样式规则)。这并不万无一失,但这将使您能够更轻松地关注事物。
It sounds to me like you're going along the route of creating an open source project and then giving commit access to all. I'd advise against this, whatever measures you try to put in place, someone stupid will come along at some point and sod the whole lot up.
Instead, I'd advise taking a different approach with commit access and handing it out only to people that prove they pay attention to the rules and restrictions you've put in place. They can generate patches, submit them to you and eventually if they're regular and trustworthy enough, you can hand out commit access.
One other point, something like Hudson may be of use here (it has a checkstyle plugin as well.) It won't prevent people committing crap code, but it can create a build and run checkstyle every so often, notifying people on a mailing list if someone screws up the code (either because it won't build or because someone's broken checkstyle rules.) Not foolproof, but this would enable you to keep an eye on things more easily.
PMD、Checkstyle、Findbugs 等都有 Eclipse 和其他 IDE 插件。您还可以将您选择的 IDE 配置为根据您想要的样式进行格式化,然后在团队成员之间共享此配置(作为文件)。
我认为您不希望 SCM 服务器为您格式化文件,否则签入后您的本地副本可能会立即与 SCM 中的内容过时。项目中的每个人都必须习惯在提交后立即进行更新,这听起来像是一个脆弱的过程。
相反,与团队共享您的 IDE 配置(检查此配置文件将是您想要的一个好主意),然后如果有人违反该策略,则构建失败。
Each of PMD, Checkstyle, Findbugs etc have Eclipse and other IDE plugins. You can also configure your IDE of choice to format according to the style you want, and then share this configuration (as a file) among members of the team.
I don't think you want your SCM server formatting files for you, otherwise after a check-in your local copy might be immediately out of date with whats in SCM. Everyone on the project would have to get used to doing an update immediately after a commit then, which sounds like a brittle process.
Instead, share your IDE configuration with the team (checking this config file in would be a good idea for what you want) and then fail the build if anyone violates the policy.
是的,例如,由于 eclipse 有一个内置的代码格式化程序,因此您可以使用格式化插件沿着固定的配置文件格式化所有 java 代码,即使没有 eclipse 或任何其他 GUI一个构建过程。
Yes, since eclipse, for example, has a build-in code-formatter, it could be possible that you may use the formatting-plugin to format all the java-code along a fixed configuration file, even without eclipse or any other GUI in a build process.