comments 13

Going Java 1.5 With Maven2-Based Project

I recently made some simple updates to Wish by using Java 1.5 generics and autoboxing/unboxing. Making Java source changes was the easy part, while the time consuming part was on hunting for information on Maven-related changes.

I started by changing maven-compiler-plugin source and target in pom.xml:

<build>
  <plugins>
    ...
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
    ...
  </plugins>
</build>

And got this error upon running mvn site

Caused by: net.sourceforge.pmd.ast.ParseException: Can't use generics unless running in JDK 1.5 mode!

which led me to change maven-pmd-plugin targetJdk:

<reporting>
  <plugins>
    ...
    <plugin>
      <artifactId>maven-pmd-plugin</artifactId>
      <configuration>
        <targetJdk>1.5</targetJdk>
      </configuration>
    </plugin>
    ...
  </plugins>
</reporting>

Then I found out that the angle brackets used by generics syntax resulted in checkstyle errors:

<' is not preceded with whitespace.
error	<;' is not followed by whitespace.
error	'>' is not preceded with whitespace.

which is fixed by adding these modules to checkstyle.xml .

If you don’t already have any existing checkstyle rules, create checkstyle.xml file and place it on the root directory of the project (i.e. same place as pom.xml) and add configLocation to maven-checkstyle-plugin:

<reporting>
  <plugins>
    ...
    <plugin>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <configuration>
        <configLocation>checkstyle.xml</configLocation>
      </configuration>
    </plugin>
    ...
  </plugins>
</reporting>
About these ads

13 Comments

  1. pys

    Very useful.. if I could make it to work !

    I probably missed somthing. I have a specific checkstyle file and I put the new modules this way inside the treewalker module :

    <module name=”NoWhitespaceAfter”>
    <!– Default tokens and additional GENERIC_START –>
    <property name=”tokens” value=”ARRAY_INIT, BNOT, DEC, DOT, INC, LNOT,
    UNARY_MINUS, UNARY_PLUS, GENERIC_START”/>
    </module>

    <module name=”NoWhitespaceBefore”>
    <!– Default tokens and additional GENERIC_START and GENERIC_END –>
    <property name=”tokens” value=”SEMI, POST_DEC, POST_INC,
    GENERIC_START, GENERIC_END”/>
    </module>

    <module name=”WhitespaceAfter”>
    <!– Default tokens and additional GENERIC_END –>
    <property name=”tokens” value=”COMMA, SEMI, TYPECAST, GENERIC_END”/>
    </module>

    <module name=”WhitespaceAround”>
    <!– Default tokens without GENERIC_START and GENERIC_END –>
    <property name=”tokens” value=”ASSIGN, BAND, BAND_ASSIGN, BOR,
    BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV,
    DIV_ASSIGN, EQUAL, GE, GT, LAND, LCURLY, LE, LITERAL_ASSERT,
    LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY,
    LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED,
    LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD,
    MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL,
    SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN,
    TYPE_EXTENSION_AND, WILDCARD_TYPE”/>
    </module>

    It continues reporting errors for missing spaces before and after < and > in generics. Any idea about what I am doing wrong ?

  2. Grateful Visitor

    Man, thanks a bunch :) You saved my backside!

  3. phn

    Thanks for the tip – saved me a lot of trouble. Tip for pys: remove the existing NoWhitespaceAfter, NoWhitespaceBefore, WhitespaceAfter and WhitespaceAround modules from your checkstyle file. They probably override the new settings that you added. It should work after you do that.

  4. børrild

    Tip for phn and pys: The genric-tokens are not valid token parameters for the checks “NoWhitespaceAfter”, “NoWhitespaceBefore” and “WhitespaceAfter”, they are simply ignored.

  5. I’m having trouble with the

    Any ideas if it is related to the nowhistespaceafter modules? I’ve tried changing the code around a bit but nothing is changing yet.

  6. Martinez Marchand

    Weird, it didn’t show the code. I’m having issues with the whitespacearound module. The whole block of code just gives me some messed up error.

  7. abhi

    I am getting this error when using ‘jspc-maven-plugin’ I run maven Install.

    C:/WorkSpace/msp-operations/build/WEB-INF/jsp/user/assignClients.jsp(2,0) File “/msp.tld” not found

    The path for msp.tld is specified in the web.xml file which i am not able to refer.

    thus can u please help me how to refer this .

  8. Maven is something which is a tool difficult to understand, but once you understand it becomes the best thing that happened to Java.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s