`exec.mainClass`在没有使用`exec-maven-plugin`的情况下如何工作?

huangapple go评论69阅读模式
英文:

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:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;io.happycoding&lt;/groupId&gt;
  &lt;artifactId&gt;google-cloud-vision-hello-world-standalone&lt;/artifactId&gt;
  &lt;version&gt;1&lt;/version&gt;

  &lt;properties&gt;
    &lt;mainClass&gt;io.happycoding.vision.CloudVisionHelloWorld&lt;/mainClass&gt;
    &lt;exec.cleanupDaemonThreads&gt;false&lt;/exec.cleanupDaemonThreads&gt;
    &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
      &lt;artifactId&gt;google-cloud-vision&lt;/artifactId&gt;
      &lt;version&gt;1.100.0&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
        &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.0.0&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;java&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;mainClass&gt;${mainClass}&lt;/mainClass&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
&lt;/project&gt;

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:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

  &lt;groupId&gt;io.happycoding&lt;/groupId&gt;
  &lt;artifactId&gt;google-cloud-vision-hello-world-standalone&lt;/artifactId&gt;
  &lt;version&gt;1&lt;/version&gt;

  &lt;properties&gt;
    &lt;exec.mainClass&gt;io.happycoding.vision.CloudVisionHelloWorld&lt;/exec.mainClass&gt;
    &lt;exec.cleanupDaemonThreads&gt;false&lt;/exec.cleanupDaemonThreads&gt;
    &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
      &lt;artifactId&gt;google-cloud-vision&lt;/artifactId&gt;
      &lt;version&gt;1.100.0&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/project&gt;

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)。其他常见的插件,通过在命令行上显式标识而不是附加到诸如cleanpackage等阶段的插件,包括versionsdependencyarchetype(最后一个甚至不需要存在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.

huangapple
  • 本文由 发表于 2020年9月13日 06:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63865462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定