Get annotations when exec-maven-plugin runs Main does not work

余生长醉 提交于 2020-01-06 03:16:24

问题


I would like to run a Main class with exec-maven-plugin and from my dependencies generate documentation like a swagger file.

The annotation that I care is javax.ws.rs.Path which has @Retention(RetentionPolicy.RUNTIME)

My Java code

public class ContextClassReader extends ClassReader {

private static final ExtensibleClassLoader CLASS_LOADER = new ExtensibleClassLoader();

public ContextClassReader(final String className) throws IOException {
    super(CLASS_LOADER.getResourceAsStream(className.replace('.', '/') + ".class"));
    final URL resource = CLASS_LOADER.getResource(className.replace('.', '/') + ".class");
}

public static ClassLoader getClassLoader() {
    return CLASS_LOADER;
}

public static void addClassPath(final URL url) {
    CLASS_LOADER.addURL(url);
}

private static class ExtensibleClassLoader extends URLClassLoader {

    ExtensibleClassLoader() {
        super(new URL[]{});
    }

    @Override
    public void addURL(final URL url) {
        super.addURL(url);
    }
}

Here is the loading of class and testing it for annotations.

final Class<?> clazz = ContextClassReader.getClassLoader().loadClass(className);
isAnnotationPresent(clazz); 

public static boolean isAnnotationPresent(final AnnotatedElement annotatedElement) { 
  ...  annotatedElement.getAnnotations().length  --> 0
  ...  clazz.getMethods().length() ---> works !
}

My pom.xml

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>java</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <executable>java</executable>
                        <workingDirectory>XXXXXXXXXXXXXXXXXXX</workingDirectory>
                        <addResourcesToClasspath>true</addResourcesToClasspath>
                        <additionalClasspathElements>true</additionalClasspathElements>
                        <includeProjectDependencies>true</includeProjectDependencies>
                        <includePluginDependencies>true</includePluginDependencies>
                        <includeProjectDependencies>true</includeProjectDependencies>

                        <mainClass>XXX.Main</mainClass>
                    </configuration>
                </plugin>

来源:https://stackoverflow.com/questions/51481776/get-annotations-when-exec-maven-plugin-runs-main-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!