Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding phases to the maven lifecycle?

I try to add some additional phases to the maven lifecycle. Mainly to add some additional test levels:

<phases>
    <phase>initialize</phase>
    <phase>process-resources</phase>
    <phase>compile</phase>
    <phase>process-test-resources</phase>
    <phase>test-compile</phase>
    <phase>test</phase>
    <phase>prepare-package</phase>
    <phase>package</phase>
    <phase>pre-integration-test</phase>
    <phase>integration-test</phase>
    <phase>post-integration-test</phase>
    <phase>pre-application-test</phase>
    <phase>application-test</phase>
    <phase>post-application-test</phase>
    <phase>pre-system-test</phase>
    <phase>system-test</phase>
    <phase>post-system-test</phase>
    <phase>finalize-tests</phase>
    <phase>install</phase>
    <phase>deploy</phase>
</phases>

Above contains new application-test and system-test phase (including pre- and post-).

I've started a test plugin at: codezoo-lifecycle-maven-plugin The pom I use for testing is in the src/it folder.

It seems the new phases or somewhat picked up but there are some weird things going on:

mvn post-application-test

This works. Also the echo plugin that I added for testing is executed. But there are some warnings (using maven 3.3.9).

mvn install

Executes the default lifecycle skipping the new phases.

If I change the id of the lifecycle fomr "test-levels" to "default" the phases are executed twice.

The warnings issued are:

[WARNING] Duplicated lifecycle phase package. Defined in default but also in test-levels
[WARNING] Duplicated lifecycle phase pre-integration-test. Defined in default but also in test-levels
[WARNING] Duplicated lifecycle phase integration-test. Defined in default but also in test-levels
....

The source code that issues this warning indicates the lifecycle is not properly namespaced. But I cannot figure out how that is done.

I found some hints on the web: create-a-new-phase (Stackoverflow) or in other plugins like the maven-scm-publish-plugin or the docker-maven-plugin. But those are either creating a complete new lifecycle or just change the plugin mapping from the default maven lifecycle.

All other stuff on the web regarding this topic seems to be at least 4 years old...

So:

  • how can I add additional phases to the maven default lifecycle (If I have to repeat the default plugin mappings: I can live with that)
  • how can I namespace the new lifecycle? It seems I create my own packaging (which is referenced as role-hint in the configuration). But still maven has some fallback to the default lifecycle.
  • can the default maven phases not be re-used?

The current state of the test plugin is on github.

Thanks!

like image 486
wemu Avatar asked Apr 14 '16 08:04

wemu


1 Answers

  • You can add lifecycle and phases if required.
  • But recommended approach is:
    • You already have a well defined build lifecycle with 24 phases
    • Implement your custom plugin if you don't find it on maven repo and bind it to any phase which is most suitable.
    • This way it is easy to maintain because every developer will be already aligned with default phases and if you have added more plugin execution to any specific phase, it is easily understood what you want to do.

Surely you can make your project as complex as you want but generally it is considered bad practice to add lifecycle and phases until you don't have very big thing to manage in the context of build lifecycle.

like image 96
Rohit Kumar Avatar answered Oct 07 '22 18:10

Rohit Kumar