6

I have a Maven project where I generate the JPA metamodel using the Hibernate metamodel generator.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>xxx</groupId>
        <artifactId>xxx</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <artifactId>xxx</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <!-- needed for meta model generation (see also compiler plugin config) -->
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>4.3.8.Final</version>
        </dependency>
    </dependencies>
</project>

The AspectJ compiler is configured in the parent project. When I run Maven, the Java compiler plugin is called first and generates the sources to target/generated-sources/generated-sources/annotations correctly. Then the AspectJ plugin is executed which generates the sources again, now into the root folder of my project and throws the following errors:

D:\xxx\git\xxx>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xxx 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ xxx ---
[INFO] Deleting D:\...
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ xxx ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 63 source files to D:\xxx\git\xxx\target\classes
[INFO]
[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ xxx ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] Hibernate JPA 2 Static-Metamodel Generator 4.3.8.Final
        <unknown source file>:<no line information>

[ERROR] The type Category_ is already defined
        D:\xxx\git\xxx\Category_.java:10
public abstract class Category_ extends com.xxx.AbstractEntity_ {
                      ^^^^^^^^

[ERROR] The type Attachment_Message_ is already defined
         D:\xxx\git\xxx\Attachment_Message_.java:9
public abstract class Attachment_Message_ extends com.xxx.AbstractEntity_ {
                      ^^^^^^^^^^^^^^^^^^

[ERROR] The type AbstractNamedEntity_ is already defined
         D:\xxx\git\xxx\AbstractNamedEntity_.java:9
public abstract class AbstractNamedEntity_ extends com.xxx.AbstractEntity_ {

...

How can I hinder the AspectJ compiler executing the model geneator a second time?

Jens Baitinger
  • 2,230
  • 14
  • 34
  • Which AspectJ version do you use? (I am not asking for the aspectj-maven-plugin version, because I can see that this is 1.7) – Ralph Feb 18 '15 at 18:57

2 Answers2

4

The point is that is that AspectJ 1.8.2 is the fist version that include the Annotation Processing Feature that causes the generation.

There are two ways to disable the generation:

  • 1) Upgrade you Maven-AspectJ-Plugin Version 1.8, it has a new parameter: proc, set it to none to disable the generation. see GitHub Issue #5,

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.8</version>
        ...
        <configuration>
            ...
           <proc>none</proc>
       </configuration>
    </plugin>
    

    or

  • 2) One other "solution" would be to use AspectJ Version 1.8.1, but not 1.8.2+.

Ralph
  • 118,862
  • 56
  • 287
  • 383
1

I excluded the model classes from aspectj compiler. This disables Aspects for the model classes (currently not needed) but also the double generation of the meta model classes:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <sources>
                    <source>
                        <basedir>src/main/java</basedir>
                        <excludes>
                            <exclude>**/domain/*</exclude>
                        </excludes>
                    </source>
                </sources>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
Jens Baitinger
  • 2,230
  • 14
  • 34
  • This seems to be a workaround rather than the solution you were looking for. If you are still interested in a solution which would also work if your model classes needed aspect weaving, please update your question and show the actual parent POM. I would like to understand how and where a compiler would generate sources at all (I am not a Hibernate user). Once I do, I might be able to propose a solution. – kriegaex Jan 19 '15 at 14:57
  • Actually, this is a workaround, excluding the domain classes from weaving (actually there are currently no aspects that regards the model classes). The Hibernate Metamodel generator (http://hibernate.org/orm/tooling/) uses the JPA annotations and generates the meta model classes. It works like a plugin to the compiler. When it is in the dependencies it generates the classes. I never tried to figure out how that works, but it might be interesting to know. – Jens Baitinger Jan 20 '15 at 10:48
  • Have you seen [this bug report](https://jira.codehaus.org/browse/MASPECTJ-137), describing a problem explained in more detail [here](https://haskovec.com/2014/12/04/maven-compiler-plugins-aspectj-hibernate-metamodel-generator/)? My hints about the AspectJ Maven parameters *Xset* or *argumentFileName* might lead to viable workarounds, but this is just a wild guess. Do you have an [SSCCE](http://sscce.org/) for me to test against? – kriegaex Jan 20 '15 at 13:35
  • Bug report can now be found here: https://github.com/mojohaus/aspectj-maven-plugin/issues/5 – Dave Aug 25 '15 at 03:40