【问题标题】:Run a goal of a plugin inside a profile在配置文件中运行插件的目标
【发布时间】:2016-05-01 01:27:34
【问题描述】:
    <profile>
        <id>integration-tests</id>
        <activation>
            <property>
                <name>integrations</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <executions>
                        <execution>
                            <id>script-chmod</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>chmod</executable>
                                <arguments>
                                    <argument>+x</argument>
                                    <argument>integration-test-docker/integrations.sh</argument>
                                </arguments>
                            </configuration>
                        </execution>
                        <execution>
                            <id>script-exec</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>integration-test-docker/integrations.sh</executable>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

此配置文件中有更多内容,但我只包含了我想要运行的内容。我怎样才能执行这个特定插件的这个特定目标?

我试过mvn exec:exec,但我明白了

[ERROR] 未能执行目标 org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on 项目ando:目标的参数“可执行” org.codehaus.mojo:exec-maven-plugin:1.3.2:exec 丢失或无效 -> [帮助 1]

此外,错误输出指示版本1.3.2,但我使用的是1.4.0

【问题讨论】:

    标签: maven maven-3 pom.xml exec-maven-plugin maven-profiles


    【解决方案1】:

    您在调用exec-maven-pluginexec 目标时遇到错误,因为它的配置(您的意思)是profile 的一部分,默认情况下它是不活动的,并且它不会被您的执行激活mvn exec:exec.

    如果您尝试通过其属性激活(根据配置)或显式(通过-P 选项)激活配置文件,那么 Maven 将知道您提到了哪个 Exec Maven 插件:

    mvn exec:exec -Pintegration-tests
    

    mvn exec:exec -Dintegrations=true
    

    因此,配置文件将处于活动状态,并且您的 Exec Maven 插件配置将成为构建的一部分。

    但是,您正在配置插件的两次执行,并且没有全局配置,因此它也会再次失败。

    configuration 元素之间有一个difference plugin 部分在execution 子部分中或之外:out,它将默认应用于所有执行和命令行执行;在,它将应用于应用的特定执行(覆盖或与任何默认执行合并,如果提供)。

    如果您想同时运行两个执行,则只需激活配置文件即可:

    mvn clean verify -Pintegration-tests
    

    mvn clean verify -Dintegrations=true
    

    但是,它将执行配置文件的全部内容以及整个构建,直到指定的phase

    如果您只想从命令行执行插件目标(exec),那么您需要指定一个默认配置(但只有一个),如下所示:

    <profile>
        <id>integration-tests</id>
        <activation>
            <property>
                <name>integrations</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <configuration>
                        <!-- here your default configuration -->
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    

    默认配置将应用于命令行执行。

    或者,您还可以覆盖default execution id (default-cli),它由命令行中的每个插件执行使用,您可以将其视为问题中提到的错误的一部分。因此,您可以仅为该执行提供特定配置,而不是为所有其他(如果有)执行提供特定配置(假设它们不会提供自己的配置)。这是一个例子:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
            <execution>
                <id>default-cli</id>
                <configuration>
                    <!-- here your specific command line execution -->
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    如果您想从命令行执行这两个执行并且仅执行这些执行(因此,不作为 Maven 阶段的一部分),您可以查看主题上的this answer(简而言之:自从 Maven 3.3.1,否则为早期版本提供建议)。

    因此,执行如下:

    mvn org.codehaus.mojo:exec-maven-plugin:1.4.0:exec@script-chmod -Dintegrations=true
    

    注意:

    • 我再次激活配置文件以将执行作为构建的一部分
    • 这次我没有传递插件aliasexec 和期望的目标exec,而是在通用 Maven 模式中用于依赖项 (@ 987654345@分隔),所以我们得到org.codehaus.mojo:exec-maven-plugin:1.4.0,然后传递目标,所以我们得到额外的:exec,然后使用上面提到的新功能和新的@运算符我传递所需的执行id(定义在POM)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多