0

I'm writing my own maven plugin, and I have an issue to load a certain class. This post proposed a way to enrich the ClassRealm to broad class loading scope.

@Mojo(
        name = "deploy",
        defaultPhase = LifecyclePhase.DEPLOY,
        requiresDependencyCollection = ResolutionScope.RUNTIME,
        requiresDirectInvocation = true,
        requiresOnline = true
)
public class DeployMojo extends AbstractMojo {
    @Parameter
    private String server;

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

    @Component
    private PluginDescriptor descriptor;

    public void execute() throws MojoExecutionException, MojoFailureException {
        // Added runtime resources for the project and create the classloader
        final var realm = descriptor.getClassRealm();
        final ArrayList<String> classpathElements;
        try {
            classpathElements = new ArrayList<>(project.getRuntimeClasspathElements());
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("Unable to resolve project dependencies", e);
        }
        classpathElements.add(project.getBuild().getOutputDirectory());
        final var urls = new URL[classpathElements.size()];
        for (int i = 0; i < classpathElements.size(); ++i) {
            try {
                urls[i] = new File(classpathElements.get(i)).toURI().toURL();
                realm.addURL(urls[i]);
            } catch (MalformedURLException e) {
                throw new MojoExecutionException(String.format("Unable to parse classpath: %s as URL", classpathElements.get(i)), e);
            }
        }
        
        //Some other operations
    }
}

However, when I try to get the ClassRealm via descriptor.getClassRealm(), it shows that Cannot access org.codehaus.plexus.classworlds.realm.ClassRealm. Also in the documentation, it mentions that Warning: This is an internal utility method that is only public for technical reasons, it is not part of the public API. In particular, this method can be changed or deleted without prior notice and must not be used by plugins.

I wonder is there a way to enrich the ClassRealm, or this is something that we shouldn't change.

Coding_Rabbit
  • 1,287
  • 3
  • 22
  • 44
  • Without code / pom file etc. it's not possible to help.. (The other question is: What purpose for are you writing a plugin?) – khmarbaise Aug 30 '21 at 13:34
  • I've added the plugin sample code. The purpose of the plugin is to serialization the project and send it to Azure blob storage for further processing. – Coding_Rabbit Aug 31 '21 at 14:30
  • First it looks from my point of view a bit strange: `requiresDirectInvocation = true` and the other question which comes into my mind is `deploy` default binding correct? Nevertheless you could try something like: `@Parameter( defaultValue = "${project.compileClasspathElements}", readonly = true, required = true ) private List compilePath;` ... and check if this is the correct way. – khmarbaise Aug 31 '21 at 17:23

0 Answers0