Search code examples
gradlegretty

Add additional source set to Gretty classpath


My project contains a jar and a war module. The jar module contains to source sets main and generated.

My jar module gradle.build defines the source sets as listed below:

sourceSets {
    generated
    main {
        compileClasspath += sourceSets.generated.output  // adds the sourceSet to the compileClassPath
        runtimeClasspath += sourceSets.generated.output  // adds the sourceSet to the runtimeClasspath
    }
}

and places the output of both source sets into the jar.

jar {
    from sourceSets.generated.output
    from sourceSets.main.output
}

Within my war module I'd like to use Gretty to run it within the build. The build.gradle file looks like this.

apply plugin: 'war'
apply from: "${rootDir}/gradle/gretty.gradle"

gretty {
    // supported values:
    // 'jetty7', 'jetty8', 'jetty9', 'tomcat7', 'tomcat8'
    servletContainer = 'tomcat8'

    httpPort = 8081
    contextPath = '/wbc'
    realm 'wbc-realm'
    realmConfigFile 'tomcat-users.xml'
}

dependencies {
    compile project(':interfaces')

    compile "org.atmosphere:atmosphere-runtime:${atmosphereVersion}"
    compile "org.springframework:spring-web:${springVersion}"
    compile "javax.servlet:javax.servlet-api:${servletVersion}"

    runtime "org.slf4j:slf4j-log4j12:${slf4jVersion}"
    runtime "log4j:log4j:1.2.17"
}

Whenever I start Gretty using gradle --daemon clean appRun Gretty fails to start up Tomcat due to a ClassNotFoundException. That class is located in the generated source set of my jar module. How can I tell gretty to add it to the classpath?


Solution

  • Try adding the output directories of generated to gretty classPath:

    gretty {
      ...
      sourceSets.generated.output.each { classPath it }
    }