简体   繁体   中英

How to fix jOOQ code generation tool error?

I'm using Spring Boot 2.1.1 with jOOQ codegen tool 3.11.7. I have a Java class configured to slightly modify class names derived from MySQL table names in pom.xml:

<generator>
  <target>
    <packageName>com.example.foglight.db</packageName>
    <directory>src/main/java</directory>
  </target>
  <database>
    <excludes>
      flyway_schema_history
      | information_schema.*
    </excludes>
    <inputSchema>${dbName}</inputSchema>
    <outputSchemaToDefault>true</outputSchemaToDefault>
    <forcedTypes>
      <forcedType>
        <userType>java.util.UUID</userType>
        <binding>com.example.foglight.config.db.MysqlUuidBinding</binding>
        <types>BINARY\(16\)</types>
      </forcedType>
    </forcedTypes>
  </database>
  <generate>
    <deprecationOnUnknownTypes>false</deprecationOnUnknownTypes>
    <pojos>true</pojos>
  </generate>
  <!-- The default code generator. You can override this one, to generate your own code style
        Defaults to org.jooq.codegen.JavaGenerator -->
  <name>org.jooq.codegen.JavaGenerator</name>
  <!-- The naming strategy used for class and field names.
        You may override this with your custom naming strategy. Some examples follow
        Defaults to org.jooq.codegen.DefaultGeneratorStrategy -->
  <strategy>
 <name>com.example.foglight.config.db.DatabaseModelNamingStrategy</name>
  </strategy>
</generator>

Everything works fine when I'm building/running the app from IntelliJ, however when I run mvn generate-sources or mvn install from command line in the very same environment, I'm getting the following error:

[ERROR] Failed to execute goal org.jooq:jooq-codegen-maven:3.11.7:generate (default) on project foglight: Error running jOOQ code generation tool: com.example.foglight.config.db.DatabaseModelNamingStrategy -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jooq:jooq-codegen-maven:3.11.7:generate (default) on project foglight: Error running jOOQ code generation tool

With detailed explanation below:

Caused by: java.lang.ClassNotFoundException: com.example.foglight.config.db.DatabaseModelNamingStrategy
    at java.net.URLClassLoader.findClass (URLClassLoader.java:471)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
    at org.jooq.codegen.GenerationTool.loadClass (GenerationTool.java:819)
    at org.jooq.codegen.GenerationTool.run (GenerationTool.java:352)
    at org.jooq.codegen.GenerationTool.generate (GenerationTool.java:220)
    at org.jooq.codegen.maven.Plugin.execute (Plugin.java:197)

The class is there (otherwise IntelliJ would be throwing the error too). Is there anything more that IDE is doing under the hood that makes it work?

You could compile and put the class on the classpath as suggested by Lukas Eder. For that, introduce another execution with the maven-compiler-plugin and bind it to the generate-sources phase:

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.8.1</version>
         <executions>
         <execution>
              <phase>generate-sources</phase>
              <goals>
                <goal>compile</goal>
              </goals>
             <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <includes>
                   <include>tb/database/jooq/custom/namingstrategies/*.java</include>
                </includes>
             </configuration>
         </execution>
         </executions>
      </plugin>

The jOOQ code generator must have access to your generator strategy via the classpath, which means it has to have been compiled before you run the code generator. Since the code generator usually runs before the compilation phase of the Maven module that contains it, you will have to extract your generator strategy into another module to make sure it gets compiled before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM