可用版本: 开发版 (3.21) | 最新版 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

使用 Maven 运行代码生成器

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

使用 Maven 运行代码生成器与独立模式运行代码生成器之间没有实质性区别。两种模式都使用完全相同的 <configuration/> 元素。Maven 插件配置在此周围添加了一些额外的样板代码。

<plugin>
  <!-- Specify the maven code generator plugin -->
              <!-- Use org.jooq                for the Open Source Edition
                       org.jooq.pro            for commercial editions with Java 21 support,
                       org.jooq.pro-java-17    for commercial editions with Java 17 support,
                       org.jooq.pro-java-11    for commercial editions with Java 11 support,
                       org.jooq.pro-java-8     for commercial editions with Java 8 support,
                       org.jooq.trial          for the free trial edition with Java 21 support,
                       org.jooq.trial-java-17  for the free trial edition with Java 17 support,
                       org.jooq.trial-java-11  for the free trial edition with Java 11 support,
                       org.jooq.trial-java-8   for the free trial edition with Java 8 support
  
               Note: Only the Open Source Edition is hosted on Maven Central.
                     Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
                     See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk -->

  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen-maven</artifactId>
  <version>3.20.5</version>

  <executions>
    <execution>
      <id>my-id</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>

  <configuration>
    ...
  </configuration>
</plugin>
请注意,在 Maven 中,您可以将配置同时放在插件的 pluginexecution 中。后者尤其有用,如果您有多个执行,如下所示。但是,如果您这样做,请记住,在命令行运行代码生成器时,您可能需要显式提供执行 ID,例如使用 mvn jooq-codegen:generate@my-id

其他 Maven 特定标志

但是,有一些额外的、仅限于 jooq-codegen-maven 插件的 Maven 特定标志可以指定。

<plugin>
  <configuration>

    <!-- A boolean property (or constant) can be specified here to tell the plugin not to do anything -->
    <skip>${skip.jooq.generation}</skip>

    <!-- Instead of providing an inline configuration here, you can specify an external XML configuration file here -->
    <configurationFile>${externalfile}</configurationFile>

    <!-- Alternatively, you can provide several external configuration files. These will be merged by using
         Maven's combine.children="append" policy -->
    <configurationFiles>
      <configurationFile>${file1}</configurationFile>
      <configurationFile>${file2}</configurationFile>
      <configurationFile>...</configurationFile>
    </configurationFiles>
  </configuration>
</plugin>

Maven 插件继承机制

与任何其他 Maven 插件配置一样,您可以利用 Maven 强大的插件继承机制。当您为 jOOQ 配置多个代码生成运行时,这尤其有用,例如:

  • 不同租户的不同配置
  • 不同模式的不同配置
  • 不同环境的不同配置

一种方法是使用 <pluginManagement/> 配置。

<pluginManagement>
  <plugins>
    <plugin>
                  <!-- Use org.jooq                for the Open Source Edition
                           org.jooq.pro            for commercial editions with Java 21 support,
                           org.jooq.pro-java-17    for commercial editions with Java 17 support,
                           org.jooq.pro-java-11    for commercial editions with Java 11 support,
                           org.jooq.pro-java-8     for commercial editions with Java 8 support,
                           org.jooq.trial          for the free trial edition with Java 21 support,
                           org.jooq.trial-java-17  for the free trial edition with Java 17 support,
                           org.jooq.trial-java-11  for the free trial edition with Java 11 support,
                           org.jooq.trial-java-8   for the free trial edition with Java 8 support
      
                   Note: Only the Open Source Edition is hosted on Maven Central.
                         Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
                         See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk -->

      <groupId>org.jooq</groupId>
      <artifactId>jooq-codegen-maven</artifactId>
      <configuration>

        <!-- Log at WARN level by default -->
        <logging>WARN</logging>
        <generator>
          <generate>

            <!-- Never generate deprecated code -->
            <deprecated>false</deprecated>
          </generate>
          <database>
            <forcedTypes>

              <!-- Use BIGINT for all ID columns -->
              <forcedType>
                <name>BIGINT</name>
                <includeExpression>ID</includeExpression>
              </forcedType>
            </forcedTypes>
          </database>
        </generator>
      </configuration>
    </plugin>
  </plugins>
</pluginManagement>

上述配置现在默认应用于您所有 jooq-codegen-maven 插件配置。现在,在某些模块或执行中,您可能希望增强/覆盖上述默认值。

<plugin>
  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen-maven</artifactId>
  <configuration>

    <!-- Log at INFO level by in this particular configuration -->
    <logging>INFO</logging>
    <generator>
      <database>
        <!-- Append additional forced type children to the default, rather
             than merging or replacing the default -->
        <forcedTypes combine.children="append">
          <forcedType>
            <name>BIGINT</name>
            <includeExpression>.*_ID</includeExpression>
          </forcedType>
        </forcedTypes>
      </database>
    </generator>
  </configuration>
</plugin>

另一种方法是使用多个执行。

多重执行

与任何 Maven 插件一样,jOOQ 的 Maven 代码生成插件可以执行多次,并在每次执行中继承通用配置。

<plugin>
              <!-- Use org.jooq                for the Open Source Edition
                       org.jooq.pro            for commercial editions with Java 21 support,
                       org.jooq.pro-java-17    for commercial editions with Java 17 support,
                       org.jooq.pro-java-11    for commercial editions with Java 11 support,
                       org.jooq.pro-java-8     for commercial editions with Java 8 support,
                       org.jooq.trial          for the free trial edition with Java 21 support,
                       org.jooq.trial-java-17  for the free trial edition with Java 17 support,
                       org.jooq.trial-java-11  for the free trial edition with Java 11 support,
                       org.jooq.trial-java-8   for the free trial edition with Java 8 support
  
               Note: Only the Open Source Edition is hosted on Maven Central.
                     Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org
                     See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk -->

  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen-maven</artifactId>

  <!-- Common configuration inherited by all executions -->
  <configuration>

    <!-- Log at WARN level by default -->
    <logging>WARN</logging>
    <generator>
      <generate>

        <!-- Never generate deprecated code -->
        <deprecated>false</deprecated>
      </generate>
      <database>
        <forcedTypes>

          <!-- Use BIGINT for all ID columns -->
          <forcedType>
            <name>BIGINT</name>
            <includeExpression>ID</includeExpression>
          </forcedType>
        </forcedTypes>
      </database>
    </generator>
  </configuration>

  <!-- Multiple executions with their own, specific configurations -->
  <executions>
    <execution>
      <id>generate-tenant-1</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        ...
      </configuration>
    <execution>

    <execution>
      <id>generate-tenant-2</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        ...
      </configuration>
    <execution>
  </executions>
</plugin>

有关更多详细信息,请参阅 Maven 文档

反馈

您对此页面有任何反馈吗? 我们非常乐意听到!

The jOOQ Logo