maven - 在配置文件中运行插件的目标

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

    <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] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project ando: The parameters 'executable' for goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec are missing or invalid -> [Help 1]



此外,错误输出表明版本 1.3.2 ,但我正在使用 1.4.0 .

最佳答案

调用 exec 时出错exec-maven-plugin的目标因为它的配置(你的意思)是 profile 的一部分默认情况下它不处于事件状态,并且不会被您的执行激活 mvn exec:exec .

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

mvn exec:exec -Pintegration-tests

或者
mvn exec:exec -Dintegrations=true

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

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

有一个difference configuration之间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

笔记:
  • 我再次激活配置文件以将执行作为构建的一部分
  • 这次我没有通过插件alias , exec ,以及期望的目标,exec , 但它的完整 Maven GAV (groupId, artifactId, version) 在通用 Maven 模式中用于依赖项( : 分隔),所以我们得到 org.codehaus.mojo:exec-maven-plugin:1.4.0 ,然后通过目标,所以我们得到额外的 :exec ,然后使用上面提到的新功能和新的 @运算符我传递所需的执行 ID(在 POM 中定义)。
  • 关于maven - 在配置文件中运行插件的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980715/

    相关文章:

    java - 为什么 pom.xml 要求依赖版本?

    java - 在 jar 旁边使用附加文件在 maven 中创建 zip

    java - Maven 插件来操作现有的 jar

    java - JSF Web 项目 <h :xxx> tag invisible

    java - 是否可以在父 pom 文件中包含 <packaging>jar</packaging> ?

    java - 从 Java 向 Jenkins 传递值

    java - 为什么 Maven 每次都下载 maven-metadata.xml?

    java - Maven适合Java小项目吗?

    java - 将 @Generated 注释添加到 JAXB 生成的类

    java - Maven:如何将本地jar打包成最终jar?