Java 在多模块反应器项目的子模块中从命令行运行特定的 Maven 插件目标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3459928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running a specific Maven plugin goal from the command line in a sub-module of a multi-module reactor project
提问by Brian Ferris
I'm looking for a general technique here, but let's give a specific example. I have a multi-module project and I'd like to run the exec:java
goal from the command-line against one of the sub-modules of my project.
我在这里寻找通用技术,但让我们举一个具体的例子。我有一个多模块项目,我想exec:java
从命令行针对我项目的子模块之一运行目标。
I know one approach is that I can run mvn install
on the whole project and then just go into the sub-module directory, run the exec:java
command from the command line, and have artifacts resolved to my local repository.?But running mvn install
all the time gets pretty tedious.
我知道一种方法是我可以mvn install
在整个项目上运行,然后进入子模块目录,从命令行运行exec:java
命令,并将工件解析到我的本地存储库。?但是一直运行mvn install
变得非常乏味.
What I'd really like is the ability to run exec:java
against the Maven reactor, where the classpath is constructed from the active modules of the project in the Maven reactor. The problem is that I'm not sure this is possible.?A naive approach is to run the exec:java
goal from the root of the project, but this tries to run the plugin against every module in the project, as opposed to the target module I'm interested in.
我真正喜欢的是exec:java
针对 Maven 反应器运行的能力,其中类路径是从 Maven 反应器中项目的活动模块构建的。问题是我不确定这是可能的。?一种幼稚的方法是exec:java
从项目的根目录运行目标,但这会尝试针对项目中的每个模块运行插件,而不是我的目标模块我感兴趣。
Any idea? I know my motivating example was exec:java
, but really there are a number of single plugin goals that I'd like to run against my project from time to time outside of the scope of the full build lifecycle.
任何的想法?我知道我的激励示例是exec:java
,但实际上我希望在整个构建生命周期范围之外不时针对我的项目运行许多单个插件目标。
回答by Pascal Thivent
I have a multi-module project and I'd like to run the
exec:java
plugin from the command-line against one of the sub-modules of my project.
我有一个多模块项目,我想
exec:java
从命令行针对我的项目的子模块之一运行插件。
I'm not saying that this will fit your exact use case but it is possible to run a goal on a subset of a multi-modules build using the -pl, --projects <arg>
option:
我并不是说这将适合您的确切用例,但可以使用以下-pl, --projects <arg>
选项在多模块构建的子集上运行目标:
mvn exec:java -pl my-module
I know one approach is that I can run "mvn install" on the whole project and then just go into the sub-module directory, run the exec:java command from the command line, and have artifacts resolved to my local repository.
我知道一种方法是我可以在整个项目上运行“mvn install”,然后进入子模块目录,从命令行运行 exec:java 命令,并将工件解析到我的本地存储库。
Dependency resolution is indeed done through the local repository.
依赖解析确实是通过本地存储库完成的。
What I'd really like is the ability to run
exec:java
against the Maven reactor, where the classpath is constructed from the active modules of the project in the Maven reactor.
我真正喜欢的是
exec:java
针对 Maven 反应器运行的能力,其中类路径是从 Maven 反应器中项目的活动模块构建的。
That's not really what a reactor build does. A reactor build constructs an oriented graph of the modules, derives an appropriate build order from this graph and runs the goal / phase against the modules in the calculated order. A reactor build doesn't construct some "global" classpath.
这不是反应堆构建的真正作用。反应器构建构建模块的定向图,从该图中导出适当的构建顺序,并按照计算出的顺序针对模块运行目标/阶段。反应堆构建不会构建一些“全局”类路径。
A naive approach is to run the
exec:java
goal from the root of the project, but this tries to run the plugin against every module in the project, as opposed to the target module I'm interested in.
一种天真的方法是
exec:java
从项目的根目录运行目标,但这会尝试针对项目中的每个模块运行插件,而不是我感兴趣的目标模块。
Well, this is the expected behavior. It just doesn't seem to be what you're actually looking for.
嗯,这是预期的行为。它似乎不是您真正要寻找的东西。
Any idea? I know my motivating example was exec:java, but really there are a number of single plugin goals that I'd like to run against my project from time to time outside of the scope of the full build lifecycle
任何的想法?我知道我的激励示例是 exec:java,但确实有许多单一的插件目标,我想不时地在完整构建生命周期的范围之外针对我的项目运行
A reactor build does allow this but, as I wrote, you seem to be looking for something different. Maybe If you clarify your exact need, I would be able to provide a better answer.
反应堆构建确实允许这样做,但是,正如我所写的,您似乎正在寻找不同的东西。也许如果您澄清您的确切需求,我将能够提供更好的答案。
回答by samskivert
A somewhat general technique that I've used in this circumstance is to define a profile in the submodule POM in question that binds exec:java to the test phase. For example:
我在这种情况下使用的一种有点通用的技术是在有问题的子模块 POM 中定义一个配置文件,将 exec:java 绑定到测试阶段。例如:
<profiles>
<profile>
<id>test-java</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.foo.bar.MyClass</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then from the top of your project, run:
然后从项目的顶部运行:
mvn test -Ptest-java
This will set up the inter-module classpath as usual, and attempt to run the test-java profile in all of your subprojects. But since only the one you care about has the profile defined, it's the only one that will do anything.
这将像往常一样设置模块间类路径,并尝试在所有子项目中运行 test-java 配置文件。但是由于只有您关心的人定义了配置文件,因此它是唯一可以做任何事情的人。
It does take a wee bit of extra time for Maven to grind through your other subprojects NOOPing, but it's not so bad.
Maven 确实需要一点额外的时间来磨练您的其他子项目 NOOPing,但这还不错。
One thing to note is that the subproject is run with the top-level directory as the current working directory (not the subproject directory). There's not much you can do to work around that, but hopefully that won't cause you trouble.
需要注意的一件事是子项目以顶级目录作为当前工作目录(而不是子项目目录)运行。您无能为力来解决这个问题,但希望这不会给您带来麻烦。
回答by Jesse Glick
Pascal's suggestion is probably what you want. Note that it is not currently possible to first compile the dependencies, then run (exec:exec
etc.) the app, in a single Maven command: https://jira.codehaus.org/browse/MNG-5059
帕斯卡的建议可能就是你想要的。请注意,目前无法exec:exec
在单个 Maven 命令中先编译依赖项,然后运行(等)应用程序:https: //jira.codehaus.org/browse/MNG-5059
回答by kapex
There is another way which lets you choose multiple modules to execute a plugin.
还有另一种方法可以让您选择多个模块来执行插件。
Many plugins have a skip
option, which you can activate on the root project by setting its value to true
. The plugin execution will be skipped by default for all sub-modules then. Sub-modules that should execute the plugin can explicitly set skip
to false
. You still need to configure any non-optional attributes in the root project.
许多插件都有一个skip
选项,您可以通过将其值设置为true
. 默认情况下,所有子模块的插件执行将被跳过。应该执行插件的子模块可以显式设置skip
为false
. 您仍然需要在根项目中配置任何非可选属性。
Example of the exec-maven-plugin
with configuration for the exec:exec
goal:
目标的exec-maven-plugin
with 配置示例exec:exec
:
<!-- root project -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<skip>true</skip>
<executable>java</executable>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- any module that should execute the plugin -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<!-- ... -->
</configuration>
</plugin>
</plugins>
</build>