gradle - including compiled classes to testCompile to avoid NoClassDefFoundError

Viewed 367

I am trying to include compiled classes (from the project3) to run junit tests.

Here is a brief project structure.

project1
   build.gradle
project2
   build.gradle
project3
   src/main/java/***.java
   src/test/java/***JunitTestClasses
   build.gradle - I am running this.

Here is the build.gradle from project3.

apply plugin: 'java'
apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'com.github.jacobono.jaxb'
apply plugin: "io.spring.dependency-management"

sourceCompatibility = 1.8
targetCompatibility = 1.8


buildscript {
  repositories {
    maven 
    {
         // omitted.
    }
  }

  dependencies 
  {
    classpath 'com.github.jacobono:gradle-jaxb-plugin:' + GRADLE_JAXB_PLUGIN_VERSION
    classpath 'io.spring.gradle:dependency-management-plugin:' + GRADLE_DEP_MGMT_PLUGIN_VERSION
  }
}

dependencies 
{
    compile project(':project1')
    compile project(':project2')
    compile project(':project3')
    compile group: 'org.apache.ant', name: 'ant', version: ANT_VERSION
    jaxb group: 'com.sun.xml.bind', name: 'jaxb-xjc', version: JAXB_VERSION
    jaxb group: 'com.sun.xml.bind', name: 'jaxb-impl', version: JAXB_VERSION
    jaxb group: 'com.sun.xml.bind', name: 'jaxb-core', version: JAXB_VERSION
    xmlbeans 'org.apache.xmlbeans:xmlbeans:2.5.0'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    runtime files('build/classes/main')
}


configurations {
  repobootstrap
  xmlbeans
}

sourceSets {
  generated {
    java {
      srcDir file("${buildDir}/generated")
    }
  }
  testGenerated {
    compileClasspath += sourceSets.main.runtimeClasspath
    java {
      srcDir file("${buildDir}/generated")
    }
  } 
  main {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }

  test {
    compileClasspath += sourceSets.generated.output
    runtimeClasspath += sourceSets.generated.output
  }
}




test 
{
  systemProperties 'basedir': projectDir
  systemProperties 'testdir': "${projectDir}/src/test"
  systemProperties 'resourcedir': "${projectDir}/src/test/resources"
  systemProperties 'TARGET':  "${buildDir}/tmp"
  systemProperties 'tmp.dir':  "${buildDir}/tmp"
}

Some tasks which generate classes from XSD are omitted..

When I do 'gradle build', it successfully generates classes from XML and compiles. But when junit tests run, it throws 'java.lang.NoClassDefFoundError' for classes that belong to project3 and 'java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.sE2A73A83DA959459556520519D4CA82A.TypeSystemHolder'. Generated artifacts for xmlbeans are located under generated\schemaorg_apache_xmlbeans3 and compiled classes are located under project3/build/main

I'd appreciated if somebody has any clue.

0 Answers
Related