4

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/**'])
        })
    }
}
skumar
  • 985
  • 4
  • 14
  • 37
  • _If I take out "src/main/java" in test under sourceSets, I am getting compilation error for junit test cases._ You need to fix that error. Please ask that as a question or add details in this question – Jayan Oct 25 '20 at 18:17
  • The directory structure seems to be standard one. You really do not need sourceSets at all. – Jayan Oct 25 '20 at 18:18
  • @jayan, The compilation error is due to gradle build doesn't find the corresponding java class (says invalid type found). I will remove the sourceSets and try it out. – skumar Oct 26 '20 at 20:00
  • It didn't work after removing sourceSet. "java:268: error: cannot find symbol FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileTestJava'. > Compilation failed; see the compiler error output for details.". It's not able to find the java classes. – skumar Oct 26 '20 at 22:05
  • Please add that error log . Normally the production classes are added to classpath for the tests. I am not sure you empty dependency block is adding to the trouble. – Jayan Oct 27 '20 at 07:10

1 Answers1

0

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.
thokuest
  • 5,800
  • 24
  • 36
  • If i remove sourceSets, the build is not able to find the java classes and nothing compiled.jar {} depends on jcompile. Do you want me to take out jar{} also? – skumar Oct 28 '20 at 16:57
  • I'd try to remove task `jcompile` first. Then remove `dependsOn jcompile` from task `jar`. If this doesn't help at all, I'd like to ask you to provide a [MVCE](https://stackoverflow.com/help/minimal-reproducible-example). – thokuest Oct 28 '20 at 19:48
  • nothing worked for me. After some research, the solution provided in this stackoverflow helped me to fix. https://stackoverflow.com/questions/61707087/error-cannot-find-symbol-in-gradle-compiletestjava – skumar Oct 30 '20 at 03:20