Understanding the purpose of Maven Plugin

Viewed 549

So far while working with Maven I was able to comprehend the difference between Maven Phases and Goals along with different Maven life cycle. What's bugging me though is why do we use plugins? I worked on an example where I used surefire plugin to execute my tests in below fashion:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <executions>
            <execution>
                <phase>test</phase>
                <goals><goal>surefire:test</goal></goals>
            </execution>
        </executions>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>testngChrome.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>
</plugins>

I do understand one use case of plugins here which allows me to pass my testNG.xml file to be used for execution.

What I want to understand is while running POM, we actually specify the phases/goals such as mvn clean test or mvn jar:jar. I know how these work differently but what I don't know why do we have the provision to specify Phases and Goals in POM under plugin.

Or Is there a way in which I can run this POM without mentioning Phase/Goals so that it picks up the phase/goals mentioned in Plugin?

If there is an existing answer which will help me in understanding this, feel free to guide me to it.

1 Answers

At the very bare minimum, maven cannot do much except paring xml files, tracking life cycle and using few plugins.

The true power of maven is realized when it makes use of plugin, which I will explain below.

A Maven Plugin is a collection of one or more goals. A jar plugin generates the jar file, compiler plugin will compile the code, surefire plugin will execute test cases and generate report.

Each plugin has number of goals associate to it. For example Compiler plugin has 2 goals:

compiler:compile is bound to the compile phase and is used to compile the main source files. compiler:testCompile is bound to the test-compile phase and is used to compile the test source files

You can read more here: [https://maven.apache.org/plugins/maven-compiler-plugin/][1]

Now each goal is a unit of task that needs to be executed. For example compiler:compile, the compiler goal has compile plugin to compile the source code.

Now the Maven core that you download initially does not have much to do with the tasks related to your project like compiling, packaging, deplying etc. It delegates this work to Compiler plugin, Jar plugin respectively.

When you use these plugins for the first time, these will be downloaded from maven repository. This way it is easy to modularize the functionalities of different plugins and if there is any update in plugin, it will be downloaded automatically from the repository and you will get the most recent changes of plugin.

Maven Lifecycle

Maven has a build lifecycle. The build lifecycle is an ordered sequence of phases involved in building a project. Put it differently, if you are working on a project it goes through a cycle of compile, test, build, deploy etc.

Maven can support a number of different lifecycles (I did not have need to use any lifecycle, other than default Maven lifecycle), but the one that’s most often used is the default Maven lifecycle, which begins with a phase to validate the basic integrity of the project and ends with a phase that involves deploying a project to production

Life Cycle Image: https://ibb.co/Msmkyjd

Plugin goals can be attached to a lifecycle phase. As Maven moves through the phases in a lifecycle, it will execute the goals attached to each particular phase. Each phase may have zero or more goals bound to it.

Now the package phase is going to create a JAR file for a project with jar packaging. But what of the goals preceding it, such as compiler:compile and preceding package in the Maven lifecycle; executing a phase will first execute all proceeding phases in order, ending with the phase specified on the command line.

Maven walks through the default lifecycle and executes the following goals:

resources:resources
compiler:compile
resources:testResources
compiler:testCompile
surefire:test
jar:jar

Put it simply, when we executed mvn install, Maven executes all phases up to the install phase. Underneath it will make use of default lifecycle and and it executes all goals bound to each phase in the lifecycle.

You can also run the goal individually:

mvn resources:resources 
compiler:compile 
resources:testResources 
compiler:testCompile 
surefire:test 
jar:jar 
install:install

As you can see from this example, it is easy to execute lifecycle phases, then it is to specify explicit goals on the command line. Also by making use of common lifecycle (defaul lifecycle), the developers can switch between different projects without having to worry much about the underlying details of the project.

Hope this helps to clarify your concepts about goals, phases, life cycle, plugins etc.

Related