15

By disable I mean completely, entirely cut the phase from the lifecycle, so that it is not even invoked.

I might need a custom lifecycle but I could not find an easy way to define it straight in my pom.xml. It appears I need to write a plugin just to list phases I want.

For example, I would like default-testResources, default-testCompile and default-test phases to never happen and turn my build log from this:

[INFO] ------------------------------------------------------------------------
[INFO] Building HelpDesk Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helpdesk ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory d:\proj\HelpDesk\repo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ helpdesk ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helpdesk ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ helpdesk ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helpdesk ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) @ helpdesk ---
[INFO] Packaging webapp
[INFO] Assembling webapp [helpdesk] in [d:\proj\HelpDesk\repo\target\helpdesk]
[INFO] Processing war project
[INFO] Copying webapp resources [d:\proj\HelpDesk\repo\src\main\webapp]
[INFO] Webapp assembled in [40 msecs]
[INFO] Building war: d:\proj\HelpDesk\repo\target\helpdesk.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

into this:

[INFO] ------------------------------------------------------------------------
[INFO] Building HelpDesk Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helpdesk ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory d:\proj\HelpDesk\repo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ helpdesk ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-war-plugin:2.3:war (default-war) @ helpdesk ---
[INFO] Packaging webapp
[INFO] Assembling webapp [helpdesk] in [d:\proj\HelpDesk\repo\target\helpdesk]
[INFO] Processing war project
[INFO] Copying webapp resources [d:\proj\HelpDesk\repo\src\main\webapp]
[INFO] Webapp assembled in [40 msecs]
[INFO] Building war: d:\proj\HelpDesk\repo\target\helpdesk.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Piper
  • 1,266
  • 3
  • 15
  • 26
Alex
  • 367
  • 1
  • 3
  • 17
  • 2
    The question is why do you need such changed life cycle for what purpose? – khmarbaise Mar 06 '13 at 14:25
  • 4
    @khmarbaise Sort of academic interest. I don't like the idea that Maven invokes phases and plugins that I **know** have no work to do, spending computational resources and adding useless log entries. I am aware that unit tests are considered good practice by Maven, but still wonder if I could _disable_ them. Best way I found so far is ``maven.test.skip`` variable that minimized the log to what I posted. The question arose from my expectation of flexibility to adjust Maven to my project, my layout and my _build phases_. I realize it has minimal, or even unnoticeable performance impact. – Alex Mar 11 '13 at 10:16
  • 1
    @khmarbaise why can't I `maven.package.skip`? I have profile which when activated disables my verification plugin. But that's dangerous for me, and I want to prohibit the creation of a package by the JAR/shade/source/etc plugins since they would be incomplete. Basically I want to stop the build – caduceus Oct 21 '20 at 09:52
  • Although I'm not sure about every lifecycle phase, it's possible to skip the _test_ lifecycle phase during the default lifecycle with ``` -Dmaven.skip.test=true ``` See more here: https://stackoverflow.com/questions/5784778/maven-skip-test-compile-in-the-life-cycle. – Piper Dec 29 '21 at 23:24

1 Answers1

17

If you want to continue using a predefined packaging (jar, war, etc.) you cannot totally remove them. You may leave the packaging as pom and define your own lifecycle by binding the goals you want to the desired phases, or you may define your own lifecycle entirely. You may also bind a default goal to phase none to prevent the goal from doing any real work as shown.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>default-testResources</id>
            <phase>none</phase>
        </execution>
    </executions>
</plugin>

All that said - khmarbaise's question is a good one. Generally it's not a best practice to change the existing pre-built lifecycles.

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
user944849
  • 14,524
  • 2
  • 61
  • 83
  • 2
    With the given solution you only disable the particular plugin but not the life cycle itself. – khmarbaise Mar 09 '13 at 19:18
  • Agreed, setting phase to `none` doesn't change the lifecycle. If there are other plugins bound to the same lifecycle phase they will still run. That's why I provided the other ideas as well. – user944849 Mar 10 '13 at 21:01
  • 2
    Found [goals listing](http://www.sonatype.com/books/mvnref-book/reference/lifecycle-sect-package-specific.html) for bundled packaging types. The page says I need to write a plugin to define custom packaging type and a lifecycle for it. Is this the only way? Cannot I define it straight in a POM for my project with some `` tag? – Alex Mar 12 '13 at 13:31
  • The `goals listing` link from @Alex is broken. I found a saved version on [the internet archive](https://web.archive.org/web/20130526015323/http://books.sonatype.com/mvnref-book/reference/lifecycle-sect-package-specific.html). – Wimateeka May 28 '21 at 18:47