代码之家  ›  专栏  ›  技术社区  ›  Kevin Workman

没有exec-maven插件,exec.mainClass如何工作?

  •  0
  • Kevin Workman  · 技术社区  · 4 年前

    我有一个使用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干净包exec:java
    

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

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

    我试着读 official documentation ,但我没有看到任何提到默认情况下是否包含该插件的内容。

    我已经 found 我也可以通过 exec.mainClass 属性作为命令行参数输入,但我仍然不明白Maven在没有明确定义插件的情况下如何处理它。

    我更喜欢较短的文件,但我想确保我了解它是如何工作的,并且我不会错过任何以后会咬我的东西。

    1 回复  |  直到 4 年前
        1
  •  1
  •   chrylis -cautiouslyoptimistic-    4 年前

    当您指定时 exec:java ,你 具体指定插件 exec-maven-plugin (连同 the goal java ). 其他通过在命令行上明确标识而不是附加到阶段来使用的常见插件,例如 clean package 包括 versions , dependency ,以及 archetype (最后一个甚至不需要POM,因为它通常会产生新的POM)。

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