0

I'm writing a Maven plugin whose job is to delegate to a core module who needs to read files from the project classpath (i.e. the project declaring the plugin as a plugin dependency).

However, from what I understand, Maven plugin comes with its own classpath, thus leading all my Class#getResourceAsStream in my core module calls to returning null.

Is there a way to include the project classpath elements in the plugin one?

1 Answer 1

1

OK, the issue was due to a leading slash. Once removed, the following code retrieves the MavenProject instance (you need to add maven-artifact and maven-project to your POM):

public static ClassLoader getClassLoader(MavenProject project) throws DependencyResolutionRequiredException, MalformedURLException {
    List<String> classPathElements = compileClassPathElements(project);
    List<URL> classpathElementUrls = new ArrayList<>(classPathElements.size());
    for (String classPathElement : classPathElements) {
        classpathElementUrls.add(new File(classPathElement).toURI().toURL());
    }
    return new URLClassLoader(
        classpathElementUrls.toArray(new URL[classpathElementUrls.size()]),
        Thread.currentThread().getContextClassLoader()
    );
}

private static List<String> compileClassPathElements(MavenProject project) throws DependencyResolutionRequiredException {
    return newArrayList(project.getCompileClasspathElements());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.