Disable maven plugins when using a specific profile

后端 未结 3 1521
北海茫月
北海茫月 2020-12-08 02:17

I\'m looking to find a way of disabling a plugin execution if running with a particular profile.

This is the opposite of running a plugin if a profile is selected.

相关标签:
3条回答
  • 2020-12-08 02:36

    There is a neat way to disable plugin execution when specific profile is active.

    Firstly you need to add an identifier to plugin execution like:

    <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>
    

    Then you need to define a profile in which this plugin will NOT be executed:

    <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>
    

    Now if you run standard maven build:

    mvn clean package
    

    the sass-maven-plugin will be executed, yet when running:

    mvn clean package -P no-sass
    

    the sass-maven-plugin will not be executed.

    0 讨论(0)
  • 2020-12-08 02:37

    Building on Krzysiek's answer, you don't need to define explicit executions, just have a look at the output maven gives you and disable the default executions.

    For instance, given the following output from 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 ---
    ....
    

    The generated default execution names is listed in parenthesis after the plugin and goal. The following profile disables the plugins above:

    <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>
    
    0 讨论(0)
  • 2020-12-08 02:48
    • Define your pom so that is has only the plugins you need in dev mode
    • Define a dev profile
    • Define a production profile which contains all plugins you want/need
    • Define the production profile as default

    example 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>
    

    To run in Dev Mode you can call the following:

    mvn -Pdev compile
    

    To run in Production Mode just use the normal steps:

    mvn compile
    

    In case you don't want/need to define anything special in your dev profile, you can omit its declaration and call your Dev Mode like this (! disables a profile):

    mvn -P!production compile
    

    Be aware: you may need to escape the exclamation mark since it is a special character in bash:

    mvn -P\!production compile
    
    0 讨论(0)
提交回复
热议问题