Maven没有编织aspectj代码

Bru*_*axo 3 java maven-2 aspectj maven

我在使用maven在eclipse中构建一个方面项目时遇到了问题.当我通过eclipse运行maven"Run As> Maven build"时我得到了这条消息:<...>/Clazz.java:[5,32]错误:找不到符号.因此,看起来aspectj不是通过maven编织代码.

我解决了这个问题,直到在上面提到的类中有类和一个定义intertype属性的方面,如下所示:

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

public aspect Aspect {

    public String Clazz.string = "string";

}
Run Code Online (Sandbox Code Playgroud)

pom.xml看起来像这样:

  <dependencies>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.7.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

chr*_*ke- 5

问题似乎是,maven-compiler-plugin当你有一个AspectJ编译并且在ajc有机会拉入ITD 之前抛出可以杀死构建的错误时,不知道会不会这样做.我的解决方案是maven-compiler-plugin完全禁用并让文件ajc处理.java:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

  • @Bruno你是否仍然有AspectJ`comple`目标,你做了一个完整的清理并重新编译? (2认同)

kri*_*aex 5

实际上你不需要停用 Maven 编译器插件,但是根据我为遇到类似问题的人找到的信息,你需要做两件事

<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>
Run Code Online (Sandbox Code Playgroud)

即你需要

  • 在 Maven Compiler Plugin 3.1 中使用增量编译(注意,开关颠倒了,这可能是一个 bug)并且
  • 将执行阶段“process-sources”分配给 AspectJ Maven Plugin 1.6。

应该可以做到这一点。