Tutorial: Checkstyle for Eclipse

idiotdiscSoftware and s/w Development

Aug 15, 2012 (5 years and 29 days ago)

685 views

Tutorial
:
Checkstyle for Eclipse

Pre
-
requisite:
Eclipse 3.
x

Steps to Setup and use Checkstyle for Eclipse

1.

Open the Eclipse

and
Navigate to
Help

|
Software Updates

|
Find and Install


2.

Select
the 2
nd

radio button
-


Search for new features to install



3.

Cli
ck on ‘New R
emote
S
ite

--

a
dd
Checkstyle
in ‘Name’ field
and point it to
http://eclipse
-
cs.sourceforge.net/update


4.

F
ollow the
wizard and
hit ‘next’ after checking the box.




Click on the ‘Finis
h’ button, it will start downloading the ‘chackstyle’ jar file.

After download, Eclipse performs verification on the downloaded
Jar file. It takes few
minutes to complete. Displays the verification details and asks for Install/Cancel.

Click on the ‘Install
’ button, to install the ‘chackstyle’ plugin.



5.

Create a new Project OR use any existing project

6.

Build the project

7.

In the Package explorer right
-
click on
‘<
project
-
name
>’

and select
Properties

|
Checkstyle

|
Check code with

Checkstyle

Note: This will run

the
Checkstyle for this project

with the default
ruleset
[
Sun
Checks (Eclipse)
-

(Global)
]

8.

Navigate to
Window

|
Show view

|
Other…

In the dialog window,
Select
Checkstyle

|
Checkstyle violations

9.

In the Checkstyle violations view
,

click
on
the chart icon i
n the upper right corner.
Th
is will open the pie chart view of the violations.



Using

the
Custom
Checkstyle
configuration file
:

Pre
-
requisite:
Copy the ‘
My
Checkstyle.xml
’ to your local machine in the path:
%
eclipse
_home%
\
plugins
\
com.atlassw.tools.eclipse
.checkstyle_4.3.2
\

1.

Navigate
to
Window

|
Preferences

Click on ‘New’


2.

Provide any appropriate name in the ‘Name’ field and Select ‘External
Configuration File’ as Type from the drop down list.


Click on ‘Browse’ and Select the appropriate configuration XML
file. Click on
‘Open’ to return back to previous window


3.

One new entry is created for the ‘External Configuration File’. Click ‘Ok’ on the
‘Preferences’ window. It will save the settings and close the window
.


4.

In the Package explorer right
-
click on
‘<
pro
ject
-
name
>’

and select

Properties
’. It
will open the ‘
Properties
’ dialog window.


5.

On the Properties window

(above)
, select the appropriate name (name provided in
step 3) from the drop
-
down list and click on ‘Ok’.


6.

Click on ‘Yes’ to
Rebuild the project
w
ith the selected Checkstlye configuration
and
viola
tions will
be
shown in the View.



Modifying
the
default

‘Rule Set’

configuration


Note: This is applicable to the particular instance of Eclipse

1.

Navigate to
Window

|
Preferences

|
Checkstyle

and Click o
n New…

2.

Select
Internal Configuration
, enter a name and description. Click
Import…

and
navigate to
ECLIPSE_HOME/plugins/com.atlassw.tools.eclipse.checkstyle_x.x.x

and select

any of the existing rules XML file (for e.g.

My
Checkstyle.xml
)
. This will
import a
ll Sun Eclipse rules for the new custom ruleset. Also note that Checkstyle
will create a new custom ruleset xml file under
WORKSPACE/.metadata/.plugins/com.atlassw.tools.eclipse.checkstyle/internal_
config__xxxxxxxxxxx.xml
. Click
OK
.

3.

Double click the new r
uleset.
It opens a dialog window. Do the required
modification and Apply.


4.

Click
OK
.

It will
rebuild

the project and
viola
tions will be shown in the View.