44

I have a checkstyle configuration XML file and want to automatically generate an Eclipse formatter configuration from this. Is there any tool that can do this?

Alex
  • 367
  • 1
  • 3
  • 17
Ken Liu
  • 22,503
  • 19
  • 75
  • 98
  • 1
    Dear Ken, some of us think you really should accept the above answer as the best (not sure that Paul is running after the Unsung hero's badge ;)) – Jean-Rémy Revy Jan 04 '16 at 14:58
  • 1
    @Jean-RémyRevy done, thanks for the ping! – Ken Liu Jan 05 '16 at 17:16
  • @Jean-RémyRevy It's good that the answer was accepted, but unsung hero would be for users that have accepted answers with 0 votes, rather than lots of votes without being accepted. (Just trying to save others from having to search what unsung hero badge means) – Vivek Chavda Aug 02 '17 at 13:22

2 Answers2

76

In Eclipse (3.6):

  • Install Checkstyle plug-in
  • Import stylesheet using Windows --> Preferences, General --> Checkstyle --> New. Since you have an external file, choose "external file" as the type.

Right-click on your project in the Package view and select Checkstyle --> Create Formatter-Profile.

Then enable the formatter for your workspace: Windows --> Preferences --> Java --> Code Style --> Formatter. Select formatter: "eclipse-cs [project name]".

Click OK!

David X
  • 3,998
  • 3
  • 29
  • 32
Paul
  • 1,939
  • 1
  • 18
  • 27
  • which file should I import into the Eclipse check-style plug-in for the Google Java style - http://google-styleguide.googlecode.com/svn/trunk/? I tried `styleguide.css`, but that failed. – Kevin Meredith Feb 26 '14 at 14:49
  • 1
    Looks like it depends on the programming language: [C++](http://google-styleguide.googlecode.com/svn/trunk/eclipse-cpp-google-style.xml) [Java](http://google-styleguide.googlecode.com/svn/trunk/eclipse-java-google-style.xml) You need an XML file in any case, not a CSS. – Paul Feb 27 '14 at 20:46
  • 2
    This feature is broken and the Eclipse plugin authors said in March 2019 that they were planning to remove it. https://stackoverflow.com/a/55323793/733092 – Noumenon Dec 10 '19 at 23:20
  • @Noumenon is there an alternative AFAYK? – Marco Bolis Apr 06 '21 at 08:07
  • @MarcoBolis I never found one and just ran it from the command line. – Noumenon Apr 06 '21 at 13:56
1

The answer currently marked as accepted does not work as is with Eclipse 2022-06 and Checkstyle Plug-in 10.0.0. These are the steps that worked for me:

  1. Install Checkstyle plug-in

  2. Import Checkstyle configuration: Windows -> Preferences -> Checkstyle -> New. Choose "external file" as the type.

  3. Right-click on your project in the Package view and select Checkstyle -> Create Formatter-Profile. The following files will be created in you project:

  • my-project-cs-cleanup.xml
  • my-project-cs-formatter.xml
  1. Add the generated my-project-cs-formatter.xml as a formatter: Windows -> Preferences -> Java -> Code Style -> Formatter -> Import
Miguel Alorda
  • 623
  • 5
  • 13