Gradle Build - Execution failed for task ':sonarqube' can't be indexed twice

Viewed 1165

I am getting an error "File can't be indexed twice" in gradle build for sonar. Do i have to add any inclusion/exclusion property for sonar? Any help greatly appreciated.

If I take out "src/main/java" in test under sourceSets, I am getting compilation error for junit test cases.

This is how my build.gradle file content look,

group = 'groupid'
apply plugin: 'gradleFortify'
apply plugin: 'org.sonarqube'
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'groovy'
//apply plugin: "java-library-distribution"
//apply plugin: 'distribution'

version=version
sourceCompatibility = 1.8


repositories {
        maven {
        url "artifactory"
            credentials {
            }
        }
        sonarqube {
            properties {
                property 'sonar.sourceEncoding', 'UTF-8'
                property "sonar.exclusions", ["**/*Test.java"]
            }
        }
}

buildscript {
    repositories {
        maven {
        url "artifactory"
            credentials {
            }
        }
    dependencies {
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.9.7' 
        classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0'
        
    }
}

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java']
        }
    }
    test {
        java.srcDirs = ['src/test/java','src/main/java']
    }
}

dependencies{


}
 task preclean(type: Delete) {
     delete 'build'
 } 
 task jcompile(type: JavaCompile) {
     dependsOn preclean
     source = fileTree(dir: 'src/main/java', include: '**/*.java')
     destinationDir = file('build/classes')
     sourceCompatibility = '1.8'
     targetCompatibility = '1.8'
     classpath = configurations.runtime
 }
 

 task copyResources( type: Copy ) { 
 }

 task copyClasses( type: Copy ) {
 }
 
 
 jar {
    dependsOn jcompile
    archiveName = 'prj1.jar'
    manifest {
        attributes('Implementation-Title': project.name,
                   'Implementation-Version': project.version)
    }
     into('lib') {
         println "includeInJar: " + configurations.runtime.collect { File file -> file }
         from configurations.runtime
     }
 }
// jar.finalizedBy(jarTask) { }
 
 
sonarqube {
    properties {
        property "sonar.projectName", "prj-${version}"
        property "sonar.projectKey", "prj-${version}"
    }
}

test {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}
jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, includes: ['com/**'])
        })
    }
}
1 Answers

The issue described is most likely caused by custom tasks for what Gradle offers out of the box:

  • Task preclean deletes probably too much. Use clean instead.
  • Task jcompile compiles the main Java sources which basically duplicates compileJava. The jar task, by default, already depends on compileJava.

I suggest the following:

  1. Remove sourceSets configuration since you're following the convention of using src/main/java and src/test/java.
  2. Remove custom tasks preclean and jcompile.
  3. Cleanup your build script and remove empty tasks copyClasses and copyResources as well as no-op assignment version=version.
  4. From there on, should you have requirements to customize the build, try to resort to what Gradle offers already. You may want to read Building Java & JVM projects first.
Related