AspectJ在Maven项目中,不工作/编织 [英] AspectJ in Maven project, not working/weaving

查看:119
本文介绍了AspectJ在Maven项目中,不工作/编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让AspectJ编织在一个简单的Maven项目中工作,并且不确定它出错的地方:
当我使用mvn exec:java运行代码时,我看不到预期的输出。

I am trying get the AspectJ weaving working in a simple Maven project, and not sure where it is going wrong : when I run the code using "mvn exec:java", I dont see expected output.

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

I am sure the code is working, because I tried the same in STS, where it works fine. I just wanted to get the AspectJ working in a Maven project.

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

Any hints to how to debug this kind of issues will be much appreciated.

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

与同一文件夹中的Aspect文件代码:

Aspect file in same folder as code :

    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.");
    }
}


推荐答案

那里您的配置存在以下几个问题:

There are several problems with your configuration:


  • 该方面应命名为 Aspect with a大写A而不是 aspect 这是一个保留关键字。

  • POM缺少结束< ; / project> tag。

  • POM有一个< pluginManagement> 部分,但没有单独的< plugins> 部分,即您为插件提供默认值,但实际上并未声明您要使用它们。因此,要么使用独立的< plugins> 部分而不使用< pluginManagement> ,要么重新启用插件额外的< plugins> 部分。

  • aspectj-maven-plugin 需要< version> 。你忘了指定一个。

  • aspectj-maven-plugin 还需要< complianceLevel> configuration。

  • 您使用编译时编织,因此您不需要< outxml> 设置。它仅用于加载时编织。

  • aspectjrt 依赖项至少需要1.7.4版才能与 aspectj-maven-plugin 1.6默认情况下为了编译你的来源。

  • The aspect should be named Aspect with a capital "A" instead of aspect which is a reserved keyword.
  • The POM is missing a closing </project> tag.
  • The POM has a <pluginManagement> section, but no separate <plugins> section, i.e. you provide defaults for your plugins, but do not actually declare that you want to use them. So either you use a stand-alone <plugins> section without <pluginManagement> or you redeclare the plugins in an additional <plugins> section.
  • The aspectj-maven-plugin needs a <version>. You forgot to specify one.
  • The aspectj-maven-plugin also needs a <complianceLevel> configuration.
  • You use compile-time weaving, so you do not need the <outxml> setting. It is only needed for load-time weaving.
  • The aspectjrt dependency needs at least version 1.7.4 in order to be compatible with the version used in aspectj-maven-plugin 1.6 by default in order to compile your sources.

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

In addition to that, I recommend to use newer versions of Maven plugins and dependencies such as JUnit and exec-maven-plugin if you do not have any compelling reasons to use older ones. I also recommend to use the latest AspectJ version 1.8.2 and also specify to use that one internally in aspectj-maven-plugin.

使用最少的更改工作 pom.xml

Working pom.xml with minimal changes:

<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

Working pom.xml with recommended changes:

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

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

BTW, the onejar-maven-plugin is just a goodie which I like to use in order to build a stand-alone uber JAR (a.k.a. fat JAR) containing everything you need to run the software, i.e. your classes/aspects plus the AspectJ runtime. You can just run the program with

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

输出类似于 mvn exec:java just没有Maven的东西:

The output should be similar to mvn exec:java just without the Maven stuff:

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

这篇关于AspectJ在Maven项目中,不工作/编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆