简体   繁体   English

如何告诉Maven将项目编译为AspectJ项目

[英]How to tell maven to compile a project as an aspectj project

My project: 我的项目:

src/main/aspects : src/main/aspects

com
 |---badmitrii
        |---Trace.aj

src/main/java : src/main/java

com
 |---badmitrii
        |---App.java

I wrote the following pom: 我写了以下pom:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.badmitrii</groupId>
    <artifactId>aspectj-test</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>aspectj-test</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <configuration>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>com.badmitrii</groupId>
                            <artifactId>aspectj-test</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.badmitrii.App</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.badmitrii.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

But the jar is still being compiled as a java-project and when I run it with java -jar <jar-name> I didn't get the desirable aspectJ result. 但是jar仍被编译为java项目,当我使用java -jar <jar-name>运行它时,没有得到令人满意的aspectJ结果。

How to tell maven to compile the jar with respect to aspects? 如何告诉Maven就方面而言编译jar?

UPD: UPD:

App.java: App.java:

package com.badmitrii; com.badmitrii软件包;

public class App 
{
    public static void main( String[] args )
    {
        App  a = new App ();
        a.m();
    }

    public void m(){
        System.out.println("test");
    }
}

Trace.aj: Trace.aj:

package com.badmitrii.aspects;

public aspect Trace {
    pointcut publicMethodExecuted(): execution(public !static * *(..));

    after(): publicMethodExecuted() {
        System.out.printf("Enters on method: %s. \n", thisJoinPoint.getSignature());

        Object[] arguments = thisJoinPoint.getArgs();
        for (int i =0; i < arguments.length; i++){
            Object argument = arguments[i];
            if (argument != null){
                System.out.printf("With argument of type %s and value %s. \n", argument.getClass().toString(), argument);
            }
        }
        System.out.printf("Exits method: %s. \n", thisJoinPoint.getSignature());
    }
}

Try configuring the plugin as the documentation describes: 尝试按照文档描述配置插件:

In order to apply already compiled aspects to your own sources you need do setup all the JAR files you would like to weave in the plugin configuration as shown below. 为了将已经编译的方面应用于您自己的源,您需要在插件配置中设置要编织的所有JAR文件,如下所示。

which would be 那将是

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.badmitrii</groupId>
                        <artifactId>aspectj-test</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
           <executions>
             <execution>
               <goals>
                 <goal>compile</goal>
               </goals>
             </execution>
           </executions>
        </plugin>

Updated after code post: 代码发布后更新:

However, yours is not a pre-compiled aspect. 但是,您不是一个预编译的方面。 So you don't need the configuration of aspectLibraries, you only need the goal: 因此,您无需配置AspectLibraries,只需要目标:

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
           <executions>
             <execution>
               <goals>
                 <goal>compile</goal>
               </goals>
             </execution>
           </executions>
        </plugin>

Also, you should add the version of the maven-plugin: 另外,您应该添加Maven插件的版本:

<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>

This will compile your aspect if you comment out the printf lines (they are not written correctly, and they are beyond the scope of this question). 如果注释掉printf行(它们的书写方式不正确,并且超出了此问题的范围),则这将编译您的方面。 You may test it with a System.out.println("I am inside the aspect") inside the aspect to see it works. 您可以使用方面内部的System.out.println(“我在方面中”)进行测试,以查看其是否有效。

Like: 喜欢:

package com.badmitrii.aspects;

public aspect Trace {
    pointcut publicMethodExecuted(): execution(public !static * *(..));

    after(): publicMethodExecuted() {
//        System.out.printf("Enters on method: %s. \n", thisJoinPoint.getSignature());
        System.out.println("inside the aspect - after");
        Object[] arguments = thisJoinPoint.getArgs();
        for (int i =0; i < arguments.length; i++){
            Object argument = arguments[i];
            if (argument != null){
//                System.out.printf("With argument of type %s and value %s. \n", argument.getClass().toString(), argument);
            }
        }
//        System.out.printf("Exits method: %s. \n", thisJoinPoint.getSignature());
    }
}

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

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