簡體   English   中英

如何告訴Maven將項目編譯為AspectJ項目

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

我的項目:

src/main/aspects

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

src/main/java

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

我寫了以下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>

但是jar仍被編譯為java項目,當我使用java -jar <jar-name>運行它時,沒有得到令人滿意的aspectJ結果。

如何告訴Maven就方面而言編譯jar?

UPD:

App.java:

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:

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());
    }
}

嘗試按照文檔描述配置插件:

為了將已經編譯的方面應用於您自己的源,您需要在插件配置中設置要編織的所有JAR文件,如下所示。

那將是

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

代碼發布后更新:

但是,您不是一個預編譯的方面。 因此,您無需配置AspectLibraries,只需要目標:

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

另外,您應該添加Maven插件的版本:

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

如果注釋掉printf行(它們的書寫方式不正確,並且超出了此問題的范圍),則這將編譯您的方面。 您可以使用方面內部的System.out.println(“我在方面中”)進行測試,以查看其是否有效。

喜歡:

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