title: Accessing annotation in Maven Plugin
body: <p>Currently I create a plugin that when executed need to be access the classes from project artifacts and retrieve the annotation.</p>
  
  <pre><code>/**
   * Generate document for services
   * 
   * @author Ali Irawan
   * 
   * @goal doc
   * @requiresDependencyResolution compile
   * @phase generate-sources
   */
  public class GeneratePlugin extends AbstractMojo {
  
      /**
       * project
       * 
       * @parameter expression = "${project}";
       */
      protected MavenProject project;
  
      /**
       * scanPackage
       * 
       * @parameter
       */
      private String scanPackage;
  
      @Override
      public void execute() throws MojoExecutionException {
          getLog().info("My Maven Plugin Started");
          getLog().info("Scanning package: " + scanPackage);
  
          // PluginDescriptor pluginDescriptor = (PluginDescriptor) super.getPluginContext().get("pluginDescriptor");
          try {
              ClassLoader original = Thread.currentThread().getContextClassLoader();
  
              Collection urls = new ArrayList();
  
              Set&lt;Artifact&gt; artifacts = project.getArtifacts();
              Iterator&lt;Artifact&gt; iterator = artifacts.iterator();
              while(iterator.hasNext()){
                  Artifact entry = iterator.next();
                  urls.add(entry.getFile().toURI().toURL());
              }
  
              urls.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL());
  
              Thread.currentThread().setContextClassLoader(new URLClassLoader((URL[]) urls.toArray(new URL[urls.size()]),original));
  
              ClassLoader loader = Thread.currentThread().getContextClassLoader();
              Class clazz = loader.loadClass("com.goijo.mona.services.AuthenticationService");
              getLog().info("Class name: " + clazz.getName());
  
              // This always return 0, don't know why ?
              getLog().info("Annotation : " + clazz.newInstance().getClass().getAnnotations().length);
  
              // Restore class loader
              Thread.currentThread().setContextClassLoader(original);
  
              getLog().info("MyMaven Plugin Stopped");
          } catch (Exception e) {
              e.printStackTrace();
              getLog().error(e.getMessage());
          }
      }
  
  
  }
  </code></pre>
  
  <p>Then I use this plugin in other project</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;com.my&lt;/groupId&gt;
      &lt;artifactId&gt;my-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
      &lt;configuration&gt;
          &lt;scanPackage&gt;com.my.services&lt;/scanPackage&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>When I try to execute the mvn goals using</p>
  
  <pre><code>mvn my:doc
  </code></pre>
  
  <p>It produce this output</p>
  
  <pre><code>[INFO] Scanning package: com.goijo.mona.services
  [INFO] Class name: com.my.services.AuthenticationService
  [INFO] Annotation : 0
  </code></pre>
  
  <p>Any one can help why the annotation is always 0
  As my class com.my.services.AuthenticationService is already annotated</p>
  
  <pre><code>@InfoIn( ... )
  public class AuthenticationService {
  
  }
  </code></pre>
  
  <p>And the annotation is already set the RetentionPolicy</p>
  
  <pre><code>@Target(value=ElementType.TYPE)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface InfoIn {
  
      public Info[] value();
  
  }
  </code></pre>