Skip to content

Commit c3a5611

Browse files
nakamura-toclaude
andcommitted
Add domaCodeGen configuration for improved dependency management
Introduces a new Gradle configuration to allow users to specify JDBC drivers and other dependencies separately from their main project dependencies. This improves dependency isolation and makes it easier to configure the plugin without polluting the main classpath. Changes: - Add domaCodeGen configuration in CodeGenPlugin - Update CodeGenConfig to use custom ClassLoader with configuration dependencies - Enhance ClassUtil to support custom ClassLoader for class loading - Update test project to use new configuration and runsql plugin for database setup 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 455657d commit c3a5611

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

codegen-test/build.gradle.kts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import org.h2.Driver
21
import org.seasar.doma.gradle.codegen.desc.LanguageType
3-
import org.seasar.doma.gradle.codegen.jdbc.SimpleDataSource
42

53
buildscript {
64
repositories {
@@ -11,10 +9,7 @@ buildscript {
119
}
1210
}
1311
dependencies {
14-
val h2Version: String = properties["h2Version"] as String
1512
classpath("org.domaframework.doma:codegen")
16-
classpath("com.h2database:h2:$h2Version")
17-
classpath("mysql:mysql-connector-java:8.0.33")
1813
}
1914
}
2015

@@ -23,6 +18,7 @@ plugins {
2318
id("org.jetbrains.kotlin.jvm") version "2.2.0"
2419
id("org.domaframework.doma.compile") version "4.0.0"
2520
id("org.domaframework.doma.codegen")
21+
id("com.nocwriter.runsql") version "1.0.3"
2622
}
2723

2824
java {
@@ -40,13 +36,19 @@ dependencies {
4036

4137
implementation("org.seasar.doma:doma-core:$domaVersion")
4238
annotationProcessor("org.seasar.doma:doma-processor:$domaVersion")
43-
39+
4440
// Use JUnit BOM for version management
4541
testImplementation(platform("org.junit:junit-bom:5.13.2"))
4642
testImplementation("org.junit.jupiter:junit-jupiter-api")
4743
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
4844
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
4945
testRuntimeOnly("com.h2database:h2:$h2Version")
46+
47+
// Add H2 to the domaCodeGen configuration
48+
domaCodeGen("com.h2database:h2:$h2Version")
49+
50+
// Used by runsql-gradle-plugin
51+
runtimeOnly("com.h2database:h2:$h2Version")
5052
}
5153

5254
val _url = "jdbc:h2:file:$projectDir/data/db"
@@ -92,15 +94,13 @@ tasks {
9294
}
9395
}
9496

95-
val createDb = register("createDb") {
96-
doLast {
97-
val ds = SimpleDataSource()
98-
ds.setDriver(Driver())
99-
ds.setUrl(_url)
100-
ds.setUser(_user)
101-
ds.setPassword(_password)
102-
103-
val sql = """
97+
val createDb = register<RunSQL>("createDb") {
98+
config {
99+
username = _user
100+
password = _password
101+
url = _url
102+
driverClassName = "org.h2.Driver"
103+
script = """
104104
drop all objects;
105105
106106
create table department(
@@ -115,13 +115,8 @@ tasks {
115115
employee_id integer not null primary key,
116116
employee_no integer not null,
117117
employee_name varchar(20)
118-
);
119-
"""
120-
ds.connection.use {
121-
it.createStatement().use {
122-
it.execute(sql)
123-
}
124-
}
118+
);
119+
""".trimIndent()
125120
}
126121
}
127122

codegen/src/main/java/org/seasar/doma/gradle/codegen/CodeGenPlugin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public class CodeGenPlugin implements Plugin<Project> {
2121

2222
public static final String EXTENSION_NAME = "domaCodeGen";
23+
public static final String CONFIGURATION_NAME = "domaCodeGen";
2324
public static final String TASK_GROUP_NAME = "Doma Code Generation";
2425
public static final String DB_META_TASK_NAME = "DbMeta";
2526
public static final String DTO_TASK_NAME = "Dto";
@@ -33,6 +34,18 @@ public class CodeGenPlugin implements Plugin<Project> {
3334

3435
@Override
3536
public void apply(Project project) {
37+
project
38+
.getConfigurations()
39+
.create(
40+
CONFIGURATION_NAME,
41+
config -> {
42+
config.setDescription(
43+
"The libraries used by the Doma CodeGen plugin for JDBC drivers and other dependencies");
44+
config.setVisible(false);
45+
config.setCanBeConsumed(false);
46+
config.setCanBeResolved(true);
47+
});
48+
3649
NamedDomainObjectContainer<CodeGenConfig> container =
3750
project.container(
3851
CodeGenConfig.class,

codegen/src/main/java/org/seasar/doma/gradle/codegen/extension/CodeGenConfig.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package org.seasar.doma.gradle.codegen.extension;
22

3+
import static org.seasar.doma.gradle.codegen.CodeGenPlugin.CONFIGURATION_NAME;
4+
35
import java.io.File;
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
48
import java.nio.charset.StandardCharsets;
59
import java.sql.Driver;
610
import java.util.Collections;
711
import java.util.List;
12+
import java.util.Set;
813
import javax.inject.Inject;
914
import javax.sql.DataSource;
1015
import org.gradle.api.Action;
1116
import org.gradle.api.Project;
17+
import org.gradle.api.artifacts.Configuration;
1218
import org.gradle.api.file.Directory;
1319
import org.gradle.api.file.DirectoryProperty;
1420
import org.gradle.api.file.FileTree;
@@ -31,6 +37,8 @@ public class CodeGenConfig {
3137

3238
private final String name;
3339

40+
private final Configuration configuration;
41+
3442
private final Property<GlobalFactory> globalFactory;
3543

3644
private final Property<DataSource> dataSource;
@@ -85,6 +93,8 @@ public class CodeGenConfig {
8593
public CodeGenConfig(String name, Project project) {
8694
this.name = name;
8795

96+
this.configuration = project.getConfigurations().getByName(CONFIGURATION_NAME);
97+
8898
ObjectFactory objects = project.getObjects();
8999

90100
globalFactory = objects.property(GlobalFactory.class);
@@ -146,13 +156,32 @@ private Provider<DataSource> dataSourceProvider() {
146156
if (driverClassName == null) {
147157
throw new CodeGenException(Message.DOMAGEN0024);
148158
}
149-
Driver driver = ClassUtil.newInstance(Driver.class, driverClassName, "driverClassName");
159+
ClassLoader classLoader = createClassLoader();
160+
Driver driver =
161+
ClassUtil.newInstance(Driver.class, driverClassName, "driverClassName", classLoader);
150162
return globalFactory
151163
.get()
152164
.createDataSource(driver, user.getOrNull(), password.getOrNull(), url.get());
153165
});
154166
}
155167

168+
protected ClassLoader createClassLoader() {
169+
if (configuration == null || configuration.isEmpty()) {
170+
return getClass().getClassLoader();
171+
}
172+
Set<File> fileSet = configuration.getFiles();
173+
URL[] urls = new URL[fileSet.size()];
174+
int i = 0;
175+
for (File file : fileSet) {
176+
try {
177+
urls[i++] = file.toURI().toURL();
178+
} catch (Exception e) {
179+
throw new RuntimeException("Failed to convert file to URL: " + file, e);
180+
}
181+
}
182+
return new URLClassLoader(urls, getClass().getClassLoader());
183+
}
184+
156185
private Provider<CodeGenDialect> codeGenDialectProvider() {
157186
return url.map(
158187
it -> {

codegen/src/main/java/org/seasar/doma/gradle/codegen/util/ClassUtil.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public static Class<?> toBoxedPrimitiveTypeIfPossible(Class<?> clazz) {
5757

5858
public static <T> T newInstance(Class<T> supertype, String className, String propertyName) {
5959
AssertionUtil.assertNotNull(supertype, className, propertyName);
60-
Class<?> clazz = forName(className, propertyName);
60+
return newInstance(supertype, className, propertyName, null);
61+
}
62+
63+
public static <T> T newInstance(
64+
Class<T> supertype, String className, String propertyName, ClassLoader classLoader) {
65+
AssertionUtil.assertNotNull(supertype, className, propertyName);
66+
Class<?> clazz = forName(className, propertyName, classLoader);
6167
if (!supertype.isAssignableFrom(clazz)) {
6268
throw new CodeGenException(Message.DOMAGEN0014, propertyName, className, supertype.getName());
6369
}
@@ -69,9 +75,18 @@ public static <T> T newInstance(Class<T> supertype, String className, String pro
6975
}
7076

7177
public static Class<?> forName(String className, String propertyName) {
78+
AssertionUtil.assertNotNull(className, propertyName);
79+
return forName(className, propertyName, null);
80+
}
81+
82+
public static Class<?> forName(String className, String propertyName, ClassLoader classLoader) {
7283
AssertionUtil.assertNotNull(className, propertyName);
7384
try {
74-
return Class.forName(className);
85+
if (classLoader != null) {
86+
return classLoader.loadClass(className);
87+
} else {
88+
return Class.forName(className);
89+
}
7590
} catch (ClassNotFoundException e) {
7691
throw new CodeGenException(Message.DOMAGEN0013, propertyName, className, e);
7792
}

0 commit comments

Comments
 (0)