Configure Gradle to include main classes as well a

2019-08-05 07:40发布

问题:

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.

回答1:

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.



标签: build gradle