Reputation: 123
I have an annotation-processor
java app that is looking into the target
folder of the external maven project for the class files. This app uses URLClassLoader
. The target folder is the absolute path app parameter. I have a simple junit test which is loading classes and processing the annotations successfully. Now I wrote the maven plugin that is just a wrapper for annotation-processor
app. When running this maven plugin, annotation-processor
loads the classes but it does not see any annotations in those. How can I access class/interface annotations from the maven plugin?
private static Class<?> getClass(String path, String className, String packageName) {
File file = new File(path);
try {
URL[] urls = new URL[]{file.toURI().toURL()};
ClassLoader cl = new URLClassLoader(urls);
return cl.loadClass(packageName + "." + className));
} catch (MalformedURLException | ClassNotFoundException e) {
log.warn("Cannot find class: " + packageName + "." + className, e);
}
return null;
}
Example call:
Class clazz = getClass("/home/user/java/my-project/target/classes", "MyClass", "com.my.example.package");
assert clazz.getAnnotations() > 0;
Upvotes: 0
Views: 145
Reputation: 6053
There are two possible reasons:
@Retention
Upvotes: 1