【问题标题】:Disable maven plugins when using a specific profile使用特定配置文件时禁用 Maven 插件
【发布时间】:2013-01-06 18:11:09
【问题描述】:

如果使用特定配置文件运行,我正在寻找一种禁用插件执行的方法。

这与running a plugin if a profile is selected相反。

我的用例:我的 Maven 构建包含大量插件,但在我的开发机器上运行时,我想跳过其中的一些。我不想在本地评论这些插件,而是希望能够使用“dev”配置文件运行构建。这些插件将继续在我的持续构建中运行。

想法?

【问题讨论】:

  • 在所有插件中都有对 元素的功能请求。 jira.codehaus.org/browse/MNG-3102
  • 这将涉及所有必须支持此功能的插件。我想更多的是<plugingroup> <run-condition> <notprofile>dev<notprofile> </run-contidion> <plugin..> <plugin..> </plugingroup>
  • 您能否详细说明您想要实现的目标?某种包装?测试等?要不然是啥?也许你可以提供你的 pom 文件的摘录?
  • 残酷的细节:我想设置它,以便当我的 maven 构建在启用“风险”配置文件的情况下运行时,我希望 pmd、checkstyle、findbugs、surefire 和故障安全插件不运行。跨度>

标签: maven


【解决方案1】:

有一种巧妙的方法可以在特定配置文件处于活动状态时禁用插件执行。

首先,您需要为插件执行添加一个标识符,例如:

<build>
    <plugins>
        <!-- (...) -->
        <plugin>
            <groupId>nl.geodienstencentrum.maven</groupId>
            <artifactId>sass-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>styles-compilation</id> <!-- plugin execution identifier -->
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>update-stylesheets</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

然后你需要定义一个不会执行这个插件的配置文件:

<profiles>
    <profile>
        <id>no-sass</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>nl.geodienstencentrum.maven</groupId>
                    <artifactId>sass-maven-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <id>styles-compilation</id> <!-- here there must be the same identifier as defined in <build><plugins> section -->
                            <phase>none</phase> <!-- this disables plugin -->
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

现在,如果您运行标准 maven 构建:

mvn clean package

sass-maven-plugin 将被执行,但在运行时:

mvn clean package -P no-sass

sass-maven-plugin 不会被执行

【讨论】:

  • 很好的答案。这甚至允许从生命周期中删除标准插件,例如maven-compiler-plugin
  • 我想扩展这个答案。值得一提的是:1)使用 none 您可以禁用整个插件,但可以禁用具体的 ,2)可以使用像 ${enablePhase} 这样的变量 并在不同的配置文件中设置或不设置此变量。这样就可以有条件地运行某个执行。
【解决方案2】:
  • 定义您的 pom,使其仅包含您在开发模式下需要的插件
  • 定义开发配置文件
  • 定义包含您想要/需要的所有插件的生产配置文件
  • 将生产配置文件定义为默认配置

示例 pom:

<profiles>
  <profile>
    <id>production</id>

    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>

    <build>
      <plugins>
        <!-- 
        <plugin>
          ...
        </plugin>
        -->
      </plugins>
    </build>
    </profile>

    <profile>
      <id>dev</id>
      <!-- Some other logic here, if necessary.
           Otherwise, there's no need for another profile. -->
    </profile>
</profiles>

要在开发模式下运行,您可以调用以下命令:

mvn -Pdev compile

要在生产模式下运行,只需使用正常步骤:

mvn compile

如果您不想/不需要在您的开发配置文件中定义任何特殊内容,您可以省略其声明并像这样调用您的 开发模式! 禁用配置文件):

mvn -P!production compile

注意:您可能需要转义感叹号,因为它是 bash 中的特殊字符:

mvn -P\!production compile

【讨论】:

  • 请添加以下步骤: 1) 要使用插件运行项目,只需照常构建即可; 2) 要在没有插件的情况下运行项目,请执行mvn clean package ... -P !production。注意:dev 配置文件不是必需的,如果它不包含任何特定内容。 -P !profileName 应该足以排除配置文件。
  • @carlspring 你能帮我解释一下为什么会出现这个错误吗? $ mvn -P !production clean -bash: !production: event not found
  • 你是对的,我的错!我几乎可以发誓我以前也这样做过。 :)
  • 您可以禁用历史记录替换:set +H
  • 是否可以创建插件定义并从配置文件中“调用”它,以便我们可以重用一些 xml 定义?
【解决方案3】:

基于 Krzysiek 的回答,您无需定义显式执行,只需查看 maven 为您提供的输出并禁用默认执行即可。

例如,给定 maven 的以下输出:

[INFO] --- maven-resources-plugin:2.7:copy-resources (prepare-dockerfile) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
...
[INFO]
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
....
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ tilbud ---
...
[INFO]
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ tilbud ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
...
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ tilbud ---
....

生成的默认执行名称列在插件和目标后面的括号中。以下配置文件禁用了上述插件:

<profiles>
    <profile>
        <id>packageOnly</id>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-compile</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>default-testCompile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-test</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>default-resources</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>default-testResources</id>
                                <phase>none</phase>
                            </execution>
                            <execution>
                                <id>prepare-dockerfile</id>
                                <phase>none</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

【讨论】:

  • 应该提到他只使用生命周期的默认插件,因为它们的绑定是在default-bindings.xml 中声明的,它们的default-... ID 是由 Maven 动态创建的。在 POM 中声明的 &lt;build&gt;/&lt;plugins&gt;/&lt;plugin&gt; 不能被 &lt;profile&gt;/&lt;build&gt;/&lt;pluginManagement&gt;/&lt;plugins&gt;/&lt;plugin&gt; 覆盖,因为它们以相反的方式工作:(专用)&lt;plugin&gt; 覆盖(一般)&lt;pluginManagement&gt;
猜你喜欢
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2015-11-04
  • 2012-04-15
  • 2016-10-23
相关资源
最近更新 更多