2024-07-02
One-liner
Always keep hope when the sun rises every morning. --- "Oneself" · Xi Yu Gu
Getting Configuration Items in Gradle
Examples of using Action and Property
1. Define the ForcedTypeConfig Class
Unchanged:
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import javax.inject.Inject;
public class ForcedTypeConfig {
private final Property<String> name;
private final Property<String> includeExpression;
private final Property<String> includeTypes;
@Inject
public ForcedTypeConfig(ObjectFactory objects) {
this.name = objects.property(String.class);
this.includeExpression = objects.property(String.class);
this.includeTypes = objects.property(String.class);
}
public Property<String> getName() {
return name;
}
public Property<String> getIncludeExpression() {
return includeExpression;
}
public Property<String> getIncludeTypes() {
return includeTypes;
}
}
2. Modify the DatabaseConfig Class
Use the newInstance method of ObjectFactory to create new ForcedTypeConfig instances in the DatabaseConfig class:
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import javax.inject.Inject;
public class DatabaseConfig {
private final Property<String> name;
private final Property<String> inputSchema;
private final ListProperty<ForcedTypeConfig> forcedTypes;
private final ObjectFactory objects; // Add ObjectFactory field
@Inject
public DatabaseConfig(ObjectFactory objects) {
this.name = objects.property(String.class);
this.inputSchema = objects.property(String.class);
this.forcedTypes = objects.listProperty(ForcedTypeConfig.class);
this.objects = objects; // Initialize ObjectFactory field
}
public Property<String> getName() {
return name;
}
public Property<String> getInputSchema() {
return inputSchema;
}
public ListProperty<ForcedTypeConfig> getForcedTypes() {
return forcedTypes;
}
public void forcedType(Action<? super ForcedTypeConfig> action) {
ForcedTypeConfig forcedTypeConfig = objects.newInstance(ForcedTypeConfig.class); // Use ObjectFactory to create an instance
action.execute(forcedTypeConfig);
this.forcedTypes.add(forcedTypeConfig);
}
}
3. Modify the GeneratorConfig Class
Unchanged:
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import javax.inject.Inject;
public class GeneratorConfig {
private final Property<String> name;
private final DatabaseConfig database;
@Inject
public GeneratorConfig(ObjectFactory objects) {
this.name = objects.property(String.class);
this.database = objects.newInstance(DatabaseConfig.class);
}
public Property<String> getName() {
return name;
}
public DatabaseConfig getDatabase() {
return database;
}
public void database(Action<? super DatabaseConfig> action) {
action.execute(database);
}
}
4. Modify the JooqConfig Class
Unchanged:
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import javax.inject.Inject;
public class JooqConfig {
private final Property<String> logging;
private final JdbcConfig jdbc;
private final GeneratorConfig generator;
@Inject
public JooqConfig(ObjectFactory objects) {
this.logging = objects.property(String.class);
this.jdbc = objects.newInstance(JdbcConfig.class);
this.generator = objects.newInstance(GeneratorConfig.class);
}
public Property<String> getLogging() {
return logging;
}
public JdbcConfig getJdbc() {
return jdbc;
}
public GeneratorConfig getGenerator() {
return generator;
}
public void jdbc(Action<? super JdbcConfig> action) {
action.execute(jdbc);
}
public void generator(Action<? super GeneratorConfig> action) {
action.execute(generator);
}
}
5. Configure in build.gradle
Unchanged:
jooq {
configurations {
main {
generateSchemaSourceOnCompilation = true
generationTool {
logging = 'WARN'
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:5432/sample'
user = 'some_user'
password = 'some_secret'
properties {
property {
key = 'ssl'
value = 'true'
}
}
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
forcedTypes {
forcedType {
name = 'varchar'
includeExpression = '.*'
includeTypes = 'JSONB?'
}
forcedType {
name = 'varchar'
includeExpression = '.*'
includeTypes = 'INET'
}
}
}
target {
packageName = 'nu.studer.sample'
directory = 'build/generated-src/jooq/main'
}
strategy.name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
}
}
}
}
Explanation
ObjectFactory: In theDatabaseConfigclass, anObjectFactoryfield was added and initialized in the constructor.ObjectFactoryis used to create objects managed by Gradle.newInstanceMethod: In theforcedTypemethod,objects.newInstance(ForcedTypeConfig.class)is used to create a newForcedTypeConfiginstance. This ensures the object is managed by Gradle and follows Gradle's idiomatic approach.- Configuration DSL: The
build.gradlefile remains unchanged to configure multipleforcedTypeentries using the DSL syntax. EachforcedTypeconfiguration block calls theforcedTypemethod in theDatabaseConfigclass, adding a newForcedTypeConfiginstance to the list.
With these changes, you can retrieve each property value within the generationTool in the JooqConfig class, including multiple forcedType configurations, for further processing. Each call to the forcedType method creates a new ForcedTypeConfig instance and adds it to the forcedTypes list.