英文:
How does exec.mainClass work without exec-maven-plugin?
问题
我有一个项目使用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如何在没有显式定义插件的情况下处理它。
虽然我更喜欢较短的文件,但我希要确保我理解它是如何工作的,并且没有漏掉任何可能会在以后出现问题的内容。
英文:
I have a project that uses the MojoHaus Exec Maven plugin to run some Java code. Here's the pom.xml
file:
<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>
This works fine, and I can run my code using this command:
mvn clean package exec:java
I understand that the exec-maven-plugin
plugin, which is specified in the plugins
tag, runs the code using the mainClass
property.
I was surprised to find that this pom.xml
also works:
<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>
This file specifies the exec.mainClass
property, but does not specify any plugins. However, I can still run my code just fine with this same command:
mvn clean package exec:java
But I don't understand how Maven knows to run this command without any plugins specified.
Is the Exec Maven plugin somehow automatically installed in Maven by default? Or is exec.mainClass
somehow setting a property used by a different default tool within Maven?
I tried reading the official documentation, but I didn't see anything that mentions whether the plugin is included by default.
I've found that I can also pass the exec.mainClass
property in as a command line argument, but I still don't understand how Maven knows what to do with that without the plugin being explicitly defined.
I much prefer the shorter file, but I want to make sure I understand how it's working and that I'm not missing anything that's going to bite me later.
答案1
得分: 1
当您指定exec:java
时,您正在指定一个插件,具体地说,是exec-maven-plugin
(以及目标java
)。其他常见的插件,通过在命令行上显式标识而不是附加到诸如clean
或package
等阶段的插件,包括versions
,dependency
和archetype
(最后一个甚至不需要存在POM,因为它通常会创建新的POM)。
请注意,在您的POM中,您没有将exec
附加到任何阶段(通常不会附加该插件);因此,在您从命令行显式运行插件的情况下,您的plugin
条目仅用于在特定情况下提供配置设置,相当于exec.mainClass
属性。
英文:
When you specify exec:java
, you are specifying a plugin, specifically exec-maven-plugin
(along with the goal java
). Other common plugins that are used by being explicitly identified on the command line rather than being attached to phases such as clean
or package
include versions
, dependency
, and archetype
(this last of which doesn't even require a POM to be present, since it generally creates new ones).
Note that in your POM you don't attach exec
to any phases (that plugin usually isn't); therefore your plugin
entry serves only to provide configuration settings in the case you run the plugin explicitly from the command line, in your specific case equivalent to the exec.mainClass
property.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论