简体   繁体   English

Maven:如何排除或设置提供给由其他依赖项拖动的依赖项

[英]Maven: how to exclude or set provided to dependencies that dragged by other dependencies

I've problem that in large java project that is using maven to build it. 我有一个问题,在使用maven构建它的大型java项目中。
I've many dependencies that the main app is using like ( 40 ). 我有很多主要应用程序正在使用的依赖项,例如(40)。

The problem is that those dependencies are dragging more dependencies 问题是那些依赖关系拖累了更多的依赖关系
and some of them I can't and don't want to include in my final build 其中一些我不能也不想包含在我的最终版本中
this is dependencies hell!! 这是依赖地狱!

How do I exclude the specific dependencies in maven in my main app pom ? 如何在主应用程序pom中排除Maven中的特定依赖关系?
I don't want it in my app final deployment. 我不希望在我的应用程序最终部署中使用它。

For example if I set this in my main pom, it doesn't put all the dependencies of spring-boot-starter-tomcat that are dragged from other dependencies in provided , only the top level . 例如,如果我在主pom中设置了此属性,它不会将spring-boot-starter-tomcat的所有依赖项(从顶层提供的其他依赖项中)拖到提供的所有依赖项中。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

Maven brings in all the dependencies that a library requires and builds it for the runtime. Maven引入了库所需的所有依赖关系,并为运行时构建了依赖关系。 Most cases you would want this to happen as you dont want to go through each dependency of your dependencies and then their inner dependencies (its a hellish job to do, please don't do that). 大多数情况下,您会希望这种情况发生,因为您不想遍历依赖项的每个依赖项,然后再遍历它们的内部依赖项(这样做很麻烦,请不要这样做)。 But in some cases you might want to avoid bringing in few inner dependencies as you have the same at project level, for those cases maven lets you exclude them by <exclusions> . 但是在某些情况下,您可能希望避免引入一些内部依赖项,因为在项目级别具有相同的内部依赖项,对于这些情况,maven允许您通过<exclusions>排除它们。

The below sample is from maven official documentation: 以下示例来自Maven官方文档:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sample.ProjectA</groupId>
      <artifactId>Project-A</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  <!-- declare the exclusion here -->
          <groupId>sample.ProjectB</groupId>
          <artifactId>Project-B</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>

For deeper knowledge go here 如需更深入的知识,请点击此处

Hope this helps !! 希望这可以帮助 !!

If you set a dependency to scope provided , all the transitive dependencies below also become provided - unless the <dependencyManagement> overwrites the versions/scopes of the dependencies. 如果将依赖项设置为provided范围,则下面的所有传递性依赖项也将变为provided -除非<dependencyManagement>覆盖<dependencyManagement>的版本/范围。 Have a look at mvn dependency:tree to figure out. 看一下mvn dependency:tree找出来。

The command mvn dependency:list will list you exactly what Maven considers a dependency of your project. 命令mvn dependency:list将准确列出Maven认为项目的依赖项。 In your final artifact, you can either set all the unwanted dependencies as provided or figure out below which artifact they are included and write exclude statements as @vizsatiz said. 在您的最终工件中,您可以按provided方式设置所有不需要的依赖关系,也可以在下面找到它们所包含的工件,并按照@vizsatiz的说明编写排除语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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