「ソースのエンコーディングを指定するには」の版間の差分
提供: tknotebook
| 1行: | 1行: | ||
[[Category:コンピュータ]][[Category:Java]][[Category:Maven Tips]] | [[Category:コンピュータ]][[Category:Java]][[Category:Maven Tips]] | ||
[[メインページ]]>[[コンピュータの部屋#Java]]>[[Maven Tips]] | [[メインページ]]>[[コンピュータの部屋#Java]]>[[Maven Tips]] | ||
| + | |||
| + | |||
| + | Mavenでコンパイルを行うと、ソースファイルのエンコーディングの既定値は | ||
| + | Mavenを実行しているプラットフォームの既定値になります。Windowsの場合は Shift_JIS です。 | ||
| + | |||
| + | 今時、Javaのソースは UTF-8 で書くのが一般的ですからこれでは困ります。 | ||
| + | |||
| + | |||
| + | maven-compiler-plugin の設定を変更するのが正攻法で、pom.xml の buildタグの中に(なければbuildタグを加え) | ||
| + | 以下の記述を加えます。 | ||
| + | |||
| + | <pre> | ||
| + | <build> | ||
| + | <plugins> | ||
| + | <plugin> | ||
| + | <artifactId>maven-compiler-plugin</artifactId> | ||
| + | <version>3.5</version> | ||
| + | <configuration> | ||
| + | <encoding>UTF-8</encoding> | ||
| + | </configuration> | ||
| + | </plugin> | ||
| + | </plugins> | ||
| + | </build> | ||
| + | </pre> | ||
| + | |||
| + | これよりももっと簡単な方法があります。pom.xml の末尾(projectタグの内側の一番最後)に | ||
| + | |||
| + | <pre> | ||
| + | <properties> | ||
| + | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| + | </properties> | ||
| + | </pre> | ||
| + | |||
| + | を加える方法です。project.build.sourceEncoding プロパティは、maven-compiler-plugin が参照している | ||
| + | ソースファイルのエンコーディングの既定値で、このプロパティが存在していない場合は、プラットフォームの既定値になります。 | ||
| + | |||
| + | 尚、project.build.sourceEncoding プロパティは maven-compiler-plugin だけではなく、他のプラグインも参照しているので、 | ||
| + | ソースファイルのエンコーディングを、プロジェクトで UTF-8 に統一するときに有用です。 | ||
| + | 通常はこちらを使えばよいでしょう。 | ||
2016年10月19日 (水) 04:45時点における版
メインページ>コンピュータの部屋#Java>Maven Tips
Mavenでコンパイルを行うと、ソースファイルのエンコーディングの既定値は
Mavenを実行しているプラットフォームの既定値になります。Windowsの場合は Shift_JIS です。
今時、Javaのソースは UTF-8 で書くのが一般的ですからこれでは困ります。
maven-compiler-plugin の設定を変更するのが正攻法で、pom.xml の buildタグの中に(なければbuildタグを加え)
以下の記述を加えます。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
これよりももっと簡単な方法があります。pom.xml の末尾(projectタグの内側の一番最後)に
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
を加える方法です。project.build.sourceEncoding プロパティは、maven-compiler-plugin が参照している ソースファイルのエンコーディングの既定値で、このプロパティが存在していない場合は、プラットフォームの既定値になります。
尚、project.build.sourceEncoding プロパティは maven-compiler-plugin だけではなく、他のプラグインも参照しているので、 ソースファイルのエンコーディングを、プロジェクトで UTF-8 に統一するときに有用です。 通常はこちらを使えばよいでしょう。