Using aspects from other jars
Asked Answered
A

1

6

What I'm trying to accomplish is the following:

I have a server with the following structure.

bin
   apis
   services
   etc...

I want to define an API that contains an aspect to be used by services. Say:

@Aspect
public class AuthorizationAspect {
    @Pointcut("call(* *()) && @annotation(Authorization)")
    public void cutAuthorize() { }

    @Before("cutAuthorize()")
    public void callFromAuthorizeBefore() {
        System.out.println("Test");
    }
}

Then I define the service and annotate the methods I want with @Authorization and it gets pointcut by that aspect.

Things you should know:

  • Services only use the API to compile the code, therefore the scope is "provided", since the API will be already in the server.
  • Services JARs are loaded dynamically, so they will reside in another classloader.

My question is, how can I do this? How do I define my maven artifacts to accomplish that?

I noticed that the aspectj plugin has a weaveDependencies section, but this will also include in the service JAR all classes in that API (something that I want to avoid). Is this the right move?

Thanks in advance,

Rui

Almeta answered 20/5, 2011 at 15:35 Comment(1)
This is an old question, but it might still be of interest. So you want(ed) to define an aspect library containing pointcuts on calls to the API from the services, is that right?Knifeedged
H
3

Take a look at how it's done in jcabi-aspects. You declare/compile your aspects in the API and then use this JAR as we com.jcabi:jcabi-aspects is being used in the example:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <configuration>
    <aspectLibraries>
      <aspectLibrary>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aspects</artifactId>
      </aspectLibrary>
    </aspectLibraries>
  </configuration>
</plugin>

It's OK to have your aspects JAR in provided (or runtime) scope.

Hajji answered 1/1, 2013 at 7:14 Comment(4)
Is there any way to define the version of the required aspectLibrary ? (cause I tried and got Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.7:compile (default) on project Thingy: Unable to parse configuration of aspectj-maven-plugin for parameter version)Homey
@Homey submit a ticket to Github, we'll try to help youHajji
@Hajji on which github ?Homey
Nope nevermind ! I found the what's to the why's there -> mojo.codehaus.org/aspectj-maven-plugin/examples/… in fact there is no need to specify a version for an aspectLibrary sice it has to refer to a project dependency who's version has already been defined. Bottom line is aspectLibrary.version is assumed as the referenced project dependency's version. Sorry for the inconvenience.Homey

© 2022 - 2024 — McMap. All rights reserved.