如何解决没有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
)。通过在命令行上明确标识而不是附加到clean
或package
等阶段使用的其他常见插件包括versions
,dependency
和{{1 }}(这最后一个甚至不需要POM,因为它通常会创建新的POM)。
请注意,在您的POM中,您没有将archetype
附加到任何阶段(该插件通常不是);因此,在您从命令行显式运行插件的情况下,您的exec
条目仅用于提供配置设置,在特定情况下相当于plugin
属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。