简体   繁体   English

在Netbeans / Eclipse中格式化Java代码,但以不同方式保存

[英]Format Java Code in Netbeans / Eclipse, but save it differently

I asked a related question before, but I guess the root of the question is. 我之前问了一个相关的问题,但我想问题的根源是。 Let's say I have 2 developers on the team and they both like to look at code in different formats. 假设团队中有2名开发人员,他们都喜欢看不同格式的代码。 One likes the braces to be on a new line and the other doesn't. 人们喜欢在新线上使用大括号,而另一个则不喜欢。

The approach I was using before is that anytime we run a build, the code is automatically formatted according to the Java/Sun standards using Jalopy; 我之前使用的方法是,无论何时运行构建,代码都会根据Java / Sun标准使用Jalopy自动格式化; however, I would like the developers to be as happy as possible. 但是,我希望开发人员尽可能快乐。 They can change the font size, font color, background color, etc. 他们可以改变字体大小,字体颜色,背景颜色等。

If I am currently using the Jalopy Maven plugin to format code, can/should I write a hook to SVN that calls mvn jalopy:format on the project when it's checked in? 如果我目前正在使用Jalopy Maven插件来格式化代码,是否可以/我应该在SVN上写一个挂钩,在项目签入时调用mvn jalopy:format? Is this reliable? 这可靠吗?

That solution doesn't work 100% because it requires the developer to manually format the source code to their liking every time they open a file that hasn't been formatted yet. 该解决方案不能100%工作,因为它要求开发人员在每次打开尚未格式化的文件时手动格式化源代码。 I was thinking an IDE plugin would be nice as it could automatically format the source to their liking and then save it as another. 我认为IDE插件会很好,因为它可以根据自己的喜好自动格式化源,然后将其保存为另一个。

What other options do I have to ensure the code is formatted nicely on checkin? 还有什么其他选项可以确保代码在签入时格式良好?

Thanks, 谢谢,

Walter 沃尔特

IMHO you are wasting your resources with this. 恕我直言,你是在浪费你的资源。 Code format is (between certain limits) a question of taste; 代码格式是(在某些限制之间)品味问题; howewer, people working as a team should be able to agree on using a common coding style. 但是,作为一个团队工作的人应该能够同意使用通用的编码风格。

Once someone has worked on a couple of projects, (s)he knows from experience that one's brain can learn a new code format in a matter of days; 一旦有人参与了几个项目,他就会从经验中知道,一个人的大脑可以在几天内学会一种新的代码格式; after that, it is as easy to interpret as the earlier favourite. 之后,它就像早期的最爱一样容易理解。 Thus, one stops having one "favourite" code format. 因此,一个人停止拥有一个“最喜欢的”代码格式。 So I must note that sticking to a specific code format is a beginner's malady; 所以我必须注意,坚持特定的代码格式是初学者的弊病; your team would be better trying to get over it to focus on the development itself. 你的团队会更好地试图克服它,专注于开发本身。

Hm.. if you use Eclipse you can do the following: 1. configure the formating you like (Window/Preferences/Java/Code Style/Formatter) 2. configure clean up (Window/Preferences/Java/Code Style/Clean Up), under edit check "Format source code" 3. if a developer has a project he can right click on it and source/clean up 嗯..如果您使用Eclipse,您可以执行以下操作:1。配置您喜欢的格式化(Window / Preferences / Java / Code Style / Formatter)2。配置清理(Window / Preferences / Java / Code Style / Clean Up) ,在编辑下检查“格式化源代码”3.如果开发人员有项目,他可以右键单击它并进行源/清理

The complete project will be then cleaned with the rules you defined there. 然后使用您在那里定义的规则清理整个项目。 So therefore after this he would have his own formatting. 所以在此之后他会有自己的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM