无法将AspectJ与Maven集成 [英] Not able to integrate AspectJ with Maven
问题描述
我敲了两天的脑袋,将Aspectj与Maven集成在一起,但是没有用. 我正在编写一个带有两个建议的简单程序(在Eclipse中可以正常工作,因此我确定这不是代码方面的错误).
I banged my head for two days to integrate aspectj with maven, But did not work. I am writing a simple program with two advices (which works fine in eclipse, so I am sure it's not a code side error).
我最近开始使用maven进行项目构建.想不到为什么我无法启动Aspectj编译器.
I have recently started using maven for project building. Can't think of why I am not able to kick off aspectj compiler.
在通过Maven进行编译期间-我只得到Java的类文件,而没有进行编织. .aj文件未编译.
During compilation through maven - I get only class file for java without weaving. .aj file is not compiled.
请帮助!!!
第一个方面文件-ExceptionAspects.aj
the first aspect file - ExceptionAspects.aj
package com.aspectSample;
public aspect ExceptionAspects {
pointcut ExceptionHandler(Exception e) : handler(Exception+) && args(e) && within(com.clickable.*);
pointcut callbeforeMethod():execution( public void HelloWorldExclude.*(..)) ;
before(Exception ex) : ExceptionHandler(ex)
{
System.out.println("Added Aspects before Exception :"+ex.toString());
}
before(): callbeforeMethod()
{
System.out.println("Aspect Before Method Call");
}
pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);
declare error
: sysOutOrErrAccess()
: "Don't write to the console";
}
源Java文件HelloWorldExclude.java 包com.aspectSample;
The source java file HelloWorldExclude.java package com.aspectSample;
import java.sql.SQLException;
public class HelloWorldExclude {
private void FoulIntro(String message, String name) throws SQLException
{
throw new SQLException("WriteSomething");
}
public void GiveMeFoulIntro(String message, String name)
{
try
{
this.FoulIntro(message, name);
}catch(SQLException exp)
{
System.out.println("catching exception");
}
}
}
并且pom.xml文件是
and the pom.xml file is
<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.clickable.HelloWorld-Maven</groupId>
<artifactId>HelloWorldAspect-Maven</artifactId>
<version>1.0.0</version>
<name>HelloWorld Aspect</name>
<packaging>jar</packaging>
<description>Hello World Code</description>
<properties>
<java-version>1.6</java-version>
<org.aspectj-version>1.6.11</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
</project>
推荐答案
pom文件中存在一个愚蠢的错误. Aspectj插件已添加到element下,这只是所有插件的配置.它没有告诉编译器使用什么.
There was a stupid mistake in pom file. aspectj plugin was added under element, which is just a configuration of all plugins. It doesn't tell compiler what to use.
只要我从该元素中删除了插件,它就会起作用.
It worked as soon as I removed plugin out of this element.
谢谢 潘卡(Pankaj)
Thanks Pankaj
这篇关于无法将AspectJ与Maven集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!