Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config Jacoco Offline Instrumention in Gradle 6.0 #1152

Open
hoangducnghi2207 opened this issue Mar 23, 2023 · 0 comments
Open

How to config Jacoco Offline Instrumention in Gradle 6.0 #1152

hoangducnghi2207 opened this issue Mar 23, 2023 · 0 comments

Comments

@hoangducnghi2207
Copy link

hoangducnghi2207 commented Mar 23, 2023

I'm setting my gradle.build like this to use Jacoco Offline Instrumention in Gradle, but it's not worked.
When I use command : gradlew clean report, the Error is:

  • What went wrong:
    Execution failed for task ':instrument'.
    Could not get unknown property 'jacoco' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.

What should I do to solve this problem ? Thanks a lot

 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.0/userguide/java_library_plugin.html
 */

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
    id 'checkstyle'
    id 'jacoco'
    id "com.github.spotbugs" version "4.2.3"
}

repositories {
	mavenCentral()
	maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}

dependencies {
    spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.7.1'    
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.0-jre'  
    implementation group: 'com.github.griddb', name: 'gridstore', version: '5.0.0'
    
    implementation group: 'com.microsoft.azure', name: 'azure-storage', version: '8.4.0'
    
    implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '11.1.1.jre11-preview'
    
    implementation group: 'commons-io', name: 'commons-io', version: '2.6'
    
    testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.18.0'
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
    testImplementation group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '5.8.2'
    testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.0-beta.5'

    testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5'
    testImplementation group: 'org.jmockit', name: 'jmockit', version: '1.25'
    
    compile fileTree(dir: 'libs', include: ['*.jar'])
    
}
configurations {
    jacoco
    jacocoRuntime
}

task instrument(dependsOn: ['classes']) {
    ext.outputDir = buildDir.path + '/classes-instrumented'
    doLast {
        ant.taskdef(name: 'instrument',
                classname: 'org.jacoco.ant.InstrumentTask',
                classpath: configurations.jacoco.asPath)
        ant.instrument(destdir: outputDir) {
            sourceSets.main.output.classesDirs.each { fileset(dir: it) }
        }
    }
}

gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(instrument)) {
        tasks.withType(Test) {
            doFirst {
                systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/tests.exec'
                classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime
            }
        }
    }
}

task report(dependsOn: ['instrument', 'test']) {
    doLast {
        ant.taskdef(name: 'report',
                classname: 'org.jacoco.ant.ReportTask',
                classpath: configurations.jacoco.asPath)
        ant.report() {
            executiondata {
                ant.file(file: buildDir.path + '/jacoco/tests.exec')
            }
            structure(name: 'Example') {
                classfiles {
                    sourceSets.main.output.classesDirs.each { fileset(dir: it) }
                }
                sourcefiles {
                    fileset(dir: 'src/main/java')
                    //uncomment this if you use groovy
                    //fileset(dir: 'src/main/groovy')
                }
            }
            html(destdir: buildDir.path + '/reports/jacoco')
        }
    }
}

jar {
  manifest {
    attributes(
      'Main-Class': 'com.toshiba.mwcloud.gs.dbaas.regular.export.App'
    )
  }
    
  from (configurations.compile.collect { entry -> zipTree(entry) }) {
        exclude 'META-INF/MANIFEST.MF'
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
    }

  
}

task fatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.toshiba.mwcloud.gs.dbaas.regular.export.App'
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

test {
    testLogging {
        events "passed", "skipped", "failed"
    }
    useJUnitPlatform()
}

def checkstyleVersion = '8.23'
checkstyle {
    configFile = file("google_checks.xml")
    toolVersion = checkstyleVersion
    ignoreFailures = false
}

jacoco {
	toolVersion = "0.8.5+"
}

jacocoTestReport {
	reports {
		html.enabled = true
		xml.enabled = true
		csv.enabled = false
	}
}

spotbugs {
  toolVersion = '4.2.3'
}

// To generate an HTML and XML report 
spotbugsMain {
  reports {
    xml.enabled = false
    html {
      enabled = true
      destination = file("$buildDir/reports/spotbugs/main/spotbugs.html")
      stylesheet = 'fancy-hist.xsl'
    }
  }
}

reporting.baseDir = "$buildDir/reports"

`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant