Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

gradle - NullPointerException with JacocoPluginExtension

I'm running a build in Azure DevOps with latest gradle (6.8) and I get NPE:

Caused by: java.lang.NullPointerException
    at org.gradle.testing.jacoco.plugins.JacocoPluginExtension.applyTo(JacocoPluginExtension.java:162)
    at org.gradle.testing.jacoco.plugins.JacocoPluginExtension$applyTo.call(Unknown Source)
    at org.akhikhl.gretty.StartBaseTask.initJacoco(StartBaseTask.groovy:156)
    at org.akhikhl.gretty.StartBaseTask.<init>(StartBaseTask.groovy:41)
    at org.akhikhl.gretty.AppStartTask.<init>(AppStartTask.groovy)
    at org.akhikhl.gretty.AppStartTask_Decorated.<init>(Unknown Source)

I have such configuration in build.gradle :

buildscript {
    repositories {
        mavenCentral()
    }
}

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'jacoco'

group = '*****'
version = '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // my deps
}

test {
    maxParallelForks = 1
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test
}

jacoco {
    toolVersion = "0.8.6"
}

I have such configuration in Azure DevOps pipeline:

- task: Gradle@2
  displayName: 'Build and run unit tests'
  inputs:
    gradleWrapperFile: project-name/gradlew
    workingDirectory: project-name
    testResultsFiles: 'project-name/build/test-results/test/TEST-*.xml'
    sonarQubeRunAnalysis: true
    sonarQubeGradlePluginVersion: 2.6.2

Any ideas why it happens?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As see be seen from the stack trace, the NullPointerException happens when the Gretty plugin tries to configure the JaCoCo plugin.

Gradle is moving very fast and APIs are often deprecated and removed at least one major version later (e.g. deprecated in 5.0 and removed in 6.0). There is often about one to two years for buildscript and plugin authors to react to these deprecations.

Though you don't mention it, I am quite sure you are using a more than three year old version of the original Gretty plugin:

enter image description here

At least I can reproduce the issue and the same stack trace using that version.

Though it looks like it was abandoned years ago, this isn't the case. Instead, the plugin has been forked and moved to a new namespace (from org.akhikhl.gretty to org.gretty):

enter image description here

There is even a bug report describing the NullPointerException issue (going all the way back to Gradle 4.6).

So to fix it, change the Gretty plugin ID to the new namespace and use a recent version:

plugins {
  id "org.gretty" version "3.0.3"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...