【问题标题】:Error while running Fortify on Gradle 7.2 project在 Gradle 7.2 项目上运行 Fortify 时出错
【发布时间】:2021-11-15 06:33:22
【问题描述】:

在 Gradle-Java 项目上运行 Fortify 20 时出现错误。该项目使用“gradle build”命令顺利编译,但在运行 Fortify 时出现此错误:

不得将 ForkOptions 上的可执行属性与 javaCompiler 属性一起使用

我唯一的线索是这个功能是从 6.7 版开始引入的,但该项目是在 gradle 7 上构建的。

我想知道是否可以抑制导致错误的两件事之一?

【问题讨论】:

    标签: java gradle fortify


    【解决方案1】:

    看起来它可能与 Gradle 有关。

    https://github.com/gradle/gradle/blob/master/subprojects/language-java/src/main/java/org/gradle/api/tasks/compile/JavaCompile.java

    如果你看一下github代码,你会看到这个方法:

    private void validateConfiguration() {
        if (javaCompiler.isPresent()) {
            checkState(getOptions().getForkOptions().getJavaHome() == null, "Must not use `javaHome` property on `ForkOptions` together with `javaCompiler` property");
            checkState(getOptions().getForkOptions().getExecutable() == null, "Must not use `executable` property on `ForkOptions` together with `javaCompiler` property");
        }
    }
    

    validateConfiguration()createSpec() 调用,在compile() 期间运行。

    请参阅下面使用java plugincompileJava 任务和options.forkOptions.executable 的示例build.gradle:

    https://docs.gradle.org/current/userguide/java_plugin.html#java_plugin

    import com.nr.builder.JarUtil
    
    apply plugin: 'java'
    
    subprojects {
        dependencies {
            // introspector classes for testing externals
            testImplementation(project(":instrumentation-test"))
        }
    }
    
    ext.moduleName = "com.greetings"
    
    dependencies {
        implementation("junit:junit:4.13")
    }
    
    compileJava {
        inputs.property("moduleName", "com.greetings")
        doFirst {
            options.compilerArgs = [
                    '--module-path', classpath.asPath,
                    '--patch-module', "$moduleName=" + files(sourceSets.main.java.srcDirs).asPath,
            ]
            classpath = files()
        }
    }
    
    compileJava.options.encoding = 'UTF-8'
    compileJava.options.fork = true
    
    // Compile with Java 11 to test module support
    compileJava.options.forkOptions.executable = jdk11 + '/bin/javac'
    compileJava.options.forkOptions.javaHome = new File(jdk11)
    
    compileTestJava.options.encoding = 'UTF-8'
    compileTestJava.options.fork = true
    
    // Compile with Java 11 to test module support
    compileTestJava.options.forkOptions.executable = jdk11 + '/bin/javac'
    compileTestJava.options.forkOptions.javaHome = new File(jdk11)
    
    // Boot classpath no longer works in JDK 9+ so we should ignore it here
    compileJava.options.bootstrapClasspath = null
    
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
    
    def module_test_args = [
            "-javaagent:${project.jar.archivePath.absolutePath}",
            "-javaagent:${JarUtil.getNewRelicJar(project(":newrelic-agent")).absolutePath}",
            "-Dnewrelic.config.file=${project(':newrelic-agent').projectDir}/src/test/resources/com/newrelic/agent/config/newrelic.yml",
            "-Dnewrelic.unittest=true",
            "-Dnewrelic.config.startup_log_level=warn",
            "-Dnewrelic.debug=$newrelicDebug",
            "--module-path=lib:out/production/classes",
            "--add-modules com.greetings",
            "--module junit/org.junit.runner.JUnitCore"
    ]
    
    test {
        dependsOn(project(":newrelic-agent").getTasksByName("newrelicJar", false))
        forkEvery = 1
        maxParallelForks = Runtime.runtime.availableProcessors()
    
        executable = jdk11 + '/bin/java'
    
        minHeapSize = "256m"
        maxHeapSize = "256m"
    
        beforeSuite {
            descriptor ->
                // We get two notifications per Test class. One of them is simply the Gradle executor used to run the test
                // We filter that one out and only log the Test class name with this null check.
                if (descriptor.getClassName() != null) {
                    logger.lifecycle("Running test suite: " + descriptor.getClassName())
                }
        }
    }
    
    javadoc {
        onlyIf { JavaVersion.current().isJava11Compatible() }
    }
    

    【讨论】:

    • 非常感谢您的回答,这对我更好地理解问题并找到解决方案帮助很大。为此,我不得不停用控制 Fortify 显然不喜欢的“工具链”配置的代码块。之后,项目正确编译。参考:blog.gradle.org/java-toolchains
    • 很高兴帮助@cgratelli
    猜你喜欢
    • 2015-06-30
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多