Java – use the eclipse compiler in Jenkins to get compiler warnings / errors

I want to display eclipse compiler warnings in my Jenkins job

Has anyone tried to use the eclipse compiler in Jenkins and get compiler warnings? May even send compiler warnings to sonar?

Solution

After a problem occurs with the eclipse compiler ant javac adapter, I will use batch compiler instead of generating eclipse warnings in a separate target Then I use warnings plugin to parse compiler warnings generated in Jenkins

The batch compiler is conveniently packaged in a separate jar, which can be downloaded in the "JDT core batch compiler" section of the eclipse project download page, or as an eclipse ECJ in the public Maven repository

If you upgrade ECJ jar to ECJ Dir, the following generation script may be used to generate eclipse warnings,

build. XML file

<?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 compiler warning / error settings, which can be downloaded from the project settings / org. eclipse. jdt. core. The prefs file is copied 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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>