【问题标题】:Ant / Jooq Generator - path to jooq libraries imported with mavenAnt / Jooq Generator - 使用 maven 导入的 jooq 库的路径
【发布时间】:2018-07-09 20:20:17
【问题描述】:

我有一个多模块 maven 项目,我正在实施一个 ant 任务以直接从 jpa 实体生成 jooq 类。

这些是我指的教程:

给我错误的我的 ant 任务是这个(我相信是因为没有设置类路径):

<target name="02-gen-jooq">
    <java   classname="org.jooq.util.GenerationTool"
            fork="true"
            failonerror="true"
            logerror="true">

        <arg value="/jooq-config.xml"/> <!-- my jooq config file in project root folder -->
        <classpath>
            <!--
            <pathelement location="?"/> // what to put here??
            <pathelement location="?"/>
            <pathelement location="?"/>
            -->
        </classpath>
    </java>
</target>

给出的错误是:

错误:无法找到或加载主类 org.jooq.util.GenerationTool

在教程中,classpath设置如下:

<pathelement location="/path/to/jooq-3.5.4.jar"/>

但似乎这些库是手动导入的。如果库是用maven导入的,我应该放什么?

【问题讨论】:

  • 愚蠢的问题,当你有 Maven 时为什么还要使用 Ant?
  • @SimonMartinelli 我使用 Ant 创建可定制的构建步骤,而 Maven 只是用于下载库

标签: java maven ant jooq


【解决方案1】:

独立使用 Ant

如果您想在 Maven 之外触发代码生成,您必须将所有这些 jar 文件放在您的 ant 类路径中:

  • JDBC 驱动程序
  • jooq-{版本}.jar
  • jooq-meta-{版本}.jar
  • jooq-codegen-{version}.jar

另请参阅: https://www.jooq.org/doc/latest/manual/code-generation/codegen-configuration

在 Maven 中使用 Ant

您链接的手册部分显示了如何使用 maven-antrun-plugin:

<!-- Run the code generation task -->
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <configuration>
        <tasks>
          <java fork="true" 
                classname="org.jooq.util.GenerationTool"
                classpathref="maven.compile.classpath">
              <arg value="/path/to/configuration.xml"/>
          </java>
       </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>

  <dependencies>
    <dependency>
      <!-- JDBC driver -->
    </dependency>
    <dependency>
      <groupId>org.jooq</groupId>
      <artifactId>jooq-codegen</artifactId>
      <version>${jooq.version}</version>
    </dependency>
  </dependencies>
</plugin>

重要的一行是:

classpathref="maven.compile.classpath"

这样,Maven 类路径被传递给运行 jOOQ 代码生成器的 ant 进程。如果将此插件放在 Maven 配置文件中,则可以从命令行显式运行它,否则不会影响 Maven 构建生命周期。

当然,您最好使用此处记录的 jooq-codegen-maven 插件:

https://www.jooq.org/doc/latest/manual/code-generation/codegen-maven

【讨论】:

  • 这不会在我构建的任何时候生成jooq 源吗?我的问题的目标是拥有一只蚂蚁target,我可以在需要时随时单独点击。请注意,我对 Maven 构建的了解非常有限
  • 您可以使用 Maven 配置文件,您可以使用 -P profile name 显式调用该配置文件。如果您将代码生成输出指定在 /src/main/java 目录内(而不是 /target),那么这将完全独立于您的 Maven 构建生命周期。一般来说,从 ant 升级到 Maven 真的很值得。不过,我会在答案中添加一些额外的部分来回答仅限 ant 的配置。
  • 对于未来的读者,我已经切换到maven,只能从中受益
猜你喜欢
  • 2014-06-04
  • 2016-06-12
  • 2020-04-30
  • 2019-03-26
  • 2014-10-09
  • 2022-11-22
  • 2013-08-13
  • 2022-09-30
  • 2014-12-28
相关资源
最近更新 更多