Search code examples
eclipsegradlemoreunit

1 Eclipse project, 2 artifacts with Gradle


I want to create structure for my new project and I intend to build it with Gradle. I already know that if I place sources and tests in one project plugins like MoreUnit will handle it easily and create tests for my classes right where I want them.

However it creates some awkward dependency issues when my project consists of several subprojects depending on each other - to be precise when I want to use some common code in tests in project A and then reuse it in tests in project B I had to do some workarounds like

project(':B') {
  // ...
  dependencies {
    // ...
    if (noEclipseTask) {
      testCompile project(':A').sourceSets.test.output
    }
  }
}

sometimes there were also some evaluation problem so another hack had to be introduced:

project(':B') {
  evaluationDependsOn(':A')
}

Splitting this into 2 separate projects got rid of that issue but then MoreUnit no longer was able to trace where it should create new test files, and mark which methods have been tested. I haven't found anything in MoreUnit config that would allow me to fix that, so am trying to fix this from Gradle side.

Can we arrange things so I can have several subprojects, sources and tests are arranged in maven like manner (project/src/java, project/test/java) but tests and sources will create separate artifacts? If I am solving the wrong problem then how should I solve the right one?


Solution

  • You can create some testenv jar for common like:

    sourceSets {
        testenv {
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    
    configurations {
        testenvCompile {
            extendsFrom runtime
        }
        testCompile {
            extendsFrom testenvRuntime
        }
        testenvDefault {
            extendsFrom testenvRuntime
        }
    }
    

    and

    task testenvJar(type: Jar, group: 'build', description: 'Assembles a jar archive containing the testenv classes.') {
        from sourceSets.testenv.output
    
        appendix = 'testenv'
    
        // add artifacts to testenvRuntime as task 'jar' does automatically (see JavaPlugin#configureArchivesAndComponent:106 and http://www.gradle.org/docs/current/userguide/java_plugin.html, "Figure 23.2. Java plugin - dependency configurations")
        configurations.testenvRuntime.artifacts.add new org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact(testenvJar)
    }
    
    task testenvSourcesJar(type: Jar, group: 'build', description: 'Assembles a jar archive containing all testenv sources.') {
        from sourceSets.testenv.allSource
    
        appendix = 'testenv'
        classifier = 'sources'
    }
    
    artifacts {
        archives testenvJar
        archives testenvSourcesJar
    }
    

    and use it in your depended projects like

        testCompile project(path: ':common', configuration: 'testenvDefault')
    

    I hope this helps!