我有一个项目,它使用MojoHaus Exec Maven插件来运行一些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
我知道plugins
标记中指定的exec-maven-plugin
插件使用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中另一个默认工具使用的属性?
我试着阅读了官方文档,但没有看到任何提到插件是否默认包含的内容。
我发现我也可以将exec.mainClass
属性作为命令行参数传入,但我仍然不明白Maven如何知道在没有明确定义插件的情况下如何处理它。
我更喜欢较短的文件,但我想确保我了解它是如何工作的,并且我不会错过任何稍后会咬我的东西。
当您指定exec:java
时,您正在指定一个插件,特别是exec-maven-plugin
(以及目标java
(。通过在命令行上显式标识而不是附加到阶段(如clean
或package
(来使用的其他常见插件包括versions
、dependency
和archetype
(最后一个插件甚至不需要POM,因为它通常会创建新的POM(。
请注意,在您的POM中,您不会将exec
附加到任何阶段(该插件通常不会(;因此,您的plugin
条目仅在您从命令行显式运行插件的情况下提供配置设置,在您的特定情况下相当于exec.mainClass
属性。