Maven과 AspectJ - 완벽 환경설정 관련

2024. 2. 15. 20:09프로그래밍

728x90

 

 

Maven + AspectJ - all steps to configure it

I have a problem with applying aspects to my maven project. Probably I am missing something, so I've made a list of steps. Could you please check if it is correct? Let say in projectA is an aspect

stackoverflow.com


질문

질문은 메이븐 프로젝트를 사용하고 있을 때 오류가 난다는 것이며,
자신이 진행 했던 단계별 내용에 대해서 우선 설명 하고 이 단계들이 맞는건지를 물어보는 상황입니다.

projectA 는 aspect class 이며 projectB의 클래스들이 이 aspect 들에 의벼서 변경 되어야만 합니다.

  • 메이븐 프로젝트 ProjectA 를 AspectJ 클래스로 생성
  • AspectJ 플러그인과 의존 라이브러리 추가
  • ProjectA 의 의존성을 projectB로 pom.xml 파일에 추가
  • projectB를 pom.xml내 플러그인으로 추가
  • AspectJ 의존성 추가
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>ProjectA</groupId>
                <artifactId>ProjectA</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
</plugin>


위 단계 별 설정을 거치고 컴파일을 하면 다음과 같은 주의 메시지 발생:

[WARNING] advice defined in AspectE has not been applied [Xlint:adviceDidNotMatch]



프로그램 실행 시 다음 예외 상황이 발생:

Exception in thread "FeatureExcutionThread" java.lang.NoClassDefFoundError: AspectE

 

선택 답변 

다음과 같은 폴더 구조와 코드로 구성하였음.

$ tree .
.
├── pom.xml
├── ProjectA
|   ├── pom.xml
|   └── src
|       └── main
|           └── aspect
|               └── com
|                   └── stackoverflow
|                       └── aspects
|                           ├── AspectL.java
|                           └── Trace.aj
└── ProjectB
    ├── pom.xml
    └── src
        └── main
            └── java
                └── com
                    └── stackoverflow
                        └── App.java

 

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>org.stackoverflow</groupId>
    <artifactId>Q12423965</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>

    <modules>
        <module>ProjectA</module>
        <module>ProjectB</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.stackoverflow</groupId>
                <artifactId>Q12423965-ProjectA</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 

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

    <parent>
        <groupId>org.stackoverflow</groupId>
        <artifactId>Q12423965</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12423965-ProjectA</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <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>
            </plugin>
        </plugins>
    </build>
</project>

 

다음 두개의 다른 aspect 를 생성하였음. 하나는 @AspectJ 어노테이션을 사용하고, 다른 하나는 클래식한 AspectJ aspect 를 정의 하였습니다.

AspectL.java

package com.stackoverflow.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author maba, 2012-09-18
 */
@Aspect
public class AspectL {

    @Pointcut("execution(* main(..))")
    public void defineEntryPoint() {
    }

    @Before("defineEntryPoint()")
    public void aaa(JoinPoint joinPoint) {
        System.out.println("aspect before");
    }

    @After("defineEntryPoint()")
    public void bbb(JoinPoint joinPoint) {
        System.out.println("aspect after");
    }
}

 

Trace.aj

package com.stackoverflow.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());
    }
}


위 두개의 파일들은 ProjectA 모듈의 한 부분이며, jar 파일에 포함하여야함
이제 이 두개의 aspect 들을 사용하기 위해서 ProjectB의 코드로 넣어주어야 함.
ProjectB/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>

    <parent>
        <groupId>org.stackoverflow</groupId>
        <artifactId>Q12423965</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12423965-ProjectB</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
        <dependency>
            <groupId>org.stackoverflow</groupId>
            <artifactId>Q12423965-ProjectA</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <configuration>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.stackoverflow</groupId>
                            <artifactId>Q12423965-ProjectA</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.stackoverflow.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


App.java

package com.stackoverflow;

/**
 * @author maba, 2012-09-17
 */
public class App {

    public void hello(String name) {
    }

    public static void main(String[] args) {
        App app = new App();
        app.hello("world");
    }
}

최상위 pom 에서 전체 빌드를 수행 합니다:

mvn clean install


ProjectB 디렉토리로 가서 app 을 실행 한다.

mvn exec:java


결과는 다음과 같이 나옵니다:

aspect before
Enters on method: void com.stackoverflow.App.hello(String). 
With argument of type class java.lang.String and value world. 
Exits method: void com.stackoverflow.App.hello(String). 
aspect after

 

따라서, 두개의 aspect 들은 잘 동작했고, 메이븐 설정 역시 제대로 동작 하였습니다.

 

728x90