在 Maven2 中,要排除单个传递依赖项,我必须执行以下操作:
<dependency>
<groupId>sample.group</groupId>
<artifactId>sample-artifactB</artifactId>
<version>1</version>
<exclusions>
<exclusion>
<groupId>sample.group</groupId>
<artifactId>sample-artifactAB</artifactId>
</exclusion>
</exclusions>
</dependency>
这种方法的问题在于,我必须为 sample-artifactB
贡献的每个传递依赖项执行此操作。
有没有办法使用某种通配符来一次排除所有传递依赖项而不是一个接一个?
对我有用的东西(可能是 Maven 的一个新特性)只是在排除元素中使用通配符。
我有一个多模块项目,其中包含在两个 WAR 打包模块中引用的“应用程序”模块。其中一个 WAR 打包的模块实际上只需要域类(我还没有将它们从应用程序模块中分离出来)。我发现这个工作:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>app</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
groupId 和 artifactId 上的通配符排除了所有通常会传播到使用此依赖项的模块的依赖项。
对于 maven2,没有办法按照您的描述进行操作。对于 Maven 3,有。如果您使用的是 maven 3,请参阅 another answer for this question
对于 maven 2,我建议为具有
我发现有用的一件事:
如果您将带有排除项的依赖项放在项目的父 POM 的 dependencyManagement 部分中,或者放在可导入的依赖项管理 POM 中,则无需重复排除项(或版本)。
例如,如果您的父 POM 具有:
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
....
</dependencies>
</dependencyManagement>
然后您项目中的模块可以简单地将依赖项声明为:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
父 POM 中的 将指定版本和排除项。我几乎在我们所有的项目中都使用了这种技术,它消除了很多重复。
Three years ago 我建议使用版本 99 不存在,但现在我想出了一个更好的方法,特别是因为版本 99 已离线:
在项目的父 POM 中,如果不需要的依赖项蔓延到构建中,请使用 maven-enforcer-plugin 使构建失败。这可以使用插件的 banned dependencies 规则来完成:
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>only-junit-dep-is-used</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>
然后,当它提醒您有关不需要的依赖项时,将其排除在父 POM 的 <dependencyManagement>
部分中:
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>2.1.8.RELEASE</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
这样,不需要的依赖项就不会意外出现(不像 <exclusion>
那样容易忘记),即使在编译时也不可用(不像 provided
范围),没有虚假的依赖项(不像版本 99),它可以在没有自定义存储库的情况下工作(与版本 99 不同)。这种方法甚至可以根据工件的版本、分类器、范围或整个 groupId - see the documentation 了解详细信息。
<configuration>
,必须直接在 <plugin>
下向上移动。
<dependencyManagement>
部分排除了依赖项。在该特定项目中运行 mvn dependency:tree
将完全没有排除的依赖项。但是所有导入该依赖项的项目都不会遵守来自另一个项目父 pom 的 <exclusions>
- 被排除的项目将潜入!!!我不得不将 <exclusions>
直接移动到每个模块 pom。
我使用以下解决方法:我没有尝试在所有适当的依赖项中排除工件,而是在顶层将依赖项绘制为“提供”。例如,为了避免发送 xml-apis "whatever version" :
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>[1.0,]</version>
<scope>provided</scope>
</dependency>
有一个解决方法,如果将依赖项的范围设置为运行时,将排除传递依赖项。尽管请注意,这意味着如果要打包运行时依赖项,则需要添加额外的处理。
要在任何打包中包含运行时依赖项,您可以使用 maven-dependency-plugin 的 copy goal for a specific artifact。
<scope>provided</scope>
而不是 <scope>runtime</scope>
。
如果您需要从要包含在程序集中的依赖项工件中排除所有传递依赖项,则可以在程序集插件的描述符中指定:
<assembly>
<id>myApp</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useTransitiveDependencies>false</useTransitiveDependencies>
<includes><include>*:struts2-spring-plugin:jar:2.1.6</include></includes>
</dependencySet>
</dependencySets>
</assembly>
如果您在 Eclipse 下开发,您可以在 POM 编辑器(启用高级选项卡)依赖关系图中查找要从项目中排除的依赖关系,然后:
右键单击它->“排除 Maven Artifact ...”,Eclipse 将为您进行排除,而无需找出 lib 链接的依赖项。
你排除所有传递依赖的原因是什么?
如果您需要从每个依赖项中排除特定的工件(例如 commons-logging),则 Version 99 Does Not Exist 方法可能会有所帮助。
2012 年更新:不要使用这种方法。使用 maven-enforcer-plugin and exclusions。版本 99 产生虚假的依赖关系,版本 99 存储库处于脱机状态(有 similar mirrors,但您也不能依赖它们永远在线;最好只使用 Maven Central)。
在一个类似的问题中,我使用提供的范围声明了所需的依赖项。使用这种方法,可以获取传递依赖项,但不包含在包阶段,这是您想要的。在维护方面我也喜欢这个解决方案,因为不需要维护 pom 或 whaley 解决方案中的自定义 pom;你只需要在容器中提供具体的依赖就可以了
在您的类路径中使用最新的 maven .. 它将删除重复的工件并保留最新的 maven 工件..