2
votes

I have some entity classes in Java and some in Kotlin. I just added the following to my pom.xml:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

IntelliJ now shows metamodel classes for the java entities, but not the kotlin ones. So I added kapt to no effect:

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>test-compile</goal>
            </goals>
        </execution>
        <execution>
            <id>kapt</id>
            <goals>
                <goal>kapt</goal>
            </goals>
            <configuration>
                <sourceDirs>
                    <sourceDir>src/main/java</sourceDir>
                </sourceDirs>
                <annotationProcessorPaths>
                    <!-- Specify your annotation processors here. -->
                    <annotationProcessorPath>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>${hibernate.version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <jvmTarget>11</jvmTarget>
        <args>
            <arg>-Xjvm-default=enable</arg>
            <!--<arg>-XXLanguage:+JvmStaticInInterface</arg>-->
        </args>
        <compilerPlugins>
            <plugin>jpa</plugin>
<!--                        <plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin>-->
        </compilerPlugins>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
</plugin>

I tried adding <plugin>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</plugin> to no apparent effect. I tried enabling annotation processing in IntelliJ IDEA: IntelliJ Settings for Annotation Processing

Oh. I can see the generated files for the Kotlin classes in target/generated-sources/kapt/compile/ instead of target/generated-sources/annotations/. This may be as simple as setting the appropriate output directory.

Now when I run mvn clean compile I get errors just for the Java entity classes, saying that the annotations have already been run (presumably by kapt) and classes created:

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MyProject ---
[INFO] Deleting /myproject/target
[INFO] 
[INFO] --- kotlin-maven-plugin:1.3.70:kapt (kapt) @ MyProject ---
[WARNING] 'tools.jar' was not found, kapt may work unreliably
[INFO] Applied plugin: 'jpa'
[INFO] Note: Hibernate JPA 2 Static-Metamodel Generator 5.4.12.Final
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 200 resources
[INFO] 
[INFO] --- kotlin-maven-plugin:1.3.70:compile (compile) @ MyProject ---
[INFO] Applied plugin: 'jpa'
[WARNING] Duplicate source root: /myproject/target/generated-sources/kapt/compile
[WARNING] Duplicate source root: /myproject/target/generated-sources/kaptKotlin/compile
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (java-compile) @ MyProject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 261 source files to /myproject/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.SomeJavaClass_
[ERROR] Problem with Filer: Attempt to recreate a file for type my.project.db.AnotherJavaClass_
...
1
Is your problem solved?Simon Martinelli
No. I don't know how to set the output directory. Nor do I know if that will actually solve it.GlenPeterson
You don't have to set the output directory. This should be used by the compiler . Do you have problems compiling with Maven or only in Eclipse`Simon Martinelli
Problems in both Maven and IntelliJ (not Eclipse).GlenPeterson

1 Answers

0
votes

I deleted the project dependency on hibernate-jpamodelgen AND the Kotlin plugin dependency on the same, leaving it mentioned only in the annotationProcessorPath for kapt. Maven completed successfully.

<!-- Java has a fit if Kapt processes this first,
     so just let Kapt do it and don't tell Java about it. -->
<!--        <dependency>-->
<!--            <groupId>org.hibernate</groupId>-->
<!--            <artifactId>hibernate-jpamodelgen</artifactId>-->
<!--            <version>${hibernate.version}</version>-->
<!--        </dependency>-->

I also went back into the IntelliJ settings window, removed the JPAMetaModelEntityProcessor and unchecked "annotation processing." IntelliJ seems to be happy now too.