Get annotations when exec-maven-plugin runs Main d

2019-08-20 04:51发布

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>

0条回答
登录 后发表回答