eclipse 自动检查 Java 文件是否符合编码标准
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15707084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically check Java files for coding standard compliance
提问by 0xCAFEBABE
I'm working in Java development. I have recently come into a situation where I have to comply to coding standards: member and method ordering, naming conventions, modifier sequence. I am thinking about methods to either automate checking for compliance, or generate some sort of mechanism that does the reordering.
我从事 Java 开发。我最近遇到了一种情况,我必须遵守编码标准:成员和方法排序、命名约定、修饰符序列。我正在考虑自动检查合规性或生成某种重新排序机制的方法。
We're developing with Eclipse, but the technology would be open. One way it might work is to generate an external builder tool and add this to the projects. The disadvantage would be that it would automagically apply to all files, which could run into problems with legacy code, blowing up the error count to a degree where it is no longer a sensible metric of compliance. Also, it makes code reviews much more difficult, which is not wanted.
我们正在使用 Eclipse 进行开发,但该技术是开放的。它可能起作用的一种方法是生成一个外部构建器工具并将其添加到项目中。缺点是它会自动应用于所有文件,这可能会遇到遗留代码的问题,将错误计数增加到不再是合规性的合理度量的程度。此外,它使代码变得更加困难,这是我们不想要的。
Another way would be some kind of parser with only informative capabilities. We could run a process inside Jenkins, and that certainly would work, but that would also mean that the code had already passed a review, which is usually a little late for a code compliance check.
另一种方法是某种仅具有信息功能的解析器。我们可以在 Jenkins 内部运行一个流程,这肯定会起作用,但这也意味着代码已经通过了,这对于代码合规性检查来说通常有点晚。
Are there suggested or even easy methods to integrate such functionality into either IDE, the source control system (Mercurial) or even Jenkins? How is this enforced elsewhere?
是否有建议甚至简单的方法将此类功能集成到 IDE、源代码控制系统(Mercurial)甚至 Jenkins 中?这在其他地方是如何执行的?
采纳答案by Mateusz Chromiński
I would not recommend doing such changes automatically. Even though most of the checkstyle/pmd complies are valid, it happens to me that I need to ignore some of the warnings/errors. Moreover - there is only very small pool of such easy issues. Most of the notifications require more complex operations and probably couldn't be done without human interaction.
我不建议自动进行此类更改。尽管大多数 checkstyle/pmd 合规是有效的,但我碰巧需要忽略一些警告/错误。此外 - 只有很少的这样简单的问题池。大多数通知需要更复杂的操作,并且可能无法在没有人工交互的情况下完成。
I'm using Sonarintegration. It contains many external checkers like PMD, CPD, Checkstyle, Findbugsand can integrate with some other useful tools like Cobertura(test coverage statistics). Its almost trivial to bind Sonar build to Jenkins build and trying to avoid major/critical issues might be considered as a good approach.
我正在使用声纳集成。它包含许多外部检查器,如PMD、CPD、Checkstyle、Findbugs,并且可以与其他一些有用的工具集成,如Cobertura(测试覆盖率统计)。将 Sonar 构建绑定到 Jenkins 构建几乎是微不足道的,并且尝试避免主要/关键问题可能被认为是一种好方法。
In developer environment I use Eclipse integration with findbugs. There is also some point of integration with sonar but it requires either submitting the code to server or running server locally, which I personally don't like. However after few cycles of polishing the code after code review in Sonar you will notice that you (and other team members) stick to most of the rules and checking reports on daily basis is enough.
在开发人员环境中,我使用 Eclipse 与 findbugs 集成。还有一些与声纳的集成点,但它需要将代码提交到服务器或在本地运行服务器,我个人不喜欢这种方式。然而,在 Sonar 中进行代码后,经过几个周期的完善代码后,您会注意到您(和其他团队成员)遵守大多数规则并且每天检查报告就足够了。
回答by gareth_bowles
Further to @Jayan's answer, Jenkins has a CheckStyle pluginthat will display the results of each CheckStyle run and let you set the build status depending on how many violations are found. So your setup steps would be:
除了@Jayan 的回答之外,Jenkins 有一个CheckStyle 插件,该插件将显示每次 CheckStyle 运行的结果,并让您根据发现的违规次数设置构建状态。所以你的设置步骤是:
- Set up your CheckStyle rulesto fit your coding standards
- Add a step to your Jenkins build that runs CheckStyle
- Add a post build step to publish the CheckStyle results.
- 设置CheckStyle 规则以符合您的编码标准
- 向运行 CheckStyle 的 Jenkins 构建添加一个步骤
- 添加后期构建步骤以发布 CheckStyle 结果。
回答by Jayan
One solution is to use JCSC/ checkstyleor other tools that are command line friendly. Integrate this with your build process. Individual developer runs this on his branch.
一种解决方案是使用JCSC/ checkstyle或其他命令行友好的工具。将此与您的构建过程集成。个人开发人员在他的分支上运行它。
Most tools integrate well with Jenkins (via plugins), which can be used as dash board
大多数工具与 Jenkins 很好地集成(通过插件),可以用作仪表板
回答by Nicolas Rinaudo
Jayan mentions checkstyle, which is great for checking against coding standards.
Jayan 提到了 checkstyle,它非常适合检查编码标准。
I remember using Jalopyyears ago for automated code formatting, it might suit your needs as well.
我记得多年前使用Jalopy进行自动代码格式化,它也可能适合您的需求。
In all honesty though, I wouldn't reformat the code automatically. Using tools such as checkstyle to raise warnings is one thing. Taking control of a developer's source code is quite another, and most people find it terribly intrusive and unpleasant. Also, a bug in a code checker will at worst generate incorrect warnings. A bug in a code beautifier might corrupt and destroy hours worth of work.
老实说,我不会自动重新格式化代码。使用诸如 checkstyle 之类的工具来发出警告是一回事。控制开发人员的源代码是另一回事,大多数人都觉得它非常具有侵入性和令人不快。此外,代码检查器中的错误在最坏的情况下会产生不正确的警告。代码美化器中的错误可能会破坏和破坏数小时的工作。
回答by Dane
You can use JArchitectto check your best practice using CQLinq queries which is useful to create easly your custom rules
您可以使用JArchitect使用 CQLinq 查询来检查您的最佳实践,这对于轻松创建自定义规则很有用
JArchitect is free for open source contributors :http://www.jarchitect.com/JArchitectForOSS.aspx
JArchitect 对开源贡献者免费:http: //www.jarchitect.com/JArchitectForOSS.aspx