如何将maven模块类传递给maven插件

如何将maven模块类传递给maven插件,maven,maven-2,maven-3,maven-plugin,Maven,Maven 2,Maven 3,Maven Plugin,我开发了一个maven插件,它可以扫描模块中的类,找到一些特定的类,并对它们做些什么 问题是,当我在maven模块中使用这个插件时,它无法在该模块中找到类 我已经检查了插件类路径,它只包含插件类,并且它是依赖项 有没有办法自动将模块类包含到插件类路径中?我采用了这种方法,显然它是有效的: 1-Mojo类中需要一个MavenProject参数: @Parameter(defaultValue = "${project}", required = true, readonly = true) pri

我开发了一个maven插件,它可以扫描模块中的类,找到一些特定的类,并对它们做些什么

问题是,当我在maven模块中使用这个插件时,它无法在该模块中找到类

我已经检查了插件类路径,它只包含插件类,并且它是依赖项


有没有办法自动将模块类包含到插件类路径中?

我采用了这种方法,显然它是有效的:

1-Mojo类中需要一个MavenProject参数:

@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
2-然后您可以从项目实例获取类路径元素:

try {
    Set<URL> urls = new HashSet<>();
    List<String> elements = project.getTestClasspathElements();
                                  //getRuntimeClasspathElements()
                                  //getCompileClasspathElements()
                                  //getSystemClasspathElements()  
    for (String element : elements) {
        urls.add(new File(element).toURI().toURL());
    }

    ClassLoader contextClassLoader = URLClassLoader.newInstance(
            urls.toArray(new URL[0]),
            Thread.currentThread().getContextClassLoader());

    Thread.currentThread().setContextClassLoader(contextClassLoader);

} catch (DependencyResolutionRequiredException e) {
    throw new RuntimeException(e);
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
}
试试看{
Set url=newhashset();
List elements=project.getTestClasspathElements();
//getRuntimeClasspathElements()
//getCompileClasspathElements()
//getSystemClasspathElements()
for(字符串元素:元素){
添加(新文件(元素).toURI().toul());
}
ClassLoader contextClassLoader=URLClassLoader.newInstance(
URL.toArray(新URL[0]),
Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(contextClassLoader);
}捕获(依赖解析要求例外e){
抛出新的运行时异常(e);
}捕获(格式错误){
抛出新的运行时异常(e);
}
这个答案有用吗?