The Maven Compiler Plugin helps you to compile Java source code. This plugin compiles your project’s sources. Since version 3.0, the default compiler is javax.tools.JavaCompiler , which compiles Java sources. The compile goal is associated with the compile phase and is used to compile the primary source files. The test-compile goal is associated with the test-compile phase and compiles the test source files.
Example of Maven Compiler Plugin Dependency<dependencies>
<!-- Define dependencies required by the project -->
<dependency>
<!-- Specifies the group ID for the JUnit framework -->
<groupId>junit</groupId>
<!-- Specifies the artifact ID for the JUnit framework -->
<artifactId>junit</artifactId>
<version>3.13.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Troubleshooting of Maven compiler plugin- By checking the Java version: Make sure the Java version you’re using is compatible with the source version. Please ensure that you have the right Java version.
- By verifying the Maven version: Ensure that you are using the most recent and updated version of Maven.
- By checking Maven Plugins: Ensure that all needed plugins are installed. They must be included in the ‘.xml’ file for your project.
Key configuration elements of the Maven Compiler Plugin- <target>: Describes which Java version of the resulting bytecode should be utilized. for instance, Take \target>1.9</target>.
- <source>: Indicates the Java version of the source code that should be utilized. for instance, Take \source>1.9</source>.
- <fork>: Indicates if the compiler should run in a different process. As an example, take <fork>true</fork>
- <encoding>: Configures the source files’ character encoding.
- <compilerVersion>: It indicates the compiler version to be used.
Configuration of the Maven compiler pluginStep 1: Create Compiler PluginUnlike many other plugins, the Compiler Plugin executes automatically during its phases, therefore you do not need to specify executions. However, you must mention the version of the Compiler Plugin.
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project> Step 2: Set the Java versionWe can make the Java version one of the POM’s properties for convenience.
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties> Step 3: Compile Sources Using A Different JDKThe compiler version parameter allows you to select the version of the compiler that the plugin will use. You must also set the fork option to true to make this function.
XML
<project>
<build>
<plugins>
<plugin>
<!-- Specifies the group ID of the Maven Compiler Plugin -->
<groupId>org.apache.maven.plugins</groupId>
<!-- Specifies the artifact ID of the Maven Compiler Plugin -->
<artifactId>maven-compiler-plugin</artifactId>
<!-- Specifies the version of the Maven Compiler Plugin to use -->
<version>3.13.0</version>
<configuration>
<!-- Enables verbose output for more detailed information during compilation -->
<verbose>true</verbose>
<!-- Indicates that the compilation should be forked to a separate process -->
<fork>true</fork>
<!-- Specifies the path to the javac executable -->
<!-- Replace with the actual path to your javac executable -->
<executable><!-- path-to-javac --></executable>
<compilerVersion>1.6</compilerVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step4: Create a class for MavenCompilerPlugin Create a MavenCompilerPlugin class that imports a package from another module.
Java
public class MavenCompilerPlugin {
private static final String XML_NS_PREFIX = "http://maven.apache.org/POM/4.0.0";
public static void main(String[] args) {
System.out.println("The XML namespace in the prefix is: ");
}
}
- In the public class, it can be accessed from other classes.
- The main method is static, which means it can be invoked without generating a new class instance.
- It accepts an array of String objects as its parameter, supplying command-line parameters to the application.
|