2013-04-27 66 views
0

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

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

我检查了插件类路径,它只包含插件类和它的依赖关系

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

+1

此答案有帮助吗? http://stackoverflow.com/questions/2659048/add-maven-build-classpath-to-plugin-execution-classpath – 2013-04-27 08:31:24

回答

2

我采取这种做法,显然它的工作:

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

@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); 
}