Add maven-build-classpath to plugin execution classpath

后端 未结 3 1589
天命终不由人
天命终不由人 2020-12-02 11:31

I am writing some code-gen maven-plugin.

I need my project classpath be injected in to my plugin execution classpath.

I found this article. The solution ther

3条回答
  •  暖寄归人
    2020-12-02 11:55

    I took this approach and apparently it's working:

    1 - a MavenProject parameter is needed within your Mojo class:

    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    private MavenProject project;
    

    2 - and then you can get the classpath elements from project instance:

    try {
        Set urls = new HashSet<>();
        List 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);
    }
    

提交回复
热议问题