简体   繁体   English

Maven 不编织 aspectj 代码

[英]Maven doesn't weave aspectj code

I'm facing problems to build an aspect project in eclipse with maven.我在使用 maven 在 Eclipse 中构建方面项目时遇到了问题。 When I run maven through eclipse "Run As > Maven build" I obtain this message: <...>/Clazz.java:[5,32] error: cannot find symbol.当我通过 Eclipse “Run As > Maven build”运行 maven 时,我收到此消息:<...>/Clazz.java:[5,32] 错误:找不到符号。 So, it looks like aspectj is not weaving the code through maven.因此,看起来aspectj 没有通过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: pom.xml 看起来像这样:

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

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.问题似乎是当您进行 AspectJ 编译时, maven-compiler-plugin不知道让开,并在ajc有机会引入 ITD 之前抛出错误以ajc构建。 My solution has been to disable maven-compiler-plugin entirely and let ajc handle compiling the .java files:我的解决方案是完全禁用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>

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 :实际上,您不需要停用 Maven Compiler Plugin,但是您需要根据我在这里为遇到类似问题的人发现的内容做两件事:

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

Ie you need to即你需要

  • use incremental compilation in Maven Compiler Plugin 3.1 (attention, the switch is reversed, which is probably a bug) and在 Maven Compiler Plugin 3.1 中使用增量编译(注意,开关是反向的,这可能是一个错误)和
  • assign execution phase "process-sources" to AspectJ Maven Plugin 1.6.将执行阶段“process-sources”分配给 AspectJ Maven Plugin 1.6。

That should do it.那应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM