csfb
csfb

Reputation: 1097

Maven doesn't weave aspectj code

I'm facing problems to build an aspect project in eclipse with maven. When I run maven through eclipse "Run As > Maven build" I obtain this message: <...>/Clazz.java:[5,32] error: cannot find symbol. So, it looks like aspectj is not weaving the code through maven.

I distilled the problem until have class and an aspect that defines an intertype attribute in the mentioned class, as follows:

public class Clazz {
    public static void main(String[] args) {
        System.out.println(new Clazz().string);
    }
}

public aspect Aspect {

    public String Clazz.string = "string";

}

The pom.xml looks like this:

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

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

Upvotes: 3

Views: 2256

Answers (2)

kriegaex
kriegaex

Reputation: 67457

Actually you do not need to deactivate the Maven Compiler Plugin, but you need to do two things according to what I found out for someone who had a similar problem here:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <aspectj.version>1.8.1</aspectj.version>
</properties>

<!-- (...) -->

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <!-- IMPORTANT -->
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <source>1.7</source>
        <target>1.7</target>
        <Xlint>ignore</Xlint>
        <complianceLevel>1.7</complianceLevel>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
    </configuration>
    <executions>
        <execution>
            <!-- IMPORTANT -->
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
</plugin>

I.e. you need to

  • use incremental compilation in Maven Compiler Plugin 3.1 (attention, the switch is reversed, which is probably a bug) and
  • assign execution phase "process-sources" to AspectJ Maven Plugin 1.6.

That should do it.

Upvotes: 5

The problem appears to be that the maven-compiler-plugin doesn't know to get out of the way when you have an AspectJ compile and throws errors that kill the build before ajc gets a chance to pull in the ITDs. My solution has been to disable maven-compiler-plugin entirely and let ajc handle compiling the .java files:

<!-- disable compiler because compiler chokes on ITDs -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
        <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
    </executions>
</plugin>

Upvotes: 5

Related Questions