3

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>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
csfb
  • 1,105
  • 1
  • 9
  • 19
  • Take a look at this: http://stackoverflow.com/questions/8718245/how-to-enable-aspectj-compile-time-weaving-with-java-7-and-maven?rq=1 It's not exactly your problem, but it will go a long way towards helping you out. – Software Engineer Jul 07 '14 at 16:45

2 Answers2

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>
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • If I do that the build works. However the generated jar doesn't have any class. – csfb Jul 07 '14 at 17:01
  • 2
    @Bruno Do you still have the AspectJ `compile` goal in place, and did you do a full clean and recompile? – chrylis -cautiouslyoptimistic- Jul 07 '14 at 17:02
  • No, I misinterpreted your answer and replaced aspectj plugin defintion by yours. So, now the build is working. However, when I execute the jar through "java -jar" with the generated jar I'm obtaining a ClassNotFoundException: org.aspectj.lang.NoAspectBoundException. So, It looks like the compiled code have dependencies with aspectj lib, what is strange since my intention is to weave aspect code in compile time. Do you hagve any guess about this? Thanks in advance. – csfb Jul 07 '14 at 17:34
  • @Bruno I'm not familiar enough with AspectJ to be able to tell, but that suggests that it's expecting the AspectJ RT. That's been my experience even with CTW (from Roo). – chrylis -cautiouslyoptimistic- Jul 07 '14 at 18:47
  • I already put "aspectjrt.jar" as dependency, as well as "aspectjtools.jar" in the following command: java -cp ".\lib\*" -jar perf-pocs-0.0.1-SNAPSHOT.jar (both jars are inside the lib folder) and the problem still persists. Thanks anyway. – csfb Jul 07 '14 at 19:06
  • Ok, now is working. Just updating... I put the dependency inside the MANIFEST.MF of my jar like this "Class-Path: lib/aspectjrt-1.7.3.jar". – csfb Jul 07 '14 at 19:11
  • @Bruno You should be using Maven to launch your program or build a "fat jar"; this applies to classpath dependencies generally. – chrylis -cautiouslyoptimistic- Jul 07 '14 at 19:19
  • @chrylis the time you just saved me! – Frankie Nov 28 '18 at 17:59
5

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.

Community
  • 1
  • 1
kriegaex
  • 63,017
  • 15
  • 111
  • 202