4
votes

How do I configure Gradle to include main classes as well as test classes into my .jar archive. Currently the .jar that gets built in the build/libs directory is missing my test classes. It is only including classes sourced from project-root/src/main/java/com/package in the .jar after Gradle compiles them into project-root/bin/com/package/ .

jar {
    manifest {
        attributes 'Implementation-Title': title, 'Implementation-Version': version, 'Main-Class': mainTestClass
    }
}

The wierd thing is that while Gradle is putting the compiled test classes into project-root/bin/com/package/ ALONG with the main classes, it strangely is deciding to not put them into the .jar archive. So, how do I get the .jar to also include the test classes that were originally sourced in the project-root/src/test/java/com/package/ directory?

Here is my build.gradle file that I need help with.

2
Your gradle file looks like it includes quite a bit that you shouldn't need. If using WebDriver with Gradle I'd also strongly recommend looking at Geb (gebish.org)Matt Whipple
Thanks for the suggestion but my code is pure Java and I don't really use Groovy in my project except that I use Gradle to build my project. At your suggestion, I edited my build.gradle and removed a few lines from it.djangofan

2 Answers

5
votes

From the docs:

The Java plugin defines two standard source sets, called main and test. The main source set contains your production source code, which is compiled and assembled into a JAR file. The test source set contains your unit test source code, which is compiled and executed using JUnit or TestNG.

It's highly suspect that you would want your tests in your jar, but adding them to the main source set would likely be the simplest solution.

0
votes

I use like this

task jarr(type: Jar) {
    from sourceSets.main.output + sourceSets.toSet().output
    manifest {
        attributes(
                'Main-Class': 'main.Mainn',
                "Manifest-Version": "1.0",
                "Class-Path": configurations.compile.collect { it.getName() }.join(' ') + ' ' +
                        configurations.testRuntime.collect { it.getName() }.join(' ') + ' ' +
                        configurations.implementation.collect { it.getName() }.join(' ')
        )
    }

}