目前我创建了一个插件,执行时需要从项目工件中访问类并检索注释。
/**
* 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<Artifact> artifacts = project.getArtifacts();
Iterator<Artifact> 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());
}
}
}
然后我在其他项目中使用这个插件
<plugin>
<groupId>com.my</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<configuration>
<scanPackage>com.my.services</scanPackage>
</configuration>
</plugin>
当我尝试使用执行 mvn 目标时
mvn my:doc
它产生这个输出
[INFO] Scanning package: com.goijo.mona.services
[INFO] Class name: com.my.services.AuthenticationService
[INFO] Annotation : 0
任何人都可以帮助为什么注释总是 0 因为我的类 com.my.services.AuthenticationService 已经被注释了
@InfoIn( ... )
public class AuthenticationService {
}
并且注释已经设置了 RetentionPolicy
@Target(value=ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InfoIn {
public Info[] value();
}