title: How do I import a project containing AspectJ aspects and annotation in another project
body: <p>I'm having some difficulties importing a utility jar file containing some custom aspects into another project. It should be noted, that I'm <em>not</em> using Spring for this project, as my client is somewhat averse to Spring.</p>
  
  <p>I have created a proof of concept (full code example below). When I run a test runner in the utility jar, any method annotated with my AspectJ annotation gets their aspects executed just fine. When I use the self-same jar in another project, the aspects are ignore.</p>
  
  <p>When I run the main class in the utility I get:</p>
  
  <pre><code>$&gt; java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar TestOne
  AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called. 
  Executing TestOne.aspectTestMethod() 
  AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.
  </code></pre>
  
  <p>If I run the main class of the consumer class, I get:</p>
  
  <pre><code>$&gt;java -cp aspectjrt-1.8.2.jar;aop-util-1.0-SNAPSHOT.jar;aop-consumer-1.0-SNAPSHOT.jar Test
  Test.testAspectOne
  AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.
  Test.testAspectTwo
  AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.
  </code></pre>
  
  <p>As I'm fairly new to aspect oriented programming, I'd really appreciate a pointer as to what I'm missing :)</p>
  
  <h1>utility jar</h1>
  
  <h2>pom.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;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"&gt;
          &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
          &lt;groupId&gt;sandbox.aop&lt;/groupId&gt;
          &lt;artifactId&gt;aop-util&lt;/artifactId&gt;
          &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
          &lt;packaging&gt;jar&lt;/packaging&gt;
  
          &lt;properties&gt;
              &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
              &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
              &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;/properties&gt;
  
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
                  &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
                  &lt;version&gt;1.8.2&lt;/version&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
  
          &lt;build&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                      &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
                      &lt;version&gt;1.7&lt;/version&gt;
                      &lt;configuration&gt;
                          &lt;complianceLevel&gt;1.8&lt;/complianceLevel&gt;
                          &lt;source&gt;1.8&lt;/source&gt;
                          &lt;target&gt;1.8&lt;/target&gt;
                      &lt;/configuration&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                              &lt;goals&gt;
                                  &lt;goal&gt;compile&lt;/goal&gt;
                              &lt;/goals&gt;
                          &lt;/execution&gt;
                      &lt;/executions&gt;
                  &lt;/plugin&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                      &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                      &lt;configuration&gt;
                          &lt;source&gt;1.8&lt;/source&gt;
                          &lt;target&gt;1.8&lt;/target&gt;
                      &lt;/configuration&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/build&gt;
      &lt;/project&gt;
  </code></pre>
  
  <h2>Annotation</h2>
  
  <pre><code>import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD)
  public @interface AnnotationOne { }
  </code></pre>
  
  <h2>Aspect</h2>
  
  <pre><code>import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.*;
  import org.aspectj.lang.JoinPoint;
  
  @Aspect
  public class AspectOne {
  
      @Pointcut("@annotation(AnnotationOne)")
      public void annotationPointCutDefinition(){
      }
  
      @Pointcut("execution(* *(..))")
      public void atExecution(){}
  
      @Around("@annotation(AnnotationOne) &amp;&amp; execution(* *(..))")
      public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
          Object returnObject = null;
          try {
              System.out.println("AspectOne's aroundAdvice's body is now executed Before aspectTestMethod is called.");
              returnObject = joinPoint.proceed();
          } catch (Throwable throwable) {
              throw throwable;
          }
          finally {
              System.out.println("AspectOne's aroundAdvice's body is now executed After aspectTestMethod is called.");
          }
          return returnObject;
      }
  
      @After("annotationPointCutDefinition() &amp;&amp; atExecution()")
      public void printNewLine(JoinPoint pointcut){
          System.out.print("\n\r");
      }
  }
  </code></pre>
  
  <h2>Main class</h2>
  
  <pre><code>public class TestOne {
  
      public static void main(String[] args) {
          TestOne testOne = new TestOne();
          testOne.aspectTestMethod();
      }
  
      @AnnotationOne
      public void aspectTestMethod(){
          System.out.println("Executing TestOne.aspectTestMethod()");
      }
  }
  </code></pre>
  
  <h1>AOP consumer</h1>
  
  <h2>pom.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;sandbox.aop&lt;/groupId&gt;
      &lt;artifactId&gt;aop-consumer&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;sandbox.aop&lt;/groupId&gt;
              &lt;artifactId&gt;aop-util&lt;/artifactId&gt;
              &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.7&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;complianceLevel&gt;1.8&lt;/complianceLevel&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;compile&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <h2>Annotation</h2>
  
  <pre><code>import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD)
  public @interface AnnotationTwo {}
  </code></pre>
  
  <h2>Aspect</h2>
  
  <pre><code>import org.aspectj.lang.JoinPoint;
  import org.aspectj.lang.ProceedingJoinPoint;
  import org.aspectj.lang.annotation.After;
  import org.aspectj.lang.annotation.Around;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Pointcut;
  
  @Aspect
  public class AspectTwo {
  
      @Pointcut("@annotation(AnnotationTwo)")
      public void annotationPointCutDefinition(){
      }
  
      @Pointcut("execution(* *(..))")
      public void atExecution(){}
  
      @Around("@annotation(AnnotationTwo) &amp;&amp; execution(* *(..))")
      public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
          Object returnObject = null;
          try {
              System.out.println("AspectTwo's aroundAdvice's body is now executed Before aspectTestMethod is called.");
              returnObject = joinPoint.proceed();
          } catch (Throwable throwable) {
              throw throwable;
          }
          finally {
              System.out.println("AspectTwo's aroundAdvice's body is now executed After aspectTestMethod is called.");
          }
          return returnObject;
      }
  
      @After("annotationPointCutDefinition() &amp;&amp; atExecution()")
      public void printNewLine(JoinPoint pointcut){
          System.out.print("\n\r");
      }
  }
  </code></pre>
  
  <h2>Main class</h2>
  
  <pre><code>public class Test {
  
      public static void main(String[] args) {
          Test test = new Test();
          test.testAspectOne();
          test.testAspectTwo();
      }
  
      @AnnotationOne
      public void testAspectOne() {
          System.out.println(Test.class.getName() + ".testAspectOne");
      }
  
      @AnnotationTwo
      public void testAspectTwo() {
          System.out.println(Test.class.getName() + ".testAspectTwo");
      }
  }
  </code></pre>