Preventing overriding dependency version in Maven child pom

我与影子孤独终老i 提交于 2020-01-20 17:55:17

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/14521176/preventing-overriding-dependency-version-in-maven-child-pom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!