Menu

Package PMD-Java after adding rules

Developers
2015-03-20
2015-03-23
  • Karthik Shantaraman

    Hi,

    I have added 2 Java rules to the existing PMD-Rules. I need to know how to package them.
    When I specify the following in the pom.xml and build the projects its throwing an error

    <parent>
            <groupId>net.sourceforge.pmd</groupId>
            <artifactId>pmd</artifactId>
            <version>5.2.3</version>
    </parent>
    
    <groupId>com.mycompany.pmd</groupId>
    <artifactId>pmd-java</artifactId>
    <packaging>jar</packaging>
    <name>PMD Java</name>
    <version>0.0.1-SNAPSHOT</version>
    

    Error that I am getting

    Missing artifact net.sourceforge.pmd:pmd-core:jar:0.0.1-SNAPSHOT
    Missing artifact net.sourceforge.pmd:pmd-test:jar:0.0.1-SNAPSHOT

    Can anyone help me with this?

    Thanks

     
  • Andreas Dangel

    Andreas Dangel - 2015-03-22

    It seems, you actually want to create an own module, with your custom rules.
    I didn't write up a documentation for this yet, but I would do a similar approach as checkstyle does for multi-module builds.

    1. Create a own project, separate from pmd. You'll add pmd-dependencies, but won't change pmd:

      Let's say, you create it in the folder c:\temp\my-custom-pmd-rules

      Create a pom.xml file:

      <project>
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.mycompany.pmd</groupId>
          <artifactId>pmd-java-custom-rules</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      
          <dependencies>
              <dependency>
                  <groupId>net.sourceforge.pmd</groupId>
                  <artifactId>pmd-java</artifactId>
                  <version>5.2.3</version>
              </dependency>
          </dependencies>
      </project>
      
    2. Now, place your ruleset xml in the subdirectory src/main/resources/rulesets/java/ and name the file e.g. custom-rules.xml. See How to make a ruleset.

      You can place (in case you are writing Java based rules) your rules in src/main/java.

    3. Build this new project: mvn clean install

    4. Now - in your main project, where you want to use the custom rules, configure the maven pmd plugin as follows:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-pmd-plugin</artifactId>
          <version>3.4</version>
          <configuration>
             <rulesets>
                 <ruleset>rulesets/java/custom-rules.xml</ruleset>
              </rulesets>
          </configuration>
          <dependencies>
              <dependency>
                  <groupId>com.mycompany.pmd</groupId>
                  <artifactId>pmd-java-custom-rules</groupId>
                  <version>0.0.1-SNAPSHOT</version>
              </dependency>
          </dependencies>
      </plugin>
      

      See also maven-pmd-plugin for more information on how to configure the maven plugin.

    5. Alternatively, you can copy the jar file from step 3 into a pmd installation's lib folder. You can zip the complete pmd installation again and can create so a custom binary distribution file with your rules includes.

    Hope that helps,
    Andreas

     
  • Karthik Shantaraman

    Hi Andreas,

    Thank you for the inputs. Created a new project and it works!

    When I use this against my code will the other PMD java Rules, included in PMD-5.2.3, also be applied for my code in addition to these two rules

    In other simpler words

    Will the rules that I added get appended to the Java ruleset or will it override the java ruleset?

     

    Last edit: Karthik Shantaraman 2015-03-23
  • Andreas Dangel

    Andreas Dangel - 2015-03-23

    It is a separate ruleset.
    You need to configure, which ruleset(s) to execute.
    If you are using maven-pmd-plugin, you can define multiple rulesets:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.4</version>
        <configuration>
           <rulesets>
               <ruleset>java-basic</ruleset>      <!-- default ruleset -->
               <ruleset>java-imports</ruleset>    <!-- default ruleset -->
               <ruleset>java-unusedcode</ruleset> <!-- default ruleset -->
               <ruleset>rulesets/java/custom-rules.xml</ruleset>
            </rulesets>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>com.mycompany.pmd</groupId>
                <artifactId>pmd-java-custom-rules</groupId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </plugin>
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.