Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jooq into separate tag #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,20 @@ by using [Testcontainers](https://www.testcontainers.org/) and applying database
You can configure any supporting jooq config here.
see https://www.jooq.org/doc/latest/manual/code-generation/codegen-configuration/
-->
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
<jooq>
<skip>false</skip> <!-- optional -->
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>
</configuration>
</execution>
</executions>
Expand Down
24 changes: 13 additions & 11 deletions examples/mariadb-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@
</locations>
</flyway>
<!-- Generator parameters -->
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>test</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
<jooq>
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>test</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>
</configuration>
</execution>
</executions>
Expand Down
24 changes: 13 additions & 11 deletions examples/mysql-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@
</locations>
</flyway>
<!-- Generator parameters -->
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>test</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
<jooq>
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>test</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>
</configuration>
</execution>
</executions>
Expand Down
29 changes: 16 additions & 13 deletions examples/postgres-flyway-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<testcontainers.version>1.18.1</testcontainers.version>
<jooq-testcontainers-codegen-maven-plugin.version>0.0.2</jooq-testcontainers-codegen-maven-plugin.version>
<jooq-testcontainers-codegen-maven-plugin.version>0.0.2
</jooq-testcontainers-codegen-maven-plugin.version>
<jooq.version>3.18.3</jooq.version>
<postgresql.version>42.6.0</postgresql.version>
</properties>
Expand Down Expand Up @@ -65,18 +66,20 @@
filesystem:src/main/resources/db/migration/postgresql
</locations>
</flyway>
<!-- Generator parameters -->
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
<!-- Generator parameters -->
<jooq>
<generator>
<database>
<includes>.*</includes>
<excludes>flyway_schema_history</excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>
</configuration>
</execution>
</executions>
Expand Down
25 changes: 14 additions & 11 deletions examples/postgres-liquibase-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@
</liquibase>

<!-- Generator parameters -->
<generator>
<database>
<includes>.*</includes>
<excludes>DATABASECHANGELOG</excludes>
<inputSchema>custom</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
<jooq>
<generator>
<database>
<includes>.*</includes>
<excludes>DATABASECHANGELOG</excludes>
<inputSchema>custom</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen.maven.example</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</jooq>

</configuration>
</execution>
</executions>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<name>K Siva Prasad Reddy</name>
<email>sivaprasadreddy.k@gmail.com</email>
</developer>
<developer>
<id>contributor</id>
<name>Roman Oborin</name>
<email>deadok68@gmail.com</email>
</developer>
</developers>

<scm>
Expand Down
80 changes: 14 additions & 66 deletions src/main/java/org/testcontainers/jooq/codegen/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
import static org.apache.maven.plugins.annotations.ResolutionScope.TEST;
import static org.jooq.Constants.XSD_CODEGEN;
import static org.jooq.codegen.GenerationTool.DEFAULT_TARGET_DIRECTORY;
import static org.testcontainers.jooq.codegen.util.OptionalUtils.bothPresent;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.Target;
import org.testcontainers.jooq.codegen.database.DatabaseProps;
import org.testcontainers.jooq.codegen.datasource.TargetDatasource;
import org.testcontainers.jooq.codegen.jooq.JooqGenerator;
import org.testcontainers.jooq.codegen.jooq.JooqProps;
import org.testcontainers.jooq.codegen.migration.runner.FlywayRunner;
import org.testcontainers.jooq.codegen.migration.runner.LiquibaseRunner;
import org.testcontainers.jooq.codegen.migration.runner.MigrationRunner;
Expand All @@ -32,52 +29,46 @@
*/
@Mojo(name = "generate", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = TEST, threadSafe = true)
public class Plugin extends AbstractMojo {

@Parameter(property = "project", required = true, readonly = true)
private MavenProject project;

@Parameter(property = "jooq.codegen.skip")
private boolean skip;

@Parameter(property = "jooq.codegen.basedir")
private String basedir;

@Parameter
private org.jooq.meta.jaxb.Jdbc jdbc;

@Parameter
private org.jooq.meta.jaxb.Generator generator;

@Parameter(required = true)
private DatabaseProps database;

@Parameter(required = true)
private JooqProps jooq;

@Parameter
private FlywayRunner flyway;

@Parameter
private LiquibaseRunner liquibase;

@Inject
private JooqGenerator jooqGenerator;

@Override
public void execute() throws MojoExecutionException {
if (skip) {
if (jooq.skip()) {
getLog().info("Skipping jOOQ code generation");
return;
}

if (database.getType() == null) {
throw new MojoExecutionException("Property 'type' should be specified inside 'database' block");
}
checkGeneratorArguments();

final var oldCL = Thread.currentThread().getContextClassLoader();
final var mavenClassloader = getMavenClassloader();

try (var targetDatasource = TargetDatasource.createOrJoinExisting(jdbc, database)) {
try (var targetDatasource = TargetDatasource.createOrJoinExisting(jooq, database)) {
doExecute(mavenClassloader, targetDatasource);
} catch (Exception ex) {
throw new MojoExecutionException("Error running jOOQ code generation tool", ex);
} finally {
closeClassloader(oldCL, mavenClassloader);
}
project.addCompileSourceRoot(generator.getTarget().getDirectory());
}

private void doExecute(URLClassLoader mavenClassloader, TargetDatasource targetDatasource) throws Exception {
Expand All @@ -89,16 +80,6 @@ private void doExecute(URLClassLoader mavenClassloader, TargetDatasource targetD
mavenClassloader,
project);
Thread.currentThread().setContextClassLoader(mavenClassloader);
String actualBasedir = basedir == null ? project.getBasedir().getAbsolutePath() : basedir;

setGeneratorTargets();

if (jdbc == null) {
jdbc = new Jdbc();
}
jdbc.setUrl(targetDatasource.getUrl());
jdbc.setUser(targetDatasource.getUsername());
jdbc.setPassword(targetDatasource.getPassword());

final var oFlyway = Optional.<MigrationRunner>ofNullable(flyway);
final var oLiquibase = Optional.<MigrationRunner>ofNullable(liquibase);
Expand All @@ -118,15 +99,7 @@ private void doExecute(URLClassLoader mavenClassloader, TargetDatasource targetD

getLog().info("Migrations applied successfully");

final var configuration = new Configuration();
configuration.setJdbc(jdbc);
configuration.setGenerator(generator);
configuration.setBasedir(actualBasedir);

if (getLog().isDebugEnabled()) {
getLog().debug("Using this configuration:\n" + configuration);
}
GenerationTool.generate(configuration);
jooqGenerator.generateSources(targetDatasource, jooq, getLog());
}

private void closeClassloader(ClassLoader oldCL, URLClassLoader mavenClassloader) {
Expand All @@ -138,31 +111,6 @@ private void closeClassloader(ClassLoader oldCL, URLClassLoader mavenClassloader
}
}

private void setGeneratorTargets() {
if (generator.getTarget() == null) {
generator.setTarget(new Target());
}
if (generator.getTarget().getDirectory() == null) {
generator.getTarget().setDirectory(DEFAULT_TARGET_DIRECTORY);
}
}

private void checkGeneratorArguments() throws MojoExecutionException {
if (generator == null) {
getLog().error("Incorrect configuration of jOOQ code generation tool");
getLog().error(
"""
The jOOQ-codegen-maven module's generator configuration is not set up correctly.
This can have a variety of reasons, among which:
- Your pom.xml's <configuration> contains invalid XML according to %s
- There is a version or artifact mismatch between your pom.xml and your commandline"""
.formatted(XSD_CODEGEN));

throw new MojoExecutionException(
"Incorrect configuration of jOOQ code generation tool. See error above for details.");
}
}

private URLClassLoader getMavenClassloader() throws MojoExecutionException {
try {
List<String> classpathElements = project.getRuntimeClasspathElements();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
package org.testcontainers.jooq.codegen.database;

/** Database configuration properties. */
import org.apache.maven.plugins.annotations.Parameter;

/**
* Database configuration properties.
*/
public class DatabaseProps {
/**
* Required
*/
@Parameter(required = true)
private DatabaseType type;

/**
* Optional
*/
@Parameter
private String containerImage;
/**
* Optional
*/
@Parameter
private String username;
/**
* Optional
*/
@Parameter
private String password;
/**
* Optional
*/
@Parameter
private String databaseName;

public DatabaseType getType() {
Expand Down
Loading