0
0

Windows で cargo:run したときの Tomcat の文字化けを解消する

Posted at

環境

  • Windows 11
    • 表示言語: 日本語
    • システム ロケール: 日本語 (日本)
    • ワールドワイド言語サポートで Unicode UTF-8 を使用 はチェックしていない
  • JDK 17
  • Maven 3.9

困りごと

以下の通り pom.xml を書いて cargo:run すると、Tomcat のログが文字化けしている。

pom.xml
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>cargo-run-test</artifactId>
    <version>0.0.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven3-plugin</artifactId>
                <version>1.10.14</version>
                <configuration>
                    <container>
                        <containerId>tomcat9x</containerId>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

解決方法

  1. 一度、文字化けした状態で Tomcat を起動する
  2. ${project.basedir}/target/cargo/configurations/conf/logging.properties${project.basedir}/conf/logging.properties にコピーする
  3. ${project.basedir}/conf/logging.properties に以下の行を追加
    java.util.logging.ConsoleHandler.encoding = UTF-8
    
  4. pom.xml を以下のように修正
    pom.xml
    <project>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>cargo-run-test</artifactId>
        <version>0.0.0-SNAPSHOT</version>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.cargo</groupId>
                    <artifactId>cargo-maven3-plugin</artifactId>
                    <version>1.10.14</version>
                    <configuration>
                        <container>
                            <containerId>tomcat9x</containerId>
                        </container>
                        <configuration>
                            <configfiles>
                                <configfile>
                                    <file>${project.basedir}/conf/logging.properties</file>
                                    <todir>conf</todir>
                                </configfile>
                            </configfiles>
                        </configuration>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
  5. mvn clean cargo:run を実行

原因

cargo:run はログが UTF-8 で記録されることを期待しているが、java.util.logging.ConsoleHandler.encoding = UTF-8 がない場合にはコンソール エンコーディングが使われるため、ここで文字化けが発生する。

ログをコンソール エンコーディングで読み取るようにするか、デフォルトの構成に java.util.logging.ConsoleHandler.encoding = UTF-8 を含めるのが本来の解決方法だと思われるが、私はバグ報告していない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0