gpt4 book ai didi

java - 如何告诉maven将项目编译为aspectj项目

转载 作者:行者123 更新时间:2023-12-01 11:33:12 29 4
gpt4 key购买 nike

我的项目:

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?

更新:

应用程序.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());
}
}

最佳答案

尝试将插件配置为 the documentation描述:

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.

这将是

       <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-plugin 的版本:

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

如果您注释掉 printf 行(它们写得不正确,并且超出了这个问题的范围),这将编译您的方面。您可以在方面内使用 System.out.println("I am inside thespect") 来测试它,看看它是否有效。

喜欢:

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

关于java - 如何告诉maven将项目编译为aspectj项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255592/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com