Java Maven Plugins & Profiles
In this page:
What are Maven Plugins?
Plugins extend what Maven can do beyond its built-in lifecycle, and every meaningful action Maven takes, even compiling source code, is actually implemented as a goal provided by a plugin bound to a lifecycle phase.
Example: What are Maven Plugins?
public class Main {
public static void main(String[] args) {
// Even "compile" is implemented by maven-compiler-plugin's compile goal
System.out.println("Every Maven action is a plugin goal bound to a lifecycle phase");
}
}
Login to try C/C++/Java/PHP code in the editor
The Compiler Plugin
The maven-compiler-plugin controls how Java source code gets compiled, and configuring its source and target properties determines which Java language version the build expects and produces bytecode for.
Example: The Compiler Plugin
public class Main {
public static void main(String[] args) {
String config = "<plugin>\n <artifactId>maven-compiler-plugin</artifactId>\n <configuration><source>17</source><target>17</target></configuration>\n</plugin>";
System.out.println(config);
}
}
Login to try C/C++/Java/PHP code in the editor
The Surefire Plugin for Tests
The maven-surefire-plugin runs unit tests during the test phase and can be configured or targeted to run a specific test class, control whether failures stop the build, or skip tests entirely for a faster build.
Example: The Surefire Plugin for Tests
public class Main {
public static void main(String[] args) {
String command = "mvn test -Dtest=MyTestClass"; // targets a specific test class via surefire
System.out.println(command);
}
}
Login to try C/C++/Java/PHP code in the editor
Build Profiles
A build profile lets pom.xml configuration change based on context, such as different settings for development versus production, and a profile is only applied when it's explicitly activated, often by name on the command line.
Example: Build Profiles
public class Main {
public static void main(String[] args) {
String activation = "mvn install -Pproduction"; // profile only applied when explicitly activated
System.out.println(activation);
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Plugin Configuration
Plugins can be configured with custom settings specific to a project's needs, such as embedding a main class in a jar's manifest so it can be run directly, or invoking a specific class through the exec plugin during development.
Example: Custom Plugin Configuration
public class Main {
public static void main(String[] args) {
String manifest = "<mainClass>com.example.Main</mainClass>"; // embedded in the jar's manifest
System.out.println(manifest);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: