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

rpvilao
  • 1,116
  • 2
  • 14
  • 31
  • 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? – Frank Pavageau Sep 30 '12 at 21:18

1 Answers1

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.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 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`) – Ar3s May 07 '15 at 13:16
  • @Ar3s submit a ticket to Github, we'll try to help you – yegor256 May 07 '15 at 18:21
  • @yegor256 on which github ? – Ar3s May 12 '15 at 12:28
  • Nope nevermind ! I found the what's to the why's there -> http://mojo.codehaus.org/aspectj-maven-plugin/examples/libraryJars.html 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. – Ar3s May 18 '15 at 14:04