Maven mojo plugin to load class from hosting project
Asked Answered
B

1

2

I have a custom plugin that loads classes, e.g.

Class<?> clazz = Class.forName(NAME_OF_CLASS_FROM_HOST_DEPENDENCIES);

NAME_OF_CLASS_FROM_HOST_DEPENDENCIES - is the class that exists in the dependencies of project, where this plugin is used.

in hosting project pom, I call plugin like this:

<plugin>
  <groupId>com.plugins</groupId>
  <artifactId>the_plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <executions>              
     <execution>
        <id>do</id>
        <phase>process-classes</phase>
          <goals>
             <goal>do</goal>
         </goals>
    </execution>
  </executions>
</plugin>

Getting ClassNotFoundException

it's important , those dependencies defined in pom as

<scope>provided</scope>
Bainbridge answered 8/4, 2017 at 8:27 Comment(0)
B
1

Ended up with following.

List<URL> listUrl = new ArrayList<URL>();

Set<Artifact> deps = project.getDependencyArtifacts();
for (Artifact artifact : deps) {
 final URL url = artifact.getFile().toURI().toURL();
 listUrl.add(url);                      
}

newClassLoader = new URLClassLoader(listUrl.toArray(new URL[listUrl.size()]), Thread.currentThread().getContextClassLoader());
Bainbridge answered 8/4, 2017 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.