pbr*_*ult 211 dependencies maven-2
在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>
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于我必须为每个由此贡献的传递依赖项执行此操作sample-artifactB
.
有没有办法使用某种通配符一次排除所有传递依赖,而不是一个一个?
小智 289
对我有用的东西(可能是Maven的新功能)仅仅是在排除元素中做通配符.
我有一个多模块项目,其中包含一个"app"模块,该模块在两个WAR打包的模块中引用.其中一个WAR打包的模块实际上只需要域类(我还没有将它们从app模块中分离出来).我发现这个工作:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>app</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
groupId和artifactId上的通配符排除了通常使用此依赖关系传播到模块的所有依赖项.
Jos*_*vis 30
我觉得有用的一件事:
如果将依赖项与依赖项放在项目的父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>
Run Code Online (Sandbox Code Playgroud)
然后,项目中的模块可以简单地将依赖关系声明为:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
父POM中将指定版本和排除项.我几乎在所有项目中使用这种技术,它消除了大量的重复.
Esk*_*ola 22
三年前我建议使用版本99不存在,但现在我想出了一个更好的方法,特别是因为版本99离线:
在项目的父POM中,如果不需要的依赖项进入构建,则使用maven-enforcer-plugin使构建失败.这可以使用插件的禁止依赖关系规则来完成:
<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>
Run Code Online (Sandbox Code Playgroud)
然后,当它提醒您有关不需要的依赖项时,请在父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>
Run Code Online (Sandbox Code Playgroud)
这样就不会意外地显示不需要的依赖(不像只是<exclusion>
容易忘记的那样),即使在编译时也不可用(与provided
范围不同),没有伪造的依赖(不像版本99)而且它'没有自定义存储库(与版本99不同).这种方法甚至可以基于工件的版本,分类器,范围或整个groupId工作 - 有关详细信息,请参阅文档.
Mic*_*ann 10
我使用以下解决方法:而不是尝试在所有适当的依赖项中排除工件,我将依赖项绘制为顶层的"提供".例如,为了避免运送xml-apis"任何版本":
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>[1.0,]</version>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
有一种解决方法,如果将依赖关系的范围设置为运行时,将排除传递依赖关系.虽然请注意,这意味着如果要打包运行时依赖项,则需要添加其他处理.
要在任何打包中包含运行时依赖项,可以将maven-dependency-plugin的复制目标用于特定工件.
如果需要从要包含在程序集中的依赖项工件中排除所有传递依赖项,可以在程序集插件的描述符中指定:
<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>
Run Code Online (Sandbox Code Playgroud)