Source sets and the layout
A source set is a group of sources with a class path of its own. The java plugin creates two of them, main and test, each with a folder for code and a folder for resources, and generates a task for each folder. A third source set is five lines, and it comes with its tasks for free.
The two source sets
| source set | code | resources | its tasks |
|---|---|---|---|
| main | src/main/java | src/main/resources | compileJava, processResources |
| test | src/test/java | src/test/resources | compileTestJava, processTestResources, test |
The output follows the name: build/classes/java/main, build/classes/java/test, build/resources/main. The test source set sees the output of the main one, which is why a test can use a class of the project without a dependency.
Resources
A file in src/main/resources is copied into the jar and is on the class path at run time, which is where a config file, a message bundle or a template belongs. The copy is a task like any other, so a changed file means a changed output and a jar that is rebuilt.
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
}
sourceSets {
tools {
java.srcDir 'src/tools/java'
compileClasspath += sourceSets.main.output
}
}
The sourceSets block adds the third set of the next section; the two files below belong to the main source set.
src/main/resources/shop.properties
greeting=Hello from a resource
src/main/java/com/example/Greeting.java
package com.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public final class Greeting {
public static String read() throws IOException {
Properties properties = new Properties();
try (InputStream in = Greeting.class.getResourceAsStream("/shop.properties")) {
properties.load(in);
}
return properties.getProperty("greeting");
}
}
A source set of your own
A tool that is not part of the application, a set of integration tests, a generated client: each of them is a source set with a folder of its own. The sourceSets block of the build script above adds one called tools, with the output of the main set on its class path.
src/main/java/com/example/Price.java
package com.example;
public final class Price {
public static String format(long cents) {
return String.format("%d.%02d", cents / 100, cents % 100);
}
}
src/tools/java/com/example/Report.java
package com.example;
public final class Report {
public static String price(long cents) {
return "price: " + Price.format(cents);
}
}
Report uses Price of the main source set, which is what the compileClasspath line makes possible.
The tasks of the new source set
gradle tasks --all --group build --console=plain --offline --no-daemon 2>&1 | grep -i tools
toolsClasses - Assembles tools classes.
compileToolsJava - Compiles tools Java source.
processToolsResources - Processes tools resources.
Two tasks per source set, generated with the name of the set in the middle: compileToolsJava for the code and processToolsResources for the resources. Gradle writes them in the same style as the two sets of the plugin, so a build with five source sets reads like a build with two.
gradle compileToolsJava --console=plain --offline --no-daemon
> Task :compileJava
> Task :processResources
> Task :classes
> Task :compileToolsJava
BUILD SUCCESSFUL in 13s
3 actionable tasks: 3 executed
Related
- The java plugin and its lifecycle: the tasks the two standard source sets produce.
- Configurations: the class paths a source set is compiled against.
- The standard directory layout in Maven: the same folders, and no way to add a third one without a plugin.
Автор статьи: Arthur Isaev