使用特定配置文件时禁用maven插件

jrh*_*ath 32 maven

我想找到一种方法,如果使用特定的配置文件运行,禁用插件执行.

如果选择了配置文件,则运行插件相反.

我的用例:我的Maven构建有一大堆插件,但是在我的开发机器上运行时,我想跳过它们中的一些.我希望能够使用"dev"配置文件运行构建,而不是在本地评论这些插件.插件将继续在我的连续构建上运行.

想法?

Krz*_*iek 68

当特定配置文件处于活动状态时,有一种简洁的方法可以禁用插件执行.

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

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您需要定义一个不会执行此插件的配置文件:

<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>
Run Code Online (Sandbox Code Playgroud)

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

mvn clean package
Run Code Online (Sandbox Code Playgroud)

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

mvn clean package -P no-sass
Run Code Online (Sandbox Code Playgroud)

sass-maven-plugin 不会被执行.

  • 我想扩展这个答案。值得一提的是:1)使用&lt;phase&gt;none&lt;/phase&gt;,您可以禁用整个插件,而是禁用具体的&lt;execution&gt;,2)可以使用像&lt;phase&gt;${enablePhase}这样的变量&lt;/phase&gt; 并在不同的配置文件中设置或不设置此变量。这样就可以有条件地运行某个执行。 (3认同)

Abs*_*ind 17

  • 定义你的pom,这样只有dev模式下你需要的插件
  • 定义开发配置文件
  • 定义包含您想要/需要的所有插件的生产配置文件
  • 将生产配置文件定义为默认值

示例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>
Run Code Online (Sandbox Code Playgroud)

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

mvn -Pdev compile
Run Code Online (Sandbox Code Playgroud)

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

mvn compile
Run Code Online (Sandbox Code Playgroud)

如果你不希望/需要在你的dev的配置文件来定义什么特别的东西,你可以忽略它的声明,并打电话给你的开发模式是这样的(!禁用配置文件):

mvn -P!production compile
Run Code Online (Sandbox Code Playgroud)

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

mvn -P\!production compile
Run Code Online (Sandbox Code Playgroud)


gog*_*tad 6

基于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 ---
....
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)