Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Eclipse Compiler in Jenkins to get compiler warnings/errors

I want to have the eclipse compiler warnings shown in my Jenkins Job. I know that it is possible to use the Eclipse Compiler using the ant javac adapter. That way Eclipse compiler warnings are shown when using ant. Problem is that when i use an ant script in Jenkins he ignores the javac settings and just uses the normal compiler.

Did anyone try to use the eclipse compiler in jenkins and get the compiler warnings? maybe even send the compiler warnings to Sonar?

like image 438
Hannes Avatar asked Jan 07 '13 15:01

Hannes


2 Answers

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 errors
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=warning
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning
org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warning
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
like image 120
John McCarthy Avatar answered Oct 29 '22 16:10

John McCarthy


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

like image 20
Narendra Pathai Avatar answered Oct 29 '22 15:10

Narendra Pathai