Reputation: 1158
I'm learning about Maven and I try to understand the difference between lifecycle and plugins. For example I have a very simple Maven project like this:
import org.apache.commons.lang3.StringUtils;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!!!! ");
System.out.println(StringUtils.capitalize("hello world"));
}
}
Here is the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>guru.springframework</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If I use this command: mvn compiler:compile
, it creates the target folder and in the HelloWorld.class I see this: Decompiled .class file, bytecode version: 55.0 (Java 11)
. So it is using Java 11.
Now if I change the Java version in the pom.xml to 1.8 and use again the command mvn compiler:compile
I see the message: Nothing to compile - all classes are up to date
. And in the target folder, the HelloWorld.class has: Decompiled .class file, bytecode version: 55.0 (Java 11)
. So it shows Java 11 after I changed it to 1.8 in the pom.xml.
And after that, if I use the command mvn compile
, first it uses the maven-clean-plugin (auto-clean)
, then maven-resources-plugin (default-resources)
and then maven-compiler-plugin (default-compiler)
with the message: Changes detected - recompiling the module!
. And in the target folder, the HelloWorld.class has: Decompiled .class file, bytecode version: 52.0 (Java 8)
. So now it uses Java 8.
So I don't understand why if I'm using the command mvn compiler:compile
it doesn't uses the auto-clean
added by me in the pom.xml. But if I'm using mvn compile
it uses the auto-clean
. Could somebody help me to understand?
And also I don't understand why if I use the command mvn clean
, it uses the default-clean
. But if I use the command mvn compile
, it uses the auto-clean
. Any feedback will be appreciated. Thank you!
Upvotes: 1
Views: 663
Reputation: 182
The difference between phases and plugin goals is that phases are groups of different plugin goal executions.
Note that all phases that are in lifecycle before the currently are executed as well, i.e. executing compile
phase also causes validate
, initialize
, generate-sources
, process-sources
, generate-resources
and process-resources
to run.
When you directly specify plugin goal on command line, you run it ignoring the whole lifecycle.
Upvotes: 1