使用Maven Exec运行Java类 | AI生成和翻译

Home 2025.07

Maven Exec 插件的 exec:java 目标是一种直接从 Maven 构建运行具有 main 方法的 Java 类的强大方式。它在与 Maven 构建相同的 JVM 内执行指定的 Java 类,并使用项目的依赖项作为类路径。

以下是使用方法,包含常见场景和示例:

1. 从命令行基本使用

使用 exec:java 最简单的方式是直接从命令行,特别适用于快速测试或运行特定的工具类。

命令:

mvn exec:java -Dexec.mainClass="com.yourcompany.YourMainClass"

解释:

示例:

假设您有一个包含 main 方法的类 com.example.myapp.HelloWorld

package com.example.myapp;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Maven exec:java!");
    }
}

您将使用以下命令运行它:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld"

注意: 最好先运行 mvn compile,以确保在 exec:java 尝试运行您的类之前它们已被编译。

2. 向 Java 程序传递参数

您可以使用 exec.args 系统属性向 Java 程序的 main 方法传递参数:

命令:

mvn exec:java -Dexec.mainClass="com.yourcompany.YourMainClass" -Dexec.args="arg1 arg2 \"arg with spaces\""

示例:

如果您的 HelloWorld 类是:

package com.example.myapp;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from Maven exec:java!");
        if (args.length > 0) {
            System.out.println("Arguments received: ");
            for (String arg : args) {
                System.out.println("- " + arg);
            }
        }
    }
}

您将这样运行它:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld" -Dexec.args="FirstArgument SecondArgument"

对于包含空格的参数,请用引号括起来:

mvn compile exec:java -Dexec.mainClass="com.example.myapp.HelloWorld" -Dexec.args="\"Hello World\" AnotherArg"

3. 在 pom.xml 中配置 exec:java

对于更持久或默认的配置,您可以将 exec-maven-plugin 添加到您的 pom.xml 中。这允许您定义默认的 mainClass 和其他参数,因此您不必每次都在命令行中指定它们。

pom.xml 配置:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yourcompany</groupId>
    <artifactId>my-maven-app</artifactId>
    <version>1.0-SNAPSHOT</modelVersion>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.2.0</version> <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.myapp.HelloWorld</mainClass>
                    <arguments>
                        <argument>defaultArg1</argument>
                        <argument>defaultArg2</argument>
                    </arguments>
                    <systemProperties>
                        <systemProperty>
                            <key>my.custom.property</key>
                            <value>someValue</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>
        </plugins>
    </build>

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

</project>

配置选项解释:

使用 pom.xml 配置运行:

一旦在 pom.xml 中配置:

4. 与 exec:exec 的主要区别

理解 exec:javaexec:exec 之间的区别很重要:

对于在 Maven 构建中执行大多数标准 Java 应用程序,exec:java 是更方便的选择。

5. 重要注意事项


Back Donate