我想让一个 Maven 目标触发一个 java 类的执行。我正在尝试使用以下行迁移 Makefile
:
neotest:
mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse"
我希望 mvn neotest
产生 make neotest
目前所做的事情。
exec plugin documentation 和 Maven Ant tasks 页面都没有任何直接的示例。
目前,我在:
<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>
不过,我不知道如何从命令行触发插件。
使用您为 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
目标,则需要将目标绑定到 default lifecycle 的特定阶段。为此,请在 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。
为了执行多个程序,我还需要一个 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
<argument>-classpath</argument><classpath />
行是怎么回事?我不认为这是正确的。
<classpath />
标记错误地到达那里,应该被删除。所以这条线看起来只是:<argument>-classpath</argument>
<classpath />
在哪里,有人删除了吗?
最好使用 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 存在这种可能性,但对于最近的版本来说很多。意味着不会发生并行执行。
exec:java
也可以用于 Scala 和 Clojure 代码,它本身不一定是 Java 代码。