Gradle compiles test classes into classes/main


(misak.boulatian) #1

The java plugin says that ‘main’ is for production and ‘test’ is for unit tests but when javaCompile completes test source is also compiled into ‘main’. Is this right behavior?


(Stefan Oehme) #2

It definitely doesn’t by default. Please share your build script, so we can see how you configured your project.


(misak.boulatian) #3

Here is my script. Even if I run compileJava, the classes/main also has the classes from ‘src/test’

apply plugin: 'java’
apply plugin: 'idea’
apply plugin: ‘eclipse’

repositories {
mavenCentral()
// maven {
// url “https://repository.jboss.org/nexus/content/groups/public-jboss/
// }
}

sourceCompatibility = 1.8

configurations {
provided
}

dependencies {
compile 'org.springframework:spring-context:4.2.3.RELEASE’
compile 'org.springframework:spring-webmvc:4.2.3.RELEASE’
compile 'org.springframework:spring-web:4.2.3.RELEASE’
compile 'org.springframework:spring-aop:4.2.3.RELEASE’
compile 'org.springframework.security:spring-security-core:4.0.3.RELEASE’
compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE’
compile 'org.springframework.security:spring-security-config:4.0.3.RELEASE’
compile 'org.springframework.security.oauth:spring-security-oauth2:2.0.8.RELEASE’
compile 'com.fasterxml.jackson.core:jackson-core:2.6.3’
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.3’
compile 'org.aspectj:aspectjweaver:1.8.7’
compile 'commons-beanutils:commons-beanutils:1.7.0’
compile 'commons-collections:commons-collections:3.2’
compile 'commons-dbutils:commons-dbutils:1.3’
compile 'commons-digester:commons-digester:1.7’
compile 'commons-fileupload:commons-fileupload:1.1.1’
compile 'commons-lang:commons-lang:2.3’
compile 'org.apache.commons:commons-lang3:3.4’
compile 'commons-validator:commons-validator:1.3.0’
compile 'commons-io:commons-io:1.3.2’
compile 'commons-pool:commons-pool:1.6’
compile 'org.apache.commons:commons-pool2:2.4.2’
compile 'org.apache.httpcomponents:httpclient:4.4.1’
compile 'org.apache.httpcomponents:httpcore:4.4.3’
compile 'org.apache.httpcomponents:httpmime:4.4.1’
compile 'commons-configuration:commons-configuration:1.10’
compile 'oro:oro:2.0.8’
compile 'commons-codec:commons-codec:1.5’
compile 'struts:struts:1.2.7’
compile 'com.lowagie:itext:2.1.7’
compile 'org.jfree:jfreechart:1.0.14’
compile 'com.google.inject:guice:2.0’
compile(‘org.opensaml:opensaml:2.4.1’) {
exclude group: ‘org.slf4j’
}
compile ‘com.ibm.icu:icu4j:4.6.1’
compile ‘org.owasp.esapi:esapi:2.0.1’
compile ‘org.jasypt:jasypt:1.9.0’
compile ‘net.sf.jasperreports:jasperreports:4.7.0’
compile(‘org.infinispan:infinispan-spring4:8.0.1.Final’) {
exclude group: ‘org.infinispan’, module: ‘infinispan-core’
}
compile ‘org.bouncycastle:bcprov-jdk15on:1.47’
compile ‘net.sf.jt400:jt400:8.7’
compile fileTree(dir: “thirdparty/volts/interfaces/lib”, includes: [’.jar’])
compile fileTree(dir: “thirdparty/quickfixj/lib”, includes: [’
.jar’])
compile fileTree(dir: “thirdparty/ostermillerutils/1.08.02/lib”, includes: [’.jar’])
compile fileTree(dir: “thirdparty/ics/peapi/1.3/lib”, includes: [’
.jar’])
compile fileTree(dir: ‘thirdparty/broadridgespc/lib’, includes: [’.jar’], excludes: [‘asm.jar’, 'aspect.jar’, ‘cglib*.jar’, ‘commons-lang*.jar’, ‘velocity*.jar’])

provided 'javax:javaee-api:7.0'
provided 'org.jboss.ejb3:jboss-ejb3-ext-api:2.2.0.Final'
provided('org.jboss.resteasy:resteasy-jaxrs:3.0.13.Final') {
    exclude group: 'org.apache.httpcomponents'
}
provided 'org.hibernate:hibernate-validator:5.2.2.Final'
provided 'org.infinispan:infinispan-spring4:8.0.1.Final'
provided 'commons-logging:commons-logging:1.1.1'
provided 'log4j:log4j:1.2.16'
provided 'org.slf4j:slf4j-simple:1.7.12'
provided 'junit:junit:4.12'
provided 'com.asual.lesscss:lesscss-engine:1.5.0'
provided fileTree(dir: 'thirdparty/ofx/lib', includes: ['*.jar'])
provided fileTree(dir: 'thirdparty/sybase/6.0/lib', includes: ['*.jar'])
provided fileTree(dir: 'thirdparty/sybase/7.0/lib', includes: ['*.jar'])
provided fileTree(dir: 'thirdparty/apache/ant/1.8.2/lib', includes: ['*.jar'])

}

sourceSets {
main {
java {
srcDir ‘src/main’
}
resources {
srcDir ‘src/resources’
}
}
test {
java {
srcDir ‘src/test’
}
}

main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided

}

idea {
module {
sourceDirs += file(‘src/main’)
sourceDirs += file(‘src/webapps’)
testSourceDirs += file(‘src/test’)
excludeDirs += file(’.gradle’)
excludeDirs += file(’.idea’)
excludeDirs += file(buildDir)
excludeDirs += file(‘bin’)
scopes.PROVIDED.plus += [configurations.provided]
}
}

eclipse {
project {
sourceSets {
main {
java {
srcDir 'src/main’
srcDir 'src/webapps’
srcDir 'src/resources’
srcDir ‘src/test’
}
}
}
jdt {
sourceCompatibility = 1.8
}
classpath {
plusConfigurations += [configurations.provided]
}
}
}

task wrapper(type: Wrapper) {
gradleVersion = ‘2.12’
}

Thanks,
Misak


(misak.boulatian) #4

Just to add: Here is my source structure

volts
.gradle
.settings
build
gradle
src
main
resources
test

So, as you can see from the above, my source structure is little different from the default. Production java sources are in ‘src/main’ instead of ‘src/main/java’ and test sources are in ‘src/test’ instead of ‘src/test/java’. Hence, I used ‘sourceSets’ element to specify the custom structure.

Thanks and appreciate all the help,
Misak


(Stefan Oehme) #5

I’ve trimmed your example down to

apply plugin: 'java'

sourceSets {
  main.java.srcDir "src/main"
  test.java.srcDir "src/test"
}

And cannot reproduce the issue. Make sure it is not some other program like Eclipse that is writing into the wrong folder.


(misak.boulatian) #6

Can you try your test by adding eclipse plugin? I just stripped down my build file and wanted to see which one is messing it up and noticed that it started putting compiled test sources into main after eclipse plugin.


(misak.boulatian) #7

eclipse plugin messed it up since I specified ‘src/test’ under main/java. It was my fault, but I did not know that eclipse plugin applies to java plugin.

Thanks,


(Stefan Oehme) #8
eclipse {
  project {
    sourceSets { //this is wrong
    }
  }
}
```

The eclipse plugin has **no** `sourceSets`. I don't know were you copied that snippet from, but is completely unnecessary. The snippet above was just configuring the Java plugin, overriding what you specified before.

Please see the [reference documentation](https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseModel.html) to see what methods each plugin supports.