无法将 AspectJ 与 Maven 集成

发布于 2024-12-03 20:09:43 字数 3785 浏览 0 评论 0原文

我绞尽脑汁想把aspectj与maven集成,但是没有成功。 我正在编写一个带有两个建议的简单程序(在 Eclipse 中运行良好,所以我确信这不是代码端错误)。

我最近开始使用maven来构建项目。想不出为什么我无法启动aspectj 编译器。

在通过 maven 编译期间 - 我只获得 java 的类文件而无需编织。 .aj 文件未编译。

请帮忙!!!!

第一个方面文件 - 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;

    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 文件是

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

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

I have recently started using maven for project building. Can't think of why I am not able to kick off aspectj compiler.

During compilation through maven - I get only class file for java without weaving. .aj file is not compiled.

Please Help!!!!

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

}

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-10 20:09:43

您可以尝试以下几件事:

  • 显式提供 maven-aspectj 插件的依赖项:

    <前><代码> <插件>
    org.codehaus.mojo;
    aspectj-maven-plugin;
    <版本>1.4
    <依赖关系>
    <依赖关系>
    org.aspectj;
    aspectjrt
    <版本>${aspectj.version}

    <依赖关系>
    org.aspectj;
    aspectjtools
    <版本>${aspectj.version}

aspectjrt和aspectjtools的版本与您正在使用的aspectj版本相匹配。

  • 您的测试文件夹中是否有任何源代码,
    使用 src/test 文件夹,然后要将方面编织到测试源,您必须在 maven-aspectj 插件中明确提及:

     <执行>
                <执行>
                    <目标>
                        <目标>编译
                        <目标>测试编译
                    
                
            
    

A couple of things that you can try:

  • Explicitly provide the dependencies of the maven-aspectj plugin:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
    

with the version of aspectjrt and aspectjtools matching the version of aspectj that you are using.

  • Do you by any chance have any of your sources in the test folder,
    with the src/test folder, then to weave the aspects to the test sources you will have to explicitly mention that in the maven-aspectj plugin:

                <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
    
孤千羽 2024-12-10 20:09:43

pom 文件中有一个愚蠢的错误。
element下添加了aspectj插件,这只是所有插件的配置。它不告诉编译器要使用什么。

当我从这个元素中删除插件后它就起作用了。

谢谢
潘卡伊

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.

Thanks
Pankaj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文