2011年11月5日土曜日

mavenでjavacのオプションを指定してみた


mavenのデフォルト設定では、アノテーションを使うとエラーになる。

デフォルト設定が、javacのオプションが-source 1.4と-target 1.4になっているのが原因。

pom.xmlで次のように-source 1.6と-target 1.6にすれば解決する。

<project>
  ...
  <build>
    <plugins>
      <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>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

参考情報



-source release
Specifies the version of source code accepted. The following values for release are allowed:
1.3
The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
1.4
The compiler accepts code containing assertions, which were introduced in JDK 1.4.
1.5
The compiler accepts code containing generics and other language features introduced in JDK 5.
5
Synonym for 1.5.
1.6
This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
6
Synonym for 1.6.

-target version
Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).
The default for -target depends on the value of -source:

If -source is not specified, the value of -target is 1.6
If -source is 1.2, the value of -target is 1.4
If -source is 1.3, the value of -target is 1.4
For all other values of -source, the value of -target is the value of -source.