15

I have a multi module project. The aspect is currently added to the "core" project. When doing a mvn clean install here it works. However trying to do a mvn clean install on parent project it fails with this error when compiling one of the other projects:

The type org.hibernate.annotations.CacheConcurrencyStrategy cannot be resolved. It is indirectly referenced from required .class files

If I add the Hibernate core dependency in that project too it works, but adding dependencies to projects that shouldn't have the dependency does not make sense - so it is not a solution. When compiling with javac it works fine.

What is the reason? And how can I fix it so I can use the AspectJ compiler without leaking dependencies to projects that shouldn't have that?

I have this configuration in the parent POM:

 <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.5</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Update

I just found out. Running mvn clean install fails every time. However, running mvn [clean] install one time fails. Then running mvn install without clean works. I see that the builddef.lst in the target folder is the reason why it works and fails based on whether or not you run clean. So now my question is: How do you automatically generate this file?

Parent POM-file:

    <?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>core-lib</artifactId>
    <name>core-lib</name>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <complianceLevel>1.6</complianceLevel>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
    </dependencies>

    <modules>
        <module>core-xyz</module>
        <module>core-xyz2</module>
    </modules>
</project>
Knight Rider
  • 219
  • 1
  • 7
  • What's the stacktrace of the exception and how does your aspect look like? – SpaceTrucker Jun 16 '14 at 06:43
  • @KnightRider you should update your Question instead of adding a comment. – Leonard Brünings Jun 16 '14 at 08:40
  • Could you post more details of your pom? Have you also tried with the 1.6 version of the `aspectj-maven-plugin` – M. Deinum Jun 16 '14 at 09:56
  • @M.Deinum Yes, I tried it. – Knight Rider Jun 16 '14 at 10:07
  • Did you try to add the `aspectj-maven-plugin` to parent pom ? – Serge Ballesta Jun 16 '14 at 10:08
  • @SergeBallesta Yes, the plugin is in the parent POM. – Knight Rider Jun 16 '14 at 10:11
  • I would compile everything with the normal java compiler and only add the aspects compiler to the core project instead of to every project. – M. Deinum Jun 16 '14 at 10:12
  • Please do what @M.Deinum suggested: post your POM and, if possible, also your code (optimally a minimal, working project, maybe on GitHub). Then I am sure we can help you. – kriegaex Jun 22 '14 at 09:10
  • @kriegaex Added the parent POM file. I thought I could add the configuration in the parent POM like this and it would apply for all the sub-modules, however it fails. – Knight Rider Jun 24 '14 at 05:55
  • Now that it is too late for the bounty you are providing the information I needed to answer it. Funny. ;-) – kriegaex Jun 24 '14 at 14:35
  • @kriegaex Ok bounty hunter, added a new one. – Knight Rider Jun 25 '14 at 08:39
  • I cannot see your module POM(s) with their dependencies and also not, as I said above, any code enabling me to reproduce the problem. Can you please make it reproduceable? The POM alone does not expose the problem. P.S.: I only have today to help you, then I will be on a business trip for a while. P.P.S.: A minimal project on GitHub would be great. – kriegaex Jun 25 '14 at 11:52
  • What dependencies do you need to see from the child-POMs? Everything should be declared here in the parent POM? – Knight Rider Jun 26 '14 at 08:12
  • Obviously not. You have a problem with a Hibernate dependency according to the error message. It is not declared. Unless I see the full picture (POMs and code), I cannot even reproduce the issue and even less resolve it for you. As I said: Make the problem **reproduceable** for me, please. Thank you. – kriegaex Jun 28 '14 at 09:59

2 Answers2

3

Enable debug on the maven invocation to dig deeper. You should observe that the aspectj compile is only being invoked during the first maven invocation with the clean. Since the builddef.lst already exists after the first invocation, invoking without clean skips the aspectj compile.

This aspectj compile plugin behavior has been observed previously and was described here:

http://out-println.blogspot.com/2007/08/compile-time-checks-with-aspectj-part-2.html?m=1

You will need to look deeper to address the underlying issue, but as one commenter has already suggested, the aspectj compiler should only be enabled in modules that require it.

Otherwise, the additional dependencies are required for the aspectj compile, as you have already observed. I have incorporated aspectj compile into my own work without issue by restricting it to only the modules that require it.

Kent Dorsey
  • 336
  • 1
  • 4
3

According to the AspectJ compiler Maven plugin you could set up the argumentFileName to locate an existing builddef.lst.

So you can generate the builddef.lst and copy it to your resources folder, and instruct the AspectJ Maven plugin to use that file.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911