Set Maven compiler version from the command line

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;

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.

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;

If you find this post useful, Please share or leave a comment below 🙂