LoginSignup
3
1

More than 3 years have passed since last update.

mavenでコマンドラインアプリ作成

Last updated at Posted at 2019-06-10

最小限のMavenプロジェクトを作成

Eclipse(正確にはSTS)でデフォルトのままMavenプロジェクトを作成するとPOMはこんな感じになります。

pom.xml
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

適当にクラス作成して...

Main.java
package jp.demo;

public class Main {
    public static void main(String[] args){
        System.out.println("### START ###");
        System.out.println("### END ###");
    }
}

実行すると...

maven install後、Jarを実行するとマニフェストがないと怒られます。

コマンドライン
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
test-0.0.1-SNAPSHOT.jarにメイン・マニフェスト属性がありません
manifestファイルを生成する

mainClassを指定

そこで以下のようにbuildタグから順にmainClassタグを指定します。
mainClassタグにはパッケージからメインクラスを指定します。

pom.xml
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>jp.demo.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

再度実行すると...

maven install後、再度実行するとちゃんと正常終了しました。

コマンドライン
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
### START ###
### END ###

こんなエラーが出たら...

  • 「ダイヤモンド演算子を使用可能にするには、-source 7以降を使用してください」
  • 「ラムダ式を使用可能にするには、-source 8以上を使用してください」

コンパイラのバージョンが低いということですので以下を追加しましょう。

pom.xml
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

こんなエラーが出たら2...

  • POM.xmlの先頭で「Unknown Error」

以下を上記同様にpropertiesに追加しましょう。(maven-jar-plugin 3.1.2のバグのようです。別プロジェクトで出ました。)

pom.xml
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>

参考:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=547409

最終的に

こうなりました。

pom.xml
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>jp.demo.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
3
1
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
3
1