在Jenkins中使用Eclipse Compiler来获取编译器警告/错误

Han*_*nes 10 java eclipse ant coding-style jenkins

我想在我的Jenkins Job中显示eclipse编译器警告.我知道可以使用ant javac适配器使用Eclipse Compiler.这样,使用ant时会显示Eclipse编译器警告.问题是,当我在Jenkins中使用ant脚本时,他忽略了javac设置,只使用了普通的编译器.

有没有人尝试在jenkins中使用eclipse编译器并获得编译器警告?甚至可以将编译器警告发送给Sonar?

Joh*_*thy 6

在使用Eclipse编译器ant javac适配器出现问题之后,我在一个单独的目标中使用批处理编译器来生成Eclipse警告.然后我使用Warnings插件来解析Jenkins中生成的编译器警告.

批处理编译器可以方便地打包在一个单独的jar中,可以在Eclipse项目下载页面的"JDT Core Batch Compiler"部分下载,也可以在Eclipse ECJ的公共maven存储库中获得.

如果您已将ecj jar发布到ecj.dir,则可以利用以下构建脚本生成Eclipse警告,

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>
Run Code Online (Sandbox Code Playgroud)

eclipse_compiler.properties包含编译器警告/错误设置,可以从项目的.settings/org.eclipse.jdt.core.prefs文件中复制或在CompilerOptions类中定义.

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
Run Code Online (Sandbox Code Playgroud)


Nar*_*hai 1

如果您希望使用与 ant 提供的编译器接口不同的编译器接口,您可以编写一个实现该CompilerAdapter接口的类(包 org.apache.tools.ant.taskdefs.compilers)。

build.compiler在属性或特性中提供完整的类名compiler

这应该有效