You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Hilco Wijbenga <hi...@gmail.com> on 2011/07/17 06:25:40 UTC

How to load a class in a Mojo?

Hi all,

According to [1] you need a custom URLClassLoader and
@requiresDependencyResolution in order to load a class from the
classpath. So I have in my Mojo:

/**
 * @goal run
 * @phase generate-sources
 * @requiresDependencyResolution compile (I also tried runtime)
 */
public class RunMojo extends AbstractMojo {
  :
  public void execute() throws MojoExecutionException {
    :
    try {
      final File rootDir = new File("target/classes");
      if (!rootDir.exists()) throw new IllegalStateException("Does not exist.");
      final Deque<File> queue = new LinkedList<File>();
      queue.add(rootDir);
      while (!queue.isEmpty()) {
        final File dir = queue.poll();
        for (final File f : dir.listFiles())
          if (f.isFile()) System.out.println(f); else if
(f.isDirectory()) queue.add(f);
      }
      final URL targetClasses = new URL("file", null, file.getAbsolutePath());
      System.out.println(targetClasses);
      final URLClassLoader classLoader = new URLClassLoader(new URL[]
{ targetClasses });
      final Class<?> mainClass = classLoader.loadClass("org.example.Main");
      getLog().info("Main.class=" + mainClass);
    } catch (final Exception e) {
      throw new MojoExecutionException(e.getMessage(), e);
    }
  }
  :
}

This yields a ClassNotFoundException. Note that org.example.Main has
no dependencies (it's simply an implementation of "Hello World!").

I can see that I made no errors in the URL, Main.class is available
and where I expect it to be.

What am I doing wrong?

Cheers,
Hilco

[1] https://cwiki.apache.org/MAVEN/maven-3x-class-loading.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to load a class in a Mojo?

Posted by Benson Margulies <bi...@gmail.com>.
Could you post a (non)working example on github for others to debug at?

On Sun, Jul 17, 2011 at 4:22 PM, Hilco Wijbenga
<hi...@gmail.com> wrote:
> On 17 July 2011 11:48, Robert Scholte <rf...@codehaus.org> wrote:
>> Don't you think it's a bit weird to run the maven-compiler-plugin:compile during the generate-sources phase? Try to keep this as they were meant to:- generate sources during the generate-sources phase- compile the sources during the compile phase.(within a few months you won't understand this anymore, neither will your fellow developers)Another question: is Main a class of the plugin or from the project using the plugin? Sounds like you are trying to solve a chicken-egg problem... -Robert > Date: Sun, 17 Jul 2011 11:20:32 -0700
>
> No, I don't think it's weird. :-) I need that class to do the source
> generation (it's the model). I don't want it in a separate JAR, that
> would be silly. There's nothing complex about this. Besides, the
> compilation is working fine.
>
> The Main class would be used by the plugin as the model for the source
> generation. So the plugin expects the source for the model (in
> src/main/java) and the templates (in something like
> src/main/templates). That seems reasonable to me.
>
> Anyway, these seem to be philosophical questions. I still don't
> understand why I can't load a class that's on the classpath.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to load a class in a Mojo?

Posted by Hilco Wijbenga <hi...@gmail.com>.
On 17 July 2011 11:48, Robert Scholte <rf...@codehaus.org> wrote:
> Don't you think it's a bit weird to run the maven-compiler-plugin:compile during the generate-sources phase? Try to keep this as they were meant to:- generate sources during the generate-sources phase- compile the sources during the compile phase.(within a few months you won't understand this anymore, neither will your fellow developers)Another question: is Main a class of the plugin or from the project using the plugin? Sounds like you are trying to solve a chicken-egg problem... -Robert > Date: Sun, 17 Jul 2011 11:20:32 -0700

No, I don't think it's weird. :-) I need that class to do the source
generation (it's the model). I don't want it in a separate JAR, that
would be silly. There's nothing complex about this. Besides, the
compilation is working fine.

The Main class would be used by the plugin as the model for the source
generation. So the plugin expects the source for the model (in
src/main/java) and the templates (in something like
src/main/templates). That seems reasonable to me.

Anyway, these seem to be philosophical questions. I still don't
understand why I can't load a class that's on the classpath.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: How to load a class in a Mojo?

Posted by Robert Scholte <rf...@codehaus.org>.
Don't you think it's a bit weird to run the maven-compiler-plugin:compile during the generate-sources phase? Try to keep this as they were meant to:- generate sources during the generate-sources phase- compile the sources during the compile phase.(within a few months you won't understand this anymore, neither will your fellow developers)Another question: is Main a class of the plugin or from the project using the plugin? Sounds like you are trying to solve a chicken-egg problem... -Robert > Date: Sun, 17 Jul 2011 11:20:32 -0700
> Subject: Re: How to load a class in a Mojo?
> From: hilco.wijbenga@gmail.com
> To: users@maven.apache.org
> 
> On 17 July 2011 02:53, Robert Scholte <rf...@codehaus.org> wrote:
> > It is less complex, it´s just like any other java-project:this.getClass().getResourceAsStream( name )
> > this.getClass().getResource( name );
> 
> That's what I started with but both give me a NULL.
> 
> Just to make sure I've given you all (possibly irrelevant)
> information, here's the full scoop:
> 
> 1. In generate-sources, my plugin runs the maven-compiler-plugin to
> compile Main.java (this works fine);
> 2. It then tries to load Main.class. This does not work, even though
> Main.class exists and is on the class path. I get NULLs and
> ClassNotFoundExceptions.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
 		 	   		  

Re: How to load a class in a Mojo?

Posted by Hilco Wijbenga <hi...@gmail.com>.
On 17 July 2011 02:53, Robert Scholte <rf...@codehaus.org> wrote:
> It is less complex, it´s just like any other java-project:this.getClass().getResourceAsStream( name )
> this.getClass().getResource( name );

That's what I started with but both give me a NULL.

Just to make sure I've given you all (possibly irrelevant)
information, here's the full scoop:

1. In generate-sources, my plugin runs the maven-compiler-plugin to
compile Main.java (this works fine);
2. It then tries to load Main.class. This does not work, even though
Main.class exists and is on the class path. I get NULLs and
ClassNotFoundExceptions.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: How to load a class in a Mojo?

Posted by Robert Scholte <rf...@codehaus.org>.
It is less complex, it´s just like any other java-project:this.getClass().getResourceAsStream( name )
this.getClass().getResource( name );
 The @requiresDependencyResolution is about which dependecies should be available for this class, based on their scope. -Robert> Date: Sat, 16 Jul 2011 21:25:40 -0700
> Subject: How to load a class in a Mojo?
> From: hilco.wijbenga@gmail.com
> To: users@maven.apache.org
> 
> Hi all,
> 
> According to [1] you need a custom URLClassLoader and
> @requiresDependencyResolution in order to load a class from the
> classpath. So I have in my Mojo:
> 
> /**
>  * @goal run
>  * @phase generate-sources
>  * @requiresDependencyResolution compile (I also tried runtime)
>  */
> public class RunMojo extends AbstractMojo {
>   :
>   public void execute() throws MojoExecutionException {
>     :
>     try {
>       final File rootDir = new File("target/classes");
>       if (!rootDir.exists()) throw new IllegalStateException("Does not exist.");
>       final Deque<File> queue = new LinkedList<File>();
>       queue.add(rootDir);
>       while (!queue.isEmpty()) {
>         final File dir = queue.poll();
>         for (final File f : dir.listFiles())
>           if (f.isFile()) System.out.println(f); else if
> (f.isDirectory()) queue.add(f);
>       }
>       final URL targetClasses = new URL("file", null, file.getAbsolutePath());
>       System.out.println(targetClasses);
>       final URLClassLoader classLoader = new URLClassLoader(new URL[]
> { targetClasses });
>       final Class<?> mainClass = classLoader.loadClass("org.example.Main");
>       getLog().info("Main.class=" + mainClass);
>     } catch (final Exception e) {
>       throw new MojoExecutionException(e.getMessage(), e);
>     }
>   }
>   :
> }
> 
> This yields a ClassNotFoundException. Note that org.example.Main has
> no dependencies (it's simply an implementation of "Hello World!").
> 
> I can see that I made no errors in the URL, Main.class is available
> and where I expect it to be.
> 
> What am I doing wrong?
> 
> Cheers,
> Hilco
> 
> [1] https://cwiki.apache.org/MAVEN/maven-3x-class-loading.html
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>