Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining Maven execution phase within a plugin

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...

like image 523
Hellblazer Avatar asked Sep 19 '10 16:09

Hellblazer


People also ask

Are Maven plugins executed in order?

Plugin executions are ordered according to their phases. See https://maven.apache.org/ref/current/maven-core/lifecycles.html for the order of phases. For example, here mavin-plugin-1 is executed before maven-plugin-2 because the process-resources phase is defined as taking place before the compile phase.

What is execution in Maven plugin?

Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.

What is the correct syntax for executing a Maven plugin?

Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”. For example mvn example:version .

What is the difference between plugin and pluginManagement tags?

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.


2 Answers

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).

like image 72
Pascal Thivent Avatar answered Sep 22 '22 05:09

Pascal Thivent


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.

like image 45
Brett Porter Avatar answered Sep 18 '22 05:09

Brett Porter