Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing overriding dependency version in Maven child pom

Tags:

maven

maven-3

I have a dependencyManagement section in parent pom like

<dependencyManagement>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
    </dependency>
</dependencyManagement>

and a child pom, having it

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.0</version>
    </dependency>
</dependencies>

I've tried to prevent this kind of overriding in child poms using enforcer plugin, allowing these only to be set in parent, but haven't been able to. I'd like this to fail the build. Is that possible, with that plugin or some other way?

There is DependencyCovergence, which forces all versions to be the same, but that's too restrictive as I don't want to control all transitive dependencies - just the ones defined explicitly.

I'd be happy if I could just prevent introducing any new dependencies at all in child poms - everything defined should really be defined in the parent pom, and then just mentioned, if needed, in the child.

like image 649
eis Avatar asked Jan 25 '13 11:01

eis


People also ask

Does Maven override dependency version?

By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

What happens if we don't specify version in Pom?

Each maven dependency defined in the pom must have a version either directly or indirectly for example, through dependencyManagement or parent. That being said, if the version is not given, then the version provided in the dependencyManagement or the parent pom will be used.

What happens if you don't specify a version in Maven?

Maven won't allow any other either. Build will fail if version is not found.


1 Answers

You could add a dependency:analyze-dep-mgt execution in your parent pom and configure it to fail on version mismatches:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>analyze</id>
            <phase>package</phase>
            <goals>
              <goal>analyze-dep-mgt</goal>
            </goals>
            <configuration>
              <failBuild>true</failBuild>
              <ignoreDirect>false</ignoreDirect>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
like image 103
FrVaBe Avatar answered Nov 16 '22 00:11

FrVaBe