如何使用 Maven 执行程序?

发布于 2024-08-25 15:39:01 字数 942 浏览 5 评论 0 原文

我想让 Maven 目标触发 java 类的执行。我正在尝试使用以下几行迁移 Makefile

neotest:
    mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse"

我希望 mvn neotest 生成 make neotest 目前所做的事情。

exec 插件文档Maven Ant 任务 页面有任何简单的示例。

目前,我处于:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1</version>
  <executions><execution>
    <goals><goal>java</goal></goals>
  </execution></executions>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

不过,我不知道如何从命令行触发插件。

I would like to have a Maven goal trigger the execution of a java class. I'm trying to migrate over a Makefile with the lines:

neotest:
    mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse"

And I would like mvn neotest to produce what make neotest does currently.

Neither the exec plugin documentation nor the Maven Ant tasks pages had any sort of straightforward example.

Currently, I'm at:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1</version>
  <executions><execution>
    <goals><goal>java</goal></goals>
  </execution></executions>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

I don't know how to trigger the plugin from the command line, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲鸠 2024-09-01 15:39:02

使用您为 exec-maven-plugin 定义的全局配置

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

在命令行上调用 mvn exec:java 将调用配置为执行该类的插件org.dhappy.test.NeoTraverse

因此,要从命令行触发插件,只需运行:

mvn exec:java

现在,如果您想将 exec:java 目标作为标准构建的一部分执行,您需要将目标绑定到默认生命周期的特定阶段 。为此,请在 execution 元素中声明要将目标绑定到的 phase

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4</version>
  <executions>
    <execution>
      <id>my-execution</id>
      <phase>package</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

在本例中,您的类将在 package< 期间执行/代码> 阶段。这只是一个示例,请根据您的需要进行调整。也适用于插件版本 1.1。

With the global configuration that you have defined for the exec-maven-plugin:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

invoking mvn exec:java on the command line will invoke the plugin which is configured to execute the class org.dhappy.test.NeoTraverse.

So, to trigger the plugin from the command line, just run:

mvn exec:java

Now, if you want to execute the exec:java goal as part of your standard build, you'll need to bind the goal to a particular phase of the default lifecycle. To do this, declare the phase to which you want to bind the goal in the execution element:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4</version>
  <executions>
    <execution>
      <id>my-execution</id>
      <phase>package</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

With this example, your class would be executed during the package phase. This is just an example, adapt it to suit your needs. Works also with plugin version 1.1.

无边思念无边月 2024-09-01 15:39:02

为了执行多个程序,我还需要一个 profiles 部分:

<profiles>
  <profile>
    <id>traverse</id>
    <activation>
      <property>
        <name>traverse</name>
      </property>
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <configuration>
            <executable>java</executable>
            <arguments>
              <argument>-classpath</argument>
              <argument>org.dhappy.test.NeoTraverse</argument>
            </arguments>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

然后可以将其执行为:

mvn exec:exec -Ptraverse

In order to execute multiple programs, I also needed a profiles section:

<profiles>
  <profile>
    <id>traverse</id>
    <activation>
      <property>
        <name>traverse</name>
      </property>
    </activation>
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <configuration>
            <executable>java</executable>
            <arguments>
              <argument>-classpath</argument>
              <argument>org.dhappy.test.NeoTraverse</argument>
            </arguments>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

This is then executable as:

mvn exec:exec -Ptraverse
一身软味 2024-09-01 15:39:02

最好使用maven-antrun-plugin。它允许使用更灵活的方式调用您想要的内容。
如果您需要获取它(工具和所有必需的依赖项)出现在类路径中,只需将其添加为插件依赖项。在最终的 jar 构建过程中(如果您构建它)不会拾取此类依赖项。它们被添加到 maven.plugin.classpath 仅用于插件执行。
我记得我们在使用 maven-exec 插件时遇到了一些麻烦。它只能找到作为整个 pom 文件的依赖项添加的依赖项,并且不想使用插件的运行时依赖项(类似的东西)。
无论如何,别再说了,让我们看一下下面的例子:

<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
    <dependency>
        <groupId>x.y.z.codegen</groupId>
        <artifactId>Generator-Project</artifactId>
        <version>${generator.project.version}</version>
    </dependency>
</dependencies>
<executions>
    <execution>
        <id>Code generation</id>
        <phase>generate-sources</phase>
        <configuration>
            <target>
                <parallel threadsPerProcessor="2">
                    <java classname="x.y.z.codegen.ObjectModelCodeGenerator" fork="true" failonerror="true">
                        <classpath>
                            <path refid="maven.plugin.classpath"/>
                        </classpath>
                        <jvmarg value="-Duser.dir=${generator.root}"/>
                        <arg value="arg1"/>
                        <arg value="arg2"/>
                        <arg value="arg3"/>
                    </java>
                    <java classname="x.y.z.codegen.ObjectModelCodeGenerator" fork="true" failonerror="true">
                        <classpath>
                            <path refid="maven.plugin.classpath"/>
                        </classpath>
                        <jvmarg value="-Duser.dir=${generator.root}"/>
                        <arg value="arg1"/>
                        <arg value="arg2"/>
                        <arg value="arg3"/>
                    </java>
                </parallel>
            </target>
        </configuration>
        <goals>
            <goal>run</goal>
        </goals>
    </execution>
  </executions>

Ps 你可以看到我在这里使用了并行执行。
如果你的工具可以并行调用,这是可以完成的。
maven-antrun-plugin 1.8 存在这种可能性,但对于最近的版本来说这种可能性很多。意味着并行执行不会发生。

Better use maven-antrun-plugin. It allows to call what you want using more flexible way.
If you need to get it (tool and all required dependencies) appeared in class path just add it as plugin dependencies. Such dependencies will not be picked up during final jar building (if you build it). They are added to maven.plugin.classpath for plugin execution only.
I remember that we have some troubles with maven-exec plugin. It was able to find only dependencies added as dependencies for whole pom file and didn't want to use plugin's runtime dependencies (something like that).
Anyway stop talking let's go through the example below:

<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
    <dependency>
        <groupId>x.y.z.codegen</groupId>
        <artifactId>Generator-Project</artifactId>
        <version>${generator.project.version}</version>
    </dependency>
</dependencies>
<executions>
    <execution>
        <id>Code generation</id>
        <phase>generate-sources</phase>
        <configuration>
            <target>
                <parallel threadsPerProcessor="2">
                    <java classname="x.y.z.codegen.ObjectModelCodeGenerator" fork="true" failonerror="true">
                        <classpath>
                            <path refid="maven.plugin.classpath"/>
                        </classpath>
                        <jvmarg value="-Duser.dir=${generator.root}"/>
                        <arg value="arg1"/>
                        <arg value="arg2"/>
                        <arg value="arg3"/>
                    </java>
                    <java classname="x.y.z.codegen.ObjectModelCodeGenerator" fork="true" failonerror="true">
                        <classpath>
                            <path refid="maven.plugin.classpath"/>
                        </classpath>
                        <jvmarg value="-Duser.dir=${generator.root}"/>
                        <arg value="arg1"/>
                        <arg value="arg2"/>
                        <arg value="arg3"/>
                    </java>
                </parallel>
            </target>
        </configuration>
        <goals>
            <goal>run</goal>
        </goals>
    </execution>
  </executions>

P.s. You can see that I use parallel execution here.
It can be done if your tool can be called in parallel.
Such possibility exists for maven-antrun-plugin 1.8 but was lot for much recent versions. Means that parallel execution doesn't happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文