2024-07-02
一言
要保持希望在每天清晨太阳升起。 --- 《自己》 · 惜于顾
Gradle获取配置项
Action
和 Property
的用法样例
1. 定义 ForcedTypeConfig
类
保持不变:
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. 修改 DatabaseConfig
类
在DatabaseConfig
类中使用ObjectFactory
的newInstance
方法创建新的ForcedTypeConfig
实例:
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; // 添加 ObjectFactory 字段
@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; // 初始化 ObjectFactory 字段
}
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); // 使用 ObjectFactory 创建实例
action.execute(forcedTypeConfig);
this.forcedTypes.add(forcedTypeConfig);
}
}
3. 修改 GeneratorConfig
类
保持不变:
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. 修改 JooqConfig
类
保持不变:
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. 在 build.gradle
中配置
保持不变:
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'
}
}
}
}
}
解释
ObjectFactory
:在DatabaseConfig
类中,添加了一个ObjectFactory
字段,并在构造函数中初始化它。ObjectFactory
用于创建由Gradle管理的对象。newInstance
方法:在forcedType
方法中,使用objects.newInstance(ForcedTypeConfig.class)
来创建新的ForcedTypeConfig
实例。这确保了对象是由Gradle管理的,并且符合Gradle的惯用方法。- 配置DSL:保持
build.gradle
文件不变,以DSL语法配置多个forcedType
条目。每个forcedType
配置块都会调用DatabaseConfig
类中的forcedType
方法,从而添加一个新的ForcedTypeConfig
实例到列表中。
通过这些更改,你可以在JooqConfig
类中获取generationTool
中的各个属性值,包括多个forcedType
配置,并进行进一步的处理。每次调用forcedType
方法都会创建一个新的ForcedTypeConfig
实例,并将其添加到forcedTypes
列表中。