usage of maven enforcer plugin

前端 未结 4 635
猫巷女王i
猫巷女王i 2020-12-29 06:23

I\'d like to use the maven enforcer plugin to check to see if I have duplicate classes on my path. I\'ve tried the example from here.

But when I run it like so:

相关标签:
4条回答
  • 2020-12-29 06:44

    Try this, moving the configuration outside executions, so it isn't bound to the life cycle phase.

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0.1</version>
        <executions>
            <execution>
                <id>enforce-versions</id>
                <goals>
                    <goal>enforce</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <rules>
                <AlwaysPass />
            </rules>
            <fail>true</fail>
        </configuration>
    </plugin>
    

    Now when you do mvn enforcer:enforce, it picks the rules from your pom.xml.

    0 讨论(0)
  • 2020-12-29 06:46

    See these answers

    You can use the special default command line execution id, default-cli to invoke it (see Maven Docs), see my example below. This works at least with 3.1.1 and the article cited says it should work with 2.2.0+

    mvn enforcer:enforce
    

    However if you are using above Maven 3.1.1 (I can confirm it works in 3.3.3 with enforcer v 1.4.1) you can specify the execution id you wish using the new @ syntax (see Maven JIRA and the answers above);

    e.g. for the example below use

    mvn enforcer:enforce@dependency-convergence
    

    Here's a snippet from my pom;

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4.1</version>
                <executions>
                    <execution>
                        <id>dependency-convergence</id>
                        <phase>install</phase>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <DependencyConvergence />
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <DependencyConvergence/>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
          ...
    
    0 讨论(0)
  • 2020-12-29 06:55

    The reason why your first version did not work is because there is a difference between a plug-in configuration inside the execution tag and a plug-in configuration outside the execution tag. The execution is only used when your plug-in is triggered by a special phase of the complete Maven build.

    The Maven guide to configuration explains it better:

    Configurations inside the tag differ from those that are outside in that they cannot be used from a direct command line invocation. Instead they are only applied when the lifecycle phase they are bound to are invoked. Alternatively, if you move a configuration section outside of the executions section, it will apply globally to all invocations of the plugin.

    0 讨论(0)
  • I don't know why it won't work with the config being in an execution, but this worked for me:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <rules>
                        <banDuplicateClasses>
                            <findAllDuplicates>true</findAllDuplicates>
                        </banDuplicateClasses>
                    </rules>
                    <fail>false</fail>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>extra-enforcer-rules</artifactId>
                        <version>1.0-alpha-1</version>
                    </dependency>
                </dependencies>
            </plugin>
    
    0 讨论(0)
提交回复
热议问题