微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有exec-maven-plugin的exec.mainClass如何工作?

如何解决没有exec-maven-plugin的exec.mainClass如何工作?

我有一个使用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如何知道在没有显式插件的情况下该怎么做。定义。

我更喜欢较短的文件,但我想确保自己了解它的工作原理,并且不会丢失以后会咬我的任何东西。

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。