2012年9月16日日曜日

m2e で Eclipse + Maven 環境を作る時に J2SE を指定する

これまで、 の両記事で m2e での import の動作についてみてきた。
m2e で pom.xml をインポートする際に、

Build path specifies execution environment J2SE-1.5. 
There are no JREs installed in the workspace that are strictly compatible with this environment.

という警告が出ていたのだが、これは pom.xml に maven-compiler-plugin の configuration を書くことで抑止できる。
やってみる。



新しい pom.xml は以下の通り。赤字が追加したところ。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>hoge</groupId>
  <artifactId>hoge</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>hoge</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>  
  </build>
  
</project>

maven-compiler-plugin の設定で source と target のバージョンを指定している。
元記事では 1.6 が指定されていたが ここでは 1.7 にした。
インポートすると確かに警告が出なくなった。



作成された .classpath と設定ファイルを見てみよう。

.classpath :
...
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
    <attributes>
      <attribute name="maven.pomderived" value="true"/>
    </attributes>
  </classpathentry>
...
JRE_CONTAINER の指定が JavaSE-1.7 になっている。

.settings/org.eclipse.jdt.core.prefs :
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
「Compiler compliance lecev」、「Generated .class files compatibility」、「Source compatibility」が デフォルトでは 1.5 だったのが 1.7 になっている。



pom.xml で Java のバージョンを記載することで import 後の警告を抑止できることが分かった。
ただ、プロジェクト個別の設定内容を変えて警告が出ないようになっているので、 個人的には、 「m2e で Eclipse + Maven 環境を作る。」 の方法で プロジェクト固有の設定を除去したほうが気持ちはいい。

0 件のコメント:

コメントを投稿