Tested on Java LTS versions from 11 to 21.
Tested on Gradle versions from 7.1 to 8.13.
plugins {
id 'name.remal.generate-sources' version '2.0.0-rc-3'
}
This plugin simplifies sources generation for JVM projects. It helps with registering all the necessary dependencies and makes generated sources visible to an IDE.
Basic configuration looks like this:
generateSources {
forMainSourceSet {
java { /* ... */ } // configure Java sources generation
resources { /* ... */ } // configure resources generation
groovy { /* ... */ } // configure Groovy sources generation
}
forSourceSet(sourceSets.test) { /* ... */ } // to generate sources for other source sets (`test` in this case)
}
This configuration
generateSources.forMainSourceSet.java {
classFile('pkg', 'Logic') {
addStaticImport('java.util.Arrays', 'asList')
addImport('java.util.List')
block("public class ${simpleName}") {
line()
suppressWarningsLine('unchecked', 'rawtypes')
block('public static List execute()') {
line('return asList(')
ident {
lines(
"\"${escapeString('multi\bline\nstring')}\"", // `escapeString` will escape Java string
'2',
)
}
line(');')
}
line()
}
}
}
... generates build/generated/generateJava/pkg/Logic.java
file:
package pkg;
import static java.util.Arrays.asList;
import java.util.List;
public class Logic {
@SuppressWarnings({"unchecked", "rawtypes"})
public static List execute() {
return asList(
"multi\bline\nstring",
2
);
}
}
The generated file will be compiled by the compileJava
task.
This configuration
generateSources.forMainSourceSet.resources {
textFile('file.txt') {
line('This is a text file')
line('with many lines')
}
}
... generates build/generated/generateResources/file.txt
file:
This is a text file
with many lines
The generated file will be processed by the processResources
task.
The delegate object of the generateSources.forMainSourceSet.resources.textFile {}
closure
is an extended java.io.BufferedWriter
with these additional methods:
writeFormat(String format, Object... args)
- creates a string usingString.format()
and passes it to thewrite()
copyFrom(java.io.Reader reader)
- copy the content from the readernewLine()
- overridesjava.io.BufferedWriter.newLine()
by writing a line separator from.editorconfig
or\n
by default (the default implementation writes system line separator that depend on the current OS)line(String line)
-write(line)
+newLine()
line(String format, Object... args)
- creates a string usingString.format()
and passes it toline()
line()
- executesnewLine()
The goals of the additional methods are to simplify the usage of java.io.BufferedWriter
(by adding writeFormat()
and copyFrom()
methods)
and to make usage experience consistent with classes generation (by adding line()
methods).
This configuration
generateSources.forMainSourceSet.resources {
binaryFile('file.bin') {
write([1, 2, 3] as byte[])
}
}
... generates build/generated/generateResources/file.bin
file with the content of three bytes: 1
, 2
, and 3
.
The generated file will be processed by the processResources
task.
The delegate object of the generateSources.forMainSourceSet.resources.binaryFile {}
closure
is an extended java.io.BufferedOutputStream
with this additional method:
copyFrom(java.io.InputStream inputStream)
- copy the content from the input stream
The goal of the additional method is to simplify the usage of java.io.BufferedOutputStream
(by adding copyFrom()
method).
This configuration
generateSources.forMainSourceSet.groovy {
classFile('pkg', 'Logic') {
addStaticImport('java.util.Arrays', 'asList')
addImport('java.util.List')
block("class ${simpleName}") {
line()
suppressWarningsLine('unchecked', 'rawtypes')
block('static List execute()') {
line('return asList(')
ident {
lines(
"\"${escapeString('multi\bline\nstring')}\"", // `escapeString` will escape Groovy string
'2,',
)
}
line(')')
}
line()
}
}
}
... generates build/generated/generateGroovy/pkg/Logic.groovy
file:
package pkg
import static java.util.Arrays.asList
class Logic {
@SuppressWarnings(["unchecked", "rawtypes"])
static List execute() {
return asList(
"multi\bline\nstring",
2,
)
}
}
The generated file will be compiled by the compileGroovy
task.
The plugin was fully rewritten. There were no intentions to keep it backward compatible.