Reputation: 125
I want to compile some java files to one jar file with gradle.
The goal is to add them to a jar and check the dependcies of the tests.
Maven should integrate the junit libary.
I also want to apply some kind of filter to test only the files with *Tests.java
The build.gradle
file is in the same directory as the src
folder.
My file structure(folders or filenames cannot be changed):
src/eu/conflicts/Conflicts.java
src/eu/conflicts/ConflictsTests.java
src/eu/utils/BuggyUtils.java
src/eu/utils/BuggyUtilsTests.java
After running gradle -b build.gradle jar
I got the error message: Could not find the main method
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
sourceSets {
main {
conflicts {
srcDir = 'src/eu/conflicts'
}
utils {
srcDir = 'src/eu/utils'
}
}
test {
conflicts {
srcDir = 'src/eu/conflicts'
}
utils {
srcDir = 'src/eu/utils'
}
}
}
jar {
baseName = 'xy'
version = '0.1'
manifest {
attributes("Implementation-Title": "xy",
"Implementation-Version": 0.1)
}
}
Upvotes: 2
Views: 2126
Reputation: 7221
Your source sets seem to be incorrect, and you don't really need to amend jar task if you are not bothered about the artifact being build.
To run all tests and compile your files you just need to run build
task, it depends on assemble
and check
which depends on other tasks like test
, compile
etc. To use it you need to apply plugin: 'java'
.
Gradle will build a single artifact (if you use build task) compiling all you want, by default packing your main sources into a jar without test sources.
Gradle is going to run all tests in test sources (Using test task bundled in build task) discovered via annotations.
You can amend all that using SourceSets{} in your script to point to non-default structures
Try maybe this:
sourceSets {
main.java{
srcDirs = ['src/eu/conflicts', 'src/eu/utils']
include '**/*.java'
}
test.java{
srcDirs = ['src/eu/conflicts', 'src/eu/utils']
include '**/*Tests.java'
}
}
Upvotes: 3
Reputation: 51461
Your source sets definition is incorrect. Java plugin only supports java
and resources
source types within a source set. Each source set can have an arbitrary name. If you name your source set main
it will customise the one used by convention by the jar task. So:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src'
exclude '**/*Tests.java'
}
}
test {
java {
srcDir 'src'
include '**/*Tests.java'
}
}
}
Upvotes: 2