Tuesday, June 09, 2015

Selectively run Maven phases and goals from Eclipse IDE

CALL FOR ACTION: If you like this feature please vote up on 470138.

1129 installs from Eclipse Marketplace so far.

This kinda answers this SO question.

Recent updates:
  • Save named configurations
  • The dialog is now mode less, thus allowing multiple invocations. If there is are multiple Maven projects in the workspace, a selector to invoke Phases and Goals on selected other project is shown.
  • Run goals in selection order instead of build agenda order. Using this mode you can run goals in any order. An astute reader may note that you can also type goals in text area before running them. This allows you to even run goals not associated with any phase.
  • Selecting a phase selects the associated goals
  • Expand all Phases
  • Expand all/Collapse all
  • If there is only one Maven project in the workspace, that is always selected.
  • Added a simple Maven Build Spy. It shows the success or failure of the goals, the timing and the exception message as a tooltip for failed goals.

You can even use the spy in your maven builds by:
  • Downloading the Maven Build Spy jar.
  • And then passing the following parameter to your mvn build like so:
> mvn -Dmaven.ext.class.path=path-to/mavenbuildspy.jar ....

When you run a phase, Maven runs all the preceding phases as per the lifecycle definition. However, sometimes I only want to run a selective subset of phases. The only way to do that is to actually run the goals associated with those phases explicitly. However how to get that list of goals? One would think that running:

> mvn help:describe -Dcmd=deploy

would do the trick. But alas it does not. It only lists the statically associated goals as per the static lifecycle definition - and not the actual goals you may configured in the pom's plugin configuration section. IMO this is simply - unimplemented yet useful functionality of Maven.

In this blog entry I talked about listing phases and actual associated goals of a maven lifecycle of a maven pom. In it I talked about an Eclipse plugin based on m2e code. I have improved it further. Now you can show the phases and goals in a checked tree dialog using the Project > Phases and Goals command.

You can run the selected phases or goals using the Launch selected goals command. When a phase is selected all goals associated with that phase are run. In Single Selection Mode the goals simply get added to the set in the order in which they were selected. This allows execution of orders in any order you want.

Maven 3.3.1 or greater is required to run the goals.

NOTE: If you install external Maven 3.3.1+ and try to use it as the installed Maven using Window > Preferences > Maven > Installations you may see an error during the build:

-Dmaven.multiModuleProjectDirectory system property is not set...

To fix that configure the property in your JDK/JRE installation using Window > Preferences > Java > Installed JREs > select default JDK/JRE > edit.

- See more at: http://marketplace.eclipse.org/content/phases-and-goals

You can print the listing to the Maven Console using the Log All command.

To use
  • Make sure you have m2e plugins installed into your Eclipse.
  • Then install the fragement from this update site URL:

or use:

Drag to your running Eclipse workspace to install Phases and GOals


Current standing see: Eclipse Marketplace

  • Restart Eclipse
  • Select a maven project node and invoke Project > Show Phases and Goals command. You will see the output like above in the Maven Console. If a resource from Maven Project is not selected you are prompted to select on.


BTW This is implemented as a fragment for https://github.com/eclipse/m2e-core/tree/master/org.eclipse.m2e.core.ui



Anonymous said...

I'm wondering if there's any command-line functionality, that you know of, to run selected lifecycle phases (without the preceding phases)?

Sandip Chitale said...

Unfortunately not. IMO this is a shortcoming of

> mvn help:describe -Dcmd=package

command in terms from listing the actual goals bound to the phases. That way you can list the goals and then invoke them (including the execution id). And the same mechanism behind it should/could be used to implement something like:

> mvn test-compile pre-integration-test...post-integration-test

That way you can only run all the goals associated with

integration-test (due to ... )

Sandip Chitale said...

See related http://stackoverflow.com/questions/1709625/maven-command-to-list-lifecycle-phases-along-with-bound-goals

Anonymous said...

I don't even think it's so much something I'd expect from the "help" plugin. In some cases I've wanted to specify the phases, rather than figure out what the goals are and run them again.

For instance in a CI environment, for status reporting, it might be useful to do a "mvn compile" in a first build phase, then do a next few lifecycle phases in another build phase. If the goals were handcoded into the CI server's "another build phase", changes to the POM would always have to be manually incorporated into the CI server's configuration also.

Sandip Chitale said...
This comment has been removed by the author.
Sandip Chitale said...

Right. That will require maven level support though. I have sent the request to Maven folks also about this. Some syntax like:

> mvn pre-integration-test...post-integration-test

or even some variant of inclusive, exclusive type syntax.

Anonymous said...

I'm not convinced that a plugin would not be able to do this: introspect the Project model and invoke separate goal executions... but I'm not going to dive into that. I would have used it, had it existed, that's all I'm saying :)

Sandip Chitale said...

I also would like to see it implemented at Maven level - via a plugin (say!) so that it could be used non-interactively in CI like you mentioned before. In fact that is what I tried first thinking that I just need to tweak the help:describe -Dcmd= goal to automatically get the list of goals associated with phases. But it turned out to be more complex...and I did not spend more time on it. See: