获取Maven插件中的Java文件列表

获取Maven插件中的Java文件列表,java,maven,maven-3,maven-plugin,Java,Maven,Maven 3,Maven Plugin,有没有一种简单的方法可以获取在Maven插件中具有特定注释的Java文件列表?我想在deploy中运行插件,以调用另一个服务,通知所有具有特定注释的类 我过去曾在运行时使用反射库完成此操作,如下所示 Reflections reflections = new Reflections(PACKAGE); Set<Class<?>> checks = reflections.getTypesAnnotatedWith(FocusCheck.class); 然后 List c

有没有一种简单的方法可以获取在Maven插件中具有特定注释的Java文件列表?我想在deploy中运行插件,以调用另一个服务,通知所有具有特定注释的类

我过去曾在运行时使用反射库完成此操作,如下所示

Reflections reflections = new Reflections(PACKAGE);
Set<Class<?>> checks = reflections.getTypesAnnotatedWith(FocusCheck.class);
然后

List compileSourceRoots=project.getCompileSourceRoots();
然后,我需要递归地遍历列表中的每个文件夹,找到java文件并检查它们。我怀疑有更好的方法来做到这一点

谢谢,
Paul

看看下面的一些
Spring
源代码:

注意:方法缩小了可读性

org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider 
/**
 * Scan the class path for candidate components.
 * @param basePackage the package to check for annotated classes
 * @return a corresponding Set of autodetected bean definitions
 */
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
    Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
    try {
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                resolveBasePackage(basePackage) + "/" + this.resourcePattern;
        Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
        for (Resource resource : resources) {
            // check its metadata to see if it's what you want
        }
    }
    return candidates;
}

因此,似乎使用
ClassLoader
获取包下的类资源,然后检查这些类的元数据是一个不错的方法。

我将看看其他库如何做同样的事情,例如,检查Spring如何扫描包中的某些注释。您是否有一个链接可以给我一个起点?
List<String> compileSourceRoots = project.getCompileSourceRoots();
org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider 
/**
 * Scan the class path for candidate components.
 * @param basePackage the package to check for annotated classes
 * @return a corresponding Set of autodetected bean definitions
 */
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
    Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
    try {
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                resolveBasePackage(basePackage) + "/" + this.resourcePattern;
        Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
        for (Resource resource : resources) {
            // check its metadata to see if it's what you want
        }
    }
    return candidates;
}
org.springframework.core.io.support.PathMatchingResourcePatternResolver
/**
 * Find all class location resources with the given path via the ClassLoader.
 * Called by {@link #findAllClassPathResources(String)}.
 * @param path the absolute path within the classpath (never a leading slash)
 * @return a mutable Set of matching Resource instances
 * @since 4.1.1
 */
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
    Set<Resource> result = new LinkedHashSet<Resource>(16);
    ClassLoader cl = getClassLoader();
    Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
    while (resourceUrls.hasMoreElements()) {
        URL url = resourceUrls.nextElement();
        result.add(convertClassLoaderURL(url));
    }
    if ("".equals(path)) {
        // The above result is likely to be incomplete, i.e. only containing file system references.
        // We need to have pointers to each of the jar files on the classpath as well...
        addAllClassLoaderJarRoots(cl, result);
    }
    return result;
}