【发布时间】: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