Web系開発メモ

Java, C#, HTML, CSS, JavaScript のことなどを書いてます。

MavenでJavaプログラムを実行する方法(Exec Pluginの使用方法)

Exec Maven Plugin を使って、Java のプログラムを実行する方法を書いていきます。

1. 実行方法

pom.xml に、プラグインと実行したいクラスを追加します。

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>[MainクラスのFQCN]</mainClass>
        </configuration>
      </plugin>
      ...
    </plugins>
    ...
  </build>
   ...
</project>

それから、以下のコマンドを実行します。

mvn exec:java

コンパイルをしていない場合は、compile の次に exec:java を実行します。

mvn compile exec:java

clean する場合も、compile が必要になります。

mvn clean compile exec:java

2. 実行例

プロジェクト sample-exec を作成して、Mainクラスを実行する例を記載します。

2.1. pom.xml の作成

以下の pom.xml を作成します。

sample-exec/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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.sample</groupId>
  <artifactId>sample-exec</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <mainClass>org.sample.exec.Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2.2. Mainクラスの作成

以下のメインクラスを作成します。

sample-exec/src/main/java/org/sample/exec/Main.java

package org.sample.exec;

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

2.3. 実行

ディレクトsample-exec で、以下のコマンドを実行します。

mvn exec:java

3. コンパイルについて

compile をしないで exec:java を実行すると、以下のエラーが出力されました。

[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.sample:sample-exec >-----------------------
[INFO] Building sample-exec 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ sample-exec ---
[WARNING]
java.lang.ClassNotFoundException: org.sample.exec.Main
    at org.codehaus.mojo.exec.URLClassLoaderBuilder$ExecJavaClassLoader.loadClass (URLClassLoaderBuilder.java:198)
    at java.lang.ClassLoader.loadClass (ClassLoader.java:520)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:271)
    at java.lang.Thread.run (Thread.java:833)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.235 s
[INFO] Finished at: 2022-10-14T19:45:22+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.1.0:java (default-cli) on project sample-exec: An exception occurred while executing the Java class. org.sample.exec.Main -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

4. 定義せずに実行する方法

pom.xml に定義をしなくても、コマンドラインでクラス名を渡せば実行できます。

mvn exec:java -Dexec.mainClass="org.sample.exec.Main"

5. 参考サイト

Exec Maven Plugin - Usage