简体   繁体   中英

How can a maven build fail when certain dependencies are present?

In one of my POM's a dependency had a wrong scope. While running the application, this (test) dependency was the cause of an OutOfMemoryError.

I would like to prevent this by adding a plugin that iterates over all (including transitive) dependencies and verify the occurrence of a certain pattern, like an artifactId containing "test".

How can I filter all dependencies and fail the build when a certain pattern appears?

After dan1st answer, this solution fits my current needs:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
        <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <bannedDependencies>
                        <excludes>
                            <exclude>*:*-test</exclude>
                            <exclude>*:test-*</exclude>
                        </excludes>
                    </bannedDependencies>
                </rules>
                <fail>true</fail>
            </configuration>
        </execution>
    </executions>
</plugin>

The maven enforcer plugin allows to ban dependencies :

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <bannedDependencies>
                <searchTransitive>true</searchTransitive>
                <message>Dependency not allowed!</message>
                  <excludes>
                    <!-- do one of the following: --> 
                    <exclude>all.dependencies.with.this.group.id.are.banned</exclude>
                    <exclude>group.id-to-ban:artifact-id-of-artifact-to-ban</exclude>
                    <exclude>group.id-to-ban:artifact-id-of-artifact-to-ban:versiontoban</exclude>
                    <!-- wildcards with '*' are also allowed -->
                  
                </bannedDependencies>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

You can customize maven-enforcer to include your own rule which implements your specific use case (text pattern match). For example take a look here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM