Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 836
Vicky Avatar asked Mar 02 '11 18:03

Vicky


People also ask

What are the 3 build lifecycle of Maven?

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's web site.

What is Maven clean lifecycle?

A Maven lifecycle consists of a sequence of named phases: prepare-resources, compile, package, and install among other. There is phase that captures compilation and a phase that captures packaging.

What is meant by Maven clean?

The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.


1 Answers

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>
like image 86
Matthew Gilliard Avatar answered Sep 24 '22 03:09

Matthew Gilliard