【问题标题】:AspectJ in Maven project, not working/weavingMaven项目中的AspectJ,不工作/编织
【发布时间】:2014-08-24 17:42:42
【问题描述】:

我正在尝试让 AspectJ 编织在一个简单的 Maven 项目中工作,但不确定哪里出错了: 当我使用“mvn exec:java”运行代码时,我没有看到预期的输出。

我确信代码可以正常工作,因为我在 STS 中尝试过相同的操作,但它可以正常工作。我只是想让 AspectJ 在 Maven 项目中工作。

任何有关如何调试此类问题的提示将不胜感激。

<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>com.aop</groupId>
<artifactId>aop1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>aop1</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.3</version> <!-- specify your version -->
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <mainClass>com.aop.aop1.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

与代码在同一文件夹中的方面文件:

    package com.aop.aop1;


public aspect aspect {

    pointcut secureAccess()
        : execution(* *.foo(..));

    before() : secureAccess() {
        System.out.println("BEHOLD the power of AOP !!!");
    }
}

Java 文件:

package com.aop.aop1;
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        foo();
    }
    public static void foo() {
        System.out.println(" IN FOO.");
    }
}

【问题讨论】:

    标签: java maven aspectj aspectj-maven-plugin


    【解决方案1】:

    你的配置有几个问题:

    • 切面应命名为Aspect,大写字母“A”,而不是保留关键字aspect
    • POM 缺少结束 &lt;/project&gt; 标记。
    • POM 有一个&lt;pluginManagement&gt; 部分,但没有单独的&lt;plugins&gt; 部分,即您为插件提供默认值,但实际上并未声明要使用它们。因此,要么使用不带 &lt;pluginManagement&gt; 的独立 &lt;plugins&gt; 部分,要么在额外的 &lt;plugins&gt; 部分中重新声明插件。
    • aspectj-maven-plugin 需要一个&lt;version&gt;。您忘记指定一个。
    • aspectj-maven-plugin 还需要 &lt;complianceLevel&gt; 配置。
    • 您使用编译时编织,因此您不需要&lt;outxml&gt; 设置。它仅在加载时编织时需要。
    • aspectjrt 依赖项至少需要 1.7.4 版本才能与默认情况下在 aspectj-maven-plugin 1.6 中使用的版本兼容,以便编译您的来源。

    除此之外,如果您没有任何令人信服的理由,我建议使用较新版本的 Maven 插件和依赖项,例如 JUnitexec-maven-plugin使用旧的。我还建议使用最新的 AspectJ 版本 1.8.2,并指定在 aspectj-maven-plugin 内部使用该版本。

    工作pom.xml,改动很小:

    <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>com.aop</groupId>
        <artifactId>aop1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>aop1</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.7.4</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.6</version>
                    <configuration>
                        <complianceLevel>1.7</complianceLevel>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                            <configuration>
                                <source>1.7</source>
                                <target>1.7</target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.1</version>
                    <configuration>
                        <mainClass>com.aop.aop1.App</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    工作 pom.xml 建议更改:

    <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>com.aop</groupId>
        <artifactId>aop1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <name>AOP Sample</name>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <aspectj.version>1.8.2</aspectj.version>
            <java.source-target.version>1.7</java.source-target.version>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</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.6</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>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>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3</version>
                    <configuration>
                        <mainClass>com.aop.aop1.App</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.dstovall</groupId>
                    <artifactId>onejar-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <executions>
                        <execution>
                            <configuration>
                                <onejarVersion>0.96</onejarVersion>
                                <mainClass>com.aop.aop1.App</mainClass>
                                <attachToBuild>true</attachToBuild>
                            </configuration>
                            <goals>
                                <goal>one-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <pluginRepositories>
            <pluginRepository>
                <id>OneJAR googlecode.com</id>
                <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
            </pluginRepository>
        </pluginRepositories>
    
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <!--<scope>runtime</scope>-->
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    

    顺便说一句,onejar-maven-plugin 只是一个好东西,我喜欢用它来构建一个独立的超级 JAR(又名胖 JAR),其中包含运行软件所需的一切,即您的类/方面加上 AspectJ 运行时。您可以运行该程序

    java -jar aop1-0.0.1-SNAPSHOT.one-jar.jar
    

    输出应该类似于 mvn exec:java 只是没有 Maven 的东西:

    Hello World!
    BEHOLD the power of AOP !!!
    IN FOO.
    

    【讨论】:

    • 非常感谢。没有一些右括号是由于 SO 以某种方式修剪了我的粘贴。我在这里做了一个^A^C^V。 :) Es hat sehr geholfen。丹克·肖恩。 :)
    • 非常感谢 - 很棒的回答
    • 您能评论一下useIncrementalCompilation 的配置吗?这是我唯一缺少的部分,我不明白其中的含义。
    • 是的,我可以:Maven编译器插件实际上有一个错误,因为它颠倒了“使用增量编译”的含义。将其设置为 false 将激活它,将其设置为 true 将禁用它。确实很奇怪,即使有票也没有人愿意修复它。
    • 这是一个绝妙的答案。 @kriegaex 感谢您花时间浏览并解释详细信息。恭喜队友。
    猜你喜欢
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2018-02-20
    相关资源
    最近更新 更多