Maven + AspectJ weaving for Java8
Asked Answered
J

1

5

I Cannot mvn package with the minimal sample below. Eclipse (Mars.2 Release 4.5.2) compiles and weaves without a problem.

What do I have to do to make it work?

The output:

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 2 source files to ...\workspace\test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] .../workspace/test/src/main/java/test/Foo.java:[6,21] cannot find symbol
  symbol: method doSomethingInjected()

A sample class:

package test;

public class Foo {
    public void bar() {
        this.doSomethingInjected();
    }
}

Sample interface:

package test;

public interface Injectable { }

aspect:

package test;

public aspect Injection {

    declare parents : test..* implements Injectable;

    public void Injectable.doSomethingInjected() {
        System.out.println("done");
    }
}

pom.xml (relevant parts as per aspectj-maven-plugin usage doc)

<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>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.9</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Joijoice answered 14/1, 2017 at 4:30 Comment(0)
E
8

Try this, it makes your project compile and run cleanly (I tested it):

<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>

  <groupId>de.scrum-master.stackoverflow</groupId>
  <artifactId>aspectj-introduce-method</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.8</java.source-target.version>
    <aspectj.version>1.8.10</aspectj.version>
  </properties>

  <build>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>${java.source-target.version}</source>
          <target>${java.source-target.version}</target>
          <!-- IMPORTANT -->
          <useIncrementalCompilation>false</useIncrementalCompilation>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.9</version>
        <configuration>
          <!--<showWeaveInfo>true</showWeaveInfo>-->
          <source>${java.source-target.version}</source>
          <target>${java.source-target.version}</target>
          <Xlint>ignore</Xlint>
          <complianceLevel>${java.source-target.version}</complianceLevel>
          <encoding>${project.build.sourceEncoding}</encoding>
          <!--<verbose>true</verbose>-->
          <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
        </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>

    </plugins>

  </build>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${aspectj.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

</project>
Eran answered 14/1, 2017 at 12:36 Comment(4)
Two questions: how would I configure it in maven if my aspect(s) are in the test-jar? Secondly, why exactly are two lines marked <!-- Important --> ? I haven't been able to find any explanation about why exactly those are necessary.Tenfold
Question #2: Those were notes to myself because in order to use incremental compilation you have to set it to false, see this bug ticket for Maven Compiler. Setting the phase to "process-sources" for AspectJ Maven helps avoid certain problems if other code is compiled with Maven Compiler. The default phase is "compile", but "process-sources" is better in most cases (earlier). I do not want to go into more detail here in a comment.Eran
Question #1: You would add the test-jar as a test dependency for the module in which you want to use it and then refer to it from AspectJ Maven's aspectLibraries section. Probably you would then define two separate plugin configs/executions for compile and test-compile so as to avoid your test aspects ending up in production. But I am not sure if that is needed, I have not tried. Interesting question though. If you have any further questions, I suggest to open a completely new question so we can deal with it separately.Eran
Here's my full pomTenfold

© 2022 - 2024 — McMap. All rights reserved.