简体   繁体   English

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

[英]Using Eclipse Compiler in Jenkins to get compiler warnings/errors

I want to have the eclipse compiler warnings shown in my Jenkins Job. 我想在Jenkins Job中显示Eclipse编译器警告。 I know that it is possible to use the Eclipse Compiler using the ant javac adapter. 我知道可以通过ant javac适配器使用Eclipse编译器。 That way Eclipse compiler warnings are shown when using ant. 这样,使用ant时会显示Eclipse编译器警告。 Problem is that when i use an ant script in Jenkins he ignores the javac settings and just uses the normal compiler. 问题是,当我在Jenkins中使用ant脚本时,他会忽略javac设置,而仅使用普通的编译器。

Did anyone try to use the eclipse compiler in jenkins and get the compiler warnings? 是否有人尝试在jenkins中使用eclipse编译器并获得编译器警告? maybe even send the compiler warnings to Sonar? 也许甚至将编译器警告发送给Sonar?

After having trouble with the Eclipse compiler ant javac adapter , I use the batch compiler instead in a separate target for generating Eclipse warnings. 在使用Eclipse编译器ant javac适配器遇到问题之后,我将批处理编译器改为在单独的目标中使用,以生成Eclipse警告。 I then use the Warnings Plugin to parse the generated compiler warnings in Jenkins. 然后,我使用警告插件解析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 . 批处理编译器方便地打包在一个单独的jar中,可在Eclipse项目下载页面的“ JDT Core Batch Compiler”部分下载 ,也可以在公共Maven存储库中以Eclipse ECJ的形式获得

Provided you've staged the ecj jar to ecj.dir , the following build script may be leveraged to generate Eclipse warnings, 如果您已将ecj jar暂存到ecj.dir ,则可以利用以下构建脚本来生成Eclipse警告,

build.xml 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包含编译器警告/错误设置,可以从项目的.settings/org.eclipse.jdt.core.prefs文件复制或在CompilerOptions类中定义。

eclipse_compiler.properties 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

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). 如果您希望使用与ant一起提供的编译器接口不同的编译器接口,则可以编写一个实现CompilerAdapter接口的类(包org.apache.tools.ant.taskdefs.compilers)。

Supply the full classname in the build.compiler property or the compiler attribute. build.compiler属性或compiler属性中提供完整的类名。

This should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM