In this quick tutorial, it shows how to set the Maven compiler version from both pom.xml and the command line (as a parameter to mvn). This comes in handy if you run in to situations like;
1 2 3 4 5 6 7 8 9 10 11 |
generics are not supported in -source 1.3 (use -source 5 or higher to enable generics) public List<XmlSuite> initializeSuitesAndJarFile(String jarPath) { for-each loops are not supported in -source 1.3 (use -source 5 or higher to enable for-each loops) for (String currentFile : toBeParsed) { annotations are not supported in -source 1.3 (use -source 5 or higher to enable annotations) @Override public void initialize() { |
The reason for this errors is Maven compiler version. Earlier, Maven 2 shipped with compiler version 1.3 by default. Later on they changed it to compiler version 1.5 by default. In Maven 3, it allows the user to set the compiler version using the parent pom of the project ( pom.xml in root of the project) by adding below plugin configuration.
1 2 3 4 5 6 7 8 9 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> |
However, when you are not in a position to edit the parent pom of the project (i.e. you might not be the owner of the project or you might not be in the team maintaining the project) and you still need to change it since you are using a different maven/compiler version by default, you can use -Dmaven.compiler.source and -Dmaven.compiler.target parameters with maven command line tool ( mvn) like shown below;
1 |
$ mvn clean install -Dmaven.compiler.source=1.6 -Dmaven.compiler.target=1.6 |
If you find this post useful, Please share or leave a comment below 🙂