Maven:从命令行执行,并在配置中多次执行 [英] Maven: execution from command line and multiple executions in config

查看:146
本文介绍了Maven:从命令行执行,并在配置中多次执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行执行插件目标,但是要多次执行插件.为此,我的POM如下所示:

I would like to execute a plugin goal from command line but perform multiple executions of the plugin. To this end my POM looks like this:

<plugin>
    <groupId>xxx.yyy</groupId>
    <artifactId>zzz</artifactId>
    <version>1.1.6</version>
    <executions>
        <execution>
            <id>default-cli-1</id>
            <goals>
                <goal>mygoal</goal>
            </goals>
            <configuration>
                .... config1 ....
            </configuration>
        </execution>
        <execution>
            <id>default-cli-2</id>
            <goals>
                <goal>mygoal</goal>
            </goals>
            <configuration>
                .... config2 ....
            </configuration>
        </execution>
    </executions>
</plugin>

我想做的事情是这样的:

What I would like to do is something like:

mvn xxx.yyy.zzz:mygoal

,然后将执行两次执行.但是我不知道怎么办.

and that would then execute the two executions. But I cannot figure out how.

我知道从命令行执行时不能使用<id>.这就是default-cli的用途.但是<id><executions>中必须是唯一的,这意味着我只能将default-cli放在一个execution上.

I'm aware that I cannot use an <id> when executing from the command line. That is what the default-cli is for. However the <id> must be unique within <executions> which means I can only put the default-cli on one execution.

Maven版本 3.0.5 .

推荐答案

您可以从Maven 3.3.1 以及@executionId附加选项,https://issues.apache.org/jira/browse/MNG-5768"rel =" nofollow noreferrer>此新功能.

You can execute a goal (and its execution) from command line starting from Maven 3.3.1 on and this new feature, via the @executionId additional option.

关于Maven和执行ID的生成,您还可以检查

Concerning Maven and execution ids generation, you can also check this SO question.

在Maven 3.3.1 之前,您可以将两个执行绑定到通常不会造成伤害的阶段(例如validate),并具有以下内容:

Before Maven 3.3.1 you could instead bind the two executions to a phase which would normally not harm (like validate) and have something like the following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>execution-1</id>
            <phase>validate</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>something1</classifier>
            </configuration>
        </execution>
        <execution>
            <id>execution-2</id>
            <phase>validate</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>something2</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

然后执行:

mvn validate

作为无害阶段的一部分,您将有效地执行同一插件的相同目标的两次执行.

You will effectively execute the two executions of the same goal of the same plugin, as part of an harmless phase.

如果您不想默认将它们作为此阶段的一部分(可以理解),则可以将它们移至配置文件并在执行过程中将其激活:

If you don't want to have them as part of this phase by default (understandable), then you can move them to a profile and activate it as part of the execution:

mvn validate -PpluginGoalExecution

为完整起见,个人资料应如下所示:

For completeness, the profile would look like:

<profile>
    <id>pluginExecution</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>execution1</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>something1</classifier>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution2</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>something2</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

不用说:在这种情况下,配置文件的ID应该完全说明了它实际执行的插件和目标(即,配置文件的目的,照常).

And it goes without saying: the id of the profile should in this case be quite self explanatory about which plugin and which goal it would actually execute (that is, the purpose of the profile, as usual).

更新
仅仅是装饰性的,但您也可以在元素上方添加到概要分析的构建中:

Update
Just cosmetic, but you could also add to the profiled build above the element:

<defaultGoal>validate</defaultGoal>

因此您只需要运行以下Maven命令(仅配置文件激活):

So that you would only need to run the following Maven command (only profile activation):

mvn -PpluginGoalExecution

然后它将自动执行验证阶段和配置的插件执行.改变不大(就像我说的那样),但是可能更接近插件目标执行,而不是Maven阶段调用(再次只是外观).

And it would then automatically execute the validate phase and the configured plugin executions. Not a big change (as I said, cosmetic), but maybe closer to a plugin goal execution rather than a Maven phase invocation (again, just appearance).

这篇关于Maven:从命令行执行,并在配置中多次执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆