Maven: compile aspectj project containing Java 1.6 source

橙三吉。 提交于 2019-11-27 19:06:48

Version 1.3 of the AspectJ plugin deliberately changed the default phase of its compile goal from "process-sources" to "compile". To restore the previous behaviour of running ajc before javac, you just need to add a "phase" tag to the relevant "execution" tag, like this:

<execution>
    <phase>process-sources</phase> <!-- or any phase before compile -->
    <goals>
        <goal>compile</goal>
        <goal>test-compile</goal>
    </goals>
</execution>
Tomasz Domzal

How about telling maven-compiler-plugin to skip all *.java files and let aspectj-maven-plugin do the job ?

...
<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.3</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>utf-8</encoding>
        <complianceLevel>1.6</complianceLevel>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>       <!-- weave main classes -->
            <goal>test-compile</goal>  <!-- weave test classes -->
          </goals>
        </execution>
       </executions>
    </plugin>
  </plugins>
</build>
matsev

I used this configuration in a project to compile AspectJ and Java 6 using Maven:

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

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

Ref: aspectj-maven-plugin

Pascal Thivent

How do you specify a custom compilerId that points to your own ajc compiler (that is make compile:compile use an aspectj compiler other than the plexus one)?

I don't know how to specify another compilerId than the "official" ones. Not sure it's possible.

My understanding is that http://jira.codehaus.org/browse/MCOMPILER-107 would solve your problem (AspectJ 1.6+ does support Java 1. 6 right?). Sadly, it's still open.

How do you ignore the failure of compile:compile?

The compiler:compile goal of the Maven Compiler plugin has a failOnError optional parameter allowing to Indicate whether the build will continue even if there are compilation errors.

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <failOnError>false</failOnError>
          ...
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

This could be a ugly workaround to the above problem.

How do you get maven to run the aspectj:compile goal directly, without ever running compile:compile?

The problem is that compiler:compile is bound to the compile phase and that you can't remove a default lifecyle binding. So there is maybe another option but the only one I can think of would be to turn everything off by using a <packaging>pom<packaging> and to rebind all the goals manually (at least for these phases: process-resources, compile, process-test-resources, test-compile, test, package). Schematically, something like this:

process-resources       resources:resources
compile                 aspectj:compile
process-test-resources  resources:testResources
test-compile            compiler:testCompile
test                    surefire:test
package                 ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
install                 install:install
deploy                  deploy:deploy

That could be another ugly workaround. Disclaimer: not tested but should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!