1

When i use maven-apsectj-plugin and maven-compiler-plugin compile phase will execute both plugins compile goal. This results in compilation with javac first and then full recompilation with ajc.

Is this double compilation necessary? It seems like i can just turn maven-compiler-plugin off and everything works just fine.

I'm using "default" configuration as stated in the usage of maven-compiler-plugin:

<project>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.8.13</version>
    </dependency>
    ...
  </dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.11</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  <build>
  ...
</project>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
Yaroslav Buhaiev
  • 1,187
  • 11
  • 16

1 Answers1

4

Yes, you could deactivate Maven Compiler plugin because the AspectJ compiler is a regularly refreshed fork of Eclipse Java Compiler. Therefore, it can also compile your Java files.

But if the situation is more complex, e.g. you use Maven Compiler to also compile Groovy files or files in other modules and want to configure it only once in <pluginManagement>, maybe deactivating it is not such a nice option. There is a way to make both plugins play nice together, see my other answers

Basically you configure Maven Compiler to use <useIncrementalCompilation>false</useIncrementalCompilation> and AspectJ Maven to use <phase>process-sources</phase>. More information is in the linked answers.

Then you will see output like this:

[INFO] --- aspectj-maven-plugin:1.12.1:compile (default) @ openbook_cleaner ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] 
[INFO] --- aspectj-maven-plugin:1.12.1:test-compile (default) @ openbook_cleaner ---
[WARNING] No sources found skipping aspectJ compile
[INFO] 
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ openbook_cleaner ---
[INFO] Nothing to compile - all classes are up to date
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks for the answer @kriegaex. Don't know if this should be the answer on it's own and i will probably make a separate question but you seem to be the right person to ask. – Yaroslav Buhaiev Jul 09 '19 at 21:17
  • I have a project that uses compiled aspects and weaves them at compile time. Unfortunately AJC is not supported by [Lombok](https://projectlombok.org/). Since this project doesn't have any sources of aspects on its own i configured AspectJ Maven plugin to do post compile bytecode weaving: `true ${project.build.outputDirectory}` That way Lombok + Javac will be invoked first and later modified with aspects. Is there any limitations/disadvantages when performing bytecode weaving on javac generated classes? – Yaroslav Buhaiev Jul 09 '19 at 21:32
  • That is definitely a new question. Please create one including a little [MCVE](http://stackoverflow.com/help/mcve), i.e. your (multi-module?) Maven build with lomboked sample class + main method, aspect - something I can copy (or clone from GitHub), fully build and run. Then I can probably help you. – kriegaex Jul 10 '19 at 02:25