hakish
hakish

Reputation: 4048

maven compiler plugin 2.0.2

Can you please tell me is it mandatory to specify the maven-compiler-plugin details in my POM under:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
            <source>1.5</source>
            <target>1.5</target>
            </configuration>

If yes why so, i understand what it does, but not sure why its required? Isnt there any other way to invoke the javac in maven?

Upvotes: 4

Views: 2459

Answers (2)

Nishant
Nishant

Reputation: 55866

Can you please tell me is it mandatory to specify the maven-compiler-plugin details?

No. But it will use Java 1.3 or something from stone age to compile.

If yes why so, i understand what it does, but not sure why its required?

The answer is 'No'. But here is the reason. You sure do not want to compile using Java 1.3 on your new code. You want all the new features in Java 5. Don't you? :) So, you have to add these extra lines in your already cluttered POM.

Isnt there any other way to invoke the javac in maven?

There isn't. But mvn compile or any other command will work even if you do not have this block. But compilation will fail if your source code has any advance stuffs that is in Java 5, but not in previous version.


Edit 1

How does it compile to 1.3 when I have JDK 5?

Well, there is an option to set to do so in Java compiler. See Java compiler options here: http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html

It says you can set source et al to older version.

Upvotes: 7

munyengm
munyengm

Reputation: 15479

The maven-compiler-plugin section is optional. Defaults are applied if you don't include that section in your pom.xml.

The default compiler is javac and is used to compile Java sources. Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with.

My personal preference is to have an explicit configuration for the reasons below.

  1. I do not think relying on external defaults is a good idea as these may change when the version of the plugin is changed.
  2. It is unlikely that I would want a new project to be compiled for Java 1.5

Upvotes: 4

Related Questions