views:

77

answers:

2

I have a plugin which transforms the compiled classes. This transformation needs to be done for both the module's classes and the module's test classes. Thus, I bind the plugin to both the process-classes and process-test-classes phases. The problem I have is that I need to determine which phase the plugin is currently executing in, as I do not (cannot, actually) transform the same set of classes twice.

Thus, within the plugin, I would need to know if I'm executing process-classes - in which case I transform the module's classes. Or if I'm executing process-test-classes - in which I case I do not transform the module's classes and transform only the module's test classes.

I could, of course, create two plugins for this, but this kind of solution deeply offends my sensibilities and is probably against the law in several states.

It seems like something I could reach from my module should be able to tell me what the current phase is. I just can't for the life of me find out what that something is.

Thanks...

+2  A: 

Thus, within the plugin, I would need to know if I'm executing process-classes (...) or if I'm executing process-test-classes

AFAIK, this is not really possible.

I could, of course, create two plugins for this, but this kind of solution deeply offends my sensibilities and is probably against the law in several states.

I don't see anything wrong with having two Mojos sharing code but bound to different phases. Something like the Maven Compiler Plugin (and its compiler:compile and compiler:testCompile goals).

Pascal Thivent
Well, that answer by Dr. J is pretty definitive. I must say that seems pretty darn weird, but what evs... Thanks for the speedy answer.
Hellblazer
I also discovered that the AspectJ compiler Maven plugin does the same thing as what I need to do and so I'm going to see how they dealt with this.
Hellblazer
So, the pattern the AspectJ compiler used works for me. For posterity, I simply create two Mojos in my plugin, one for process-classes and one for process-test-classes. This solution doesn't require another plugin (which was my main fear).
Hellblazer
Can't you get from the MavenExecutionRequest? Of course I ask about how to get that object.
Thomas
@Hellblazer AspectJ is an even better example indeed.
Pascal Thivent
Yup, in almost all Maven Plugins I implemented there was an AbstractXyzMojo, a CompileXyzMojo and a TestCompileXyzMojo. I guess that's standard for Maven Plugin development (+1)
seanizer
@seanizer Yes, exactly, same here.
Pascal Thivent
+1  A: 

you can't get the phase, but you can get the execution ID which you have as separate. In the plugin:

/** 
 * @parameter expression="${mojoExecution}" 
 */
private org.apache.maven.plugin.MojoExecution execution;

...

public void execute() throws MojoExecutionException
{
    ...
    System.out.println( "executionId is: " + execution.getExecutionId() );
}

I'm not sure if this is portable to Maven 3 yet.

Brett Porter
Thanks. I just used the pattern that AspectJ uses and it works perfectly fine. Don't want to go against the grain when Maven wants a particular pattern to be used.
Hellblazer
yes, that's definitely a better way to handle it
Brett Porter