【问题标题】:How does exec.mainClass work without exec-maven-plugin?没有 exec-maven-plugin 的 exec.mainClass 如何工作?
【发布时间】:2020-12-31 01:31:04
【问题描述】:

我有一个项目使用 MojoHaus Exec Maven plugin 运行一些 Java 代码。这是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>io.happycoding</groupId>
  <artifactId>google-cloud-vision-hello-world-standalone</artifactId>
  <version>1</version>

  <properties>
    <mainClass>io.happycoding.vision.CloudVisionHelloWorld</mainClass>
    <exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-vision</artifactId>
      <version>1.100.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>${mainClass}</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这很好,我可以使用这个命令运行我的代码:

mvn clean package exec:java

我了解exec-maven-plugin 插件(在plugins 标记中指定)使用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>io.happycoding</groupId>
  <artifactId>google-cloud-vision-hello-world-standalone</artifactId>
  <version>1</version>

  <properties>
    <exec.mainClass>io.happycoding.vision.CloudVisionHelloWorld</exec.mainClass>
    <exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-vision</artifactId>
      <version>1.100.0</version>
    </dependency>
  </dependencies>
</project>

此文件指定exec.mainClass 属性,但未指定任何插件。但是,我仍然可以使用相同的命令运行我的代码:

mvn clean package exec:java

但我不明白 Maven 是如何知道在没有指定任何插件的情况下运行此命令的。

Exec Maven 插件是否默认自动安装在 Maven 中?或者exec.mainClass 是否以某种方式设置了 Maven 中不同默认工具使用的属性?

我尝试阅读official documentation,但没有看到任何提及该插件是否默认包含的内容。

我有found,我也可以将exec.mainClass 属性作为命令行参数传递,但我仍然不明白如果没有明确定义插件,Maven 如何知道如何处理它。

我更喜欢较短的文件,但我想确保我了解它是如何工作的,并且我不会遗漏任何会影响我的东西。

【问题讨论】:

    标签: java maven pom.xml exec-maven-plugin


    【解决方案1】:

    当您指定exec:java 时,您正在指定一个插件,特别是exec-maven-plugin(以及the goal java)。通过在命令行上明确标识而不是附加到诸如cleanpackage 等阶段的其他常见插件包括versionsdependencyarchetype(最后一个不甚至需要存在 POM,因为它通常会创建新的)。

    请注意,在您的 POM 中,您不会将 exec 附加到任何阶段(该插件通常不是);因此,您的 plugin 条目仅在您从命令行显式运行插件的情况下提供配置设置,在您的特定情况下相当于 exec.mainClass 属性。

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 2014-10-15
      • 2014-04-19
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2014-04-23
      • 2015-08-26
      • 2014-03-10
      相关资源
      最近更新 更多