Using Eclipse Compiler in Jenkins to get compiler warnings/errors Using Eclipse Compiler in Jenkins to get compiler warnings/errors jenkins jenkins

Using Eclipse Compiler in Jenkins to get compiler warnings/errors


After having trouble with the Eclipse compiler ant javac adapter, I use the batch compiler instead in a separate target for generating Eclipse warnings. I then use the Warnings Plugin to parse the generated compiler warnings in Jenkins.

The batch compiler is conveniently packaged in a separate jar, downloadable under the "JDT Core Batch Compiler" section on an Eclipse project download page and also available in public maven repositories as Eclipse ECJ.

Provided you've staged the ecj jar to ecj.dir, the following build script may be leveraged to generate Eclipse warnings,

build.xml

<?xml version="1.0" encoding="UTF-8" ?><project name="some-project" default="eclipse-warnings" basedir=".">    <target name="eclipse-warnings" depends="init">                <property name="ecj.log.dir" value="${build.dir}/ecj" />        <property name="ecj.warnings.file" value="${ecj.log.dir}\eclipse_compiler_out.txt"/>        <delete dir="${ecj.log.dir}" />        <mkdir  dir="${ecj.log.dir}" />        <property name="ecj.properties" value="${basedir}\etc\ecj\eclipse_compiler.properties" />                        <!-- Redirect output to tempfile if you don't want results also on console -->        <tempfile property="ecj.output.tempfile" suffix=".log" deleteonexit="true" />        <echo message="Generating Eclipse warnings to ${ecj.warnings.file}" />                <java             jar="${ecj.dir}/ecj.jar"            fork="true"            maxmemory="512m"            output="${ecj.output.tempfile}">            <arg value="-cp" />            <arg value="${toString:compile.classpath}" />            <arg value="-d" />            <arg value="none" />            <arg value="-enableJavadoc" />            <arg value="-log" />            <arg value="${ecj.warnings.file}" />                        <arg value="-${javac.source.version}" />            <arg value="-properties" />            <arg value="${ecj.properties}" />            <arg value="${src.dir}" />            <arg value="${test.dir}" />                    </java>        <!-- Echo number of warnings found -->        <loadfile srcfile="${ecj.warnings.file}" property="ecj.warnings.file.summary">            <filterchain>                <tailfilter lines="1" />            </filterchain>        </loadfile>       <echo message="${ecj.warnings.file.summary}" />    </target>    </project>

eclipse_compiler.properties contains the compiler warn/error settings and may be copied from a project's .settings/org.eclipse.jdt.core.prefs file or defined in the CompilerOptions class.

eclipse_compiler.properties

#Eclipse compiler warnings and errorsorg.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=warningorg.eclipse.jdt.core.compiler.problem.redundantNullCheck=warningorg.eclipse.jdt.core.compiler.problem.fallthroughCase=warningorg.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=warningorg.eclipse.jdt.core.compiler.problem.emptyStatement=warningorg.eclipse.jdt.core.compiler.problem.enumIdentifier=errororg.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warningorg.eclipse.jdt.core.compiler.problem.assertIdentifier=errororg.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warningorg.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warningorg.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warningorg.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warningorg.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning


If you wish to use a different compiler interface than those supplied with ant, you can write a class that implements the CompilerAdapter interface (package org.apache.tools.ant.taskdefs.compilers).

Supply the full classname in the build.compiler property or the compiler attribute.

This should work