Search code examples
maven-2buildmavenbuild-process

Overriding 'clean' lifecycle in Maven


I was going through a book that explain how to override 'default' lifecycle of Maven.

It says: To define a new lifecycle for a packaging type, you'll need to configure a LifecycleMapping component in Plexus. In your plugin project, create a META-INF/plexus/components.xml under src/main/resources. In components.xml add the content as shown below, and you're done. With below configuration, I'm able to customize the default lifecycle for 'jar' packaging type. Now If I exeute
$ mvn package
It straigh away executes 'package' phase skipping all other phases of default lifecycle and executes 'echo' goal of 'maven-zip-plugin'.

<component-set>
    <components>
        <component>
            <role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
            <role-hint>zip</role-hint>
            <implementation>
                org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping
            </implementation>
            <configuration>
                <phases>
                    <package>org.sonatype.mavenbook.plugins:maven-zip-plugin:echo
                    </package>
                </phases>
            </configuration>
        </component>
    </components>
</component-set>

My question is: How can I customize 'clean' lifecycle. For example, assume when some one types
$ mvn clean
Instead of running clean:clean that will execute 'clean' goal of 'maven-clean-plugin' plugin, I wanted to execute 'customClean' goal of 'customPlugin'.


Solution

  • For what you describe, it is simpler to just prevent the maven-clean-plugin from running during the clean phase, and attach customPlugin to the clean phase instead. This is simpler than short-circuiting the whole lifecycle, and keeps all your maven config in your pom.

    1 prevent the maven-clean-plugin

    <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
            <skip>true</skip>
        </configuration>
    </plugin>
    

    2 attach your own plugin to the clean phase

    <plugin>
        <artifactId>maven-customPlugin-plugin</artifactId>
        <version>customPlugin-version</version>
        <executions>
            <execution>
                <id>customised-clean</id>
                <goals>
                    <goal>customClean</goal>
                </goals>
                <phase>clean</phase>
            </execution>
        </executions>
    </plugin>