Skip to content

Replace PMD used by the engine

Robert Sösemann edited this page Nov 17, 2016 · 18 revisions

The Code Climate engine is a Docker image which basically wraps a custom PMD version. Contributions made to PMD will not be automatically used by the Code Climate engine. You have to locally build a stripped-down (only Apex-relevant jars) version of PMD and add / replace the jars in the engine.

Build PMD Delete the old image in docker using command

docker rmi

  1. Download and unpack the desired PMD release to a folder

copy new rulesset

  1. Replace the /pmd-dist/pom.xml with this lean version which only contains Apex related jars. This is required to keep the engine's Docker file as small as possible.

     <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
         <artifactId>pmd-dist</artifactId>
         <name>PMD Distribution Packages</name>
         <packaging>pom</packaging>
    
         <parent>
             <groupId>net.sourceforge.pmd</groupId>
             <artifactId>pmd</artifactId>
             <version>5.5.2-SNAPSHOT</version>
         </parent>
    
         <properties>
             <config.basedir>${basedir}/../pmd-core</config.basedir>
         </properties>
    
         <build>
             <plugins>
                 <plugin>
                     <artifactId>maven-assembly-plugin</artifactId>
                     <configuration>
                         <appendAssemblyId>false</appendAssemblyId>
                         <attach>false</attach>
                         <archiverConfig>
                             <defaultDirectoryMode>493</defaultDirectoryMode> <!-- 0755 -->
                         </archiverConfig>
                     </configuration>
                     <executions>
                         <execution>
                             <id>build-bin-dist</id>
                             <phase>package</phase>
                             <goals>
                                 <goal>single</goal>
                             </goals>
                             <configuration>
                                 <finalName>pmd-bin-${project.version}</finalName>
                                 <descriptors>
                                     <descriptor>src/main/assembly/bin.xml</descriptor>
                                 </descriptors>
                             </configuration>
                         </execution>
                         <execution>
                             <id>build-src-dist</id>
                             <phase>package</phase>
                             <goals>
                                 <goal>single</goal>
                             </goals>
                             <configuration>
                                 <finalName>pmd-src-${project.version}</finalName>
                                 <descriptors>
                                     <descriptor>src/main/assembly/src.xml</descriptor>
                                 </descriptors>
                             </configuration>
                         </execution>
                     </executions>
                 </plugin>
             </plugins>
         </build>
         <dependencies>
             <dependency>
                 <groupId>net.sourceforge.pmd</groupId>
                 <artifactId>pmd-core</artifactId>
                 <version>${project.version}</version>
             </dependency>
         </dependencies>
    
         <profiles>
             <profile>
                 <id>jdk8-modules</id>
                 <activation>
                     <jdk>1.8</jdk>
                 </activation>
                 <dependencies>
                     <dependency>
                         <groupId>net.sourceforge.pmd</groupId>
                         <artifactId>pmd-apex</artifactId>
                         <version>${project.version}</version>
                     </dependency>
                     <dependency>
                         <groupId>net.sourceforge.pmd</groupId>
                         <artifactId>pmd-apex</artifactId>
                         <version>${project.version}</version>
                         <classifier>apex-jorje-shaded</classifier>
                     </dependency>
                 </dependencies>
             </profile>
         </profiles>
     </project>
    
  2. Install Maven in order to execute mvn commands in your CLI.

  3. Check if you have a valid ${user.home}/.m2/toolchains.xml containing valid JDK paths. Read the official PMD README for more details. Our toolchains.xml von MacOSX looks like this:

     <?xml version="1.0" encoding="UTF8"?>
     <toolchains>
       <!-- place this file in ${user.home}/.m2/toolchains.xml -->
       <toolchain>
         <type>jdk</type>
         <provides>
           <version>1.7</version>
         </provides>
         <configuration>
           <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home</jdkHome>
         </configuration>
       </toolchain>
    
       <toolchain>
         <type>jdk</type>
         <provides>
           <version>1.8</version>
         </provides>
         <configuration>
           <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home</jdkHome>
         </configuration>
       </toolchain>
     </toolchains>
    
  4. To build PMD navigate with your CLI to the root directory of PMD and execute mvn clean package

  5. If the build fails you have to have a deeper look into the debug log to find and fix the root cause. Most of the time a test fails or the toolchains.xml file is invalid.

Update the PMD version of the engine

  1. After your build succeeds navigate to /pmd/pmd-dist/target directory.

  2. Unzip the generated pmd-bin-*.*.*-SNAPSHOT.zip.

  3. Fork the Apex Metrics engine and connect the repository to your Eclipse and import it as a general project in Eclipse.

  4. Replace the content of /codeclimate-apexmetrics/lib/pmd with the content of your unzipped pmd-bin-*.*.*-SNAPSHOT directory.

  5. Make sure that the file /codeclimate-apexmetrics/lib/pmd/bin/run.sh is executable. E.g. using Eclipse properties dialog

runshexecutable

Otherwise the engine will later fail to run as described here: https://github.com/Up2Go/codeclimate-apexmetrics/issues/40.

Build and test your custom engine locally

First you need to install Docker and the Code Climate CLI.

  1. Start Docker deamon (On MacOsX by opening Docker Quickstart Terminal) and check if it is running by executing docker images If you get Cannot connect to the Docker daemon. Is the docker daemon running on this host? you need to execute eval "$(docker-machine env default)". Here is why.

  2. Navigate into your codeclimate-apexmetrics directory inside your git directory and execute

     docker build -t codeclimate/codeclimate-apexmetrics .
    
  3. Check if there is a codeclimate/codeclimate-apexmetrics image by executing

     docker images
    
  4. Download and unzip the following Open source repositories

  1. Run the newly built engine in the root directory of each repository by executing

     docker run codeclimate/codeclimate analyze --dev
    

    If the analysis is successful it should show issue output like this. If an error occurs try to understand the root cause and fix it BEFORE you can proceed with the last step!

     ...
     83: The method getInit() has an NCSS line count of 61 [apexmetrics]
     
     == src/classes/TableGridUtils.cls (1 issue) ==
     53: Avoid long parameter lists [apexmetrics]
     
     Analysis complete! Found 22 issues.
    
  2. Commit your changes to your fork and create a Pull Request to merge your changes. You should provide a detailed description of your changes, why you did them and how you tested them. After we checked and merged your pull request we will publish your changes to Code Climate.

Clone this wiki locally