Gradle adopts the idea of convention, so it is not the default folder structure Unable to build project. However, in Gradle, by changing the configuration and properties It is possible to flexibly change those rules.
As an example, change the Java source and resource placement conventions for the main source set.
This time, build a project with the following configuration.
Project structure
<project-root>
└ src
├ Mapping.xml
├ app.properties
└ examples
└ Dummy.java
In Gradle, Java source is under src / main / java directory, Resources are expected to be located under the src / main / resources directory.
However, in the above project, Java sources and resources are put together in the src directory. Therefore, by setting the Java source and resource placement path in the build script Build a project that doesn't follow the default conventions.
apply plugin: 'java'
sourceSets {
main {
java {
// sourceSets.main.java.Add settings to srcDirs property
// =Set the Java source location
srcDir 'src'
}
resources {
// sourceSets.test.java.Add settings to srcDirs property
// =Set the resource placement destination
srcDir 'src'
}
}
}
processResources {
// .Exclude directories that do not have anything other than java files from copying
includeEmptyDirs = false
}
The intent is a unit for grouping and classifying dependencies. Actually, only the path referenced by the task (= task processing target) is set.
The Java plug-in configurations are as follows.
For example, if you want to set a library dependency, Suppose you want to apply JUnit library additions only to test-related tasks. In that case, to set the library dependencies only for the compileTestJava task Make settings for the testCompile configuration.
dependencies {
testCompile 'junit:junit:4.11'
}
You can add the source set by writing the following in the build script.
sourceSets {
exampleSet {
java.srcDir file('src/example/java')
}
When you add the source set, the following configurations are automatically added.
To set the above configuration, write the following in the build script.
sourceSets {
exampleSet {
compileClasspath = 'src/ex/java'
runtimeClasspath = 'build/ex/classes'
}
}
When you add the source set, the following tasks are added automatically.
Below, through the addition of a source set (integrationTest) for performing integration tests Try adding a Test type task.
sourceSets {
integrationTest {
//Test code for integration test (Java source)Set the placement destination of
java.srcDir file('src/intTest/java')
//See the output class of the main source set and the compile-time classpath of the test source set at compile time
compileClasspath = sourceSets.main.output + configration.testCompile
//See your own output class at runtime, the compile-time classpath, and the same classpath as the test source set at runtime
runtimeClasspath = output + compileClasspath + configradion.testCompile
}
}
task doIntegrationTest(type: Test) {
//Test the output of the source set integrationTest
testClassDir = sourceSets.integrationTest.output.classDir
//Set the integrationTest runtimeClasspath in the classpath when running the test
classpath = sourceSets.integrationTest.runtimeClasspath
This article is written with reference to the following books.
Reference book: Thorough introduction to Gradle Building an automation platform with next-generation build tools
Recommended Posts