Java 当提供多个依赖项时,如何仅复制未提供的依赖项?
在我们的项目设置中,我们在运行时有一个spring boot应用程序,我们通过。这些JAR都位于实际的spring boot应用程序旁边的Java 当提供多个依赖项时,如何仅复制未提供的依赖项?,java,maven,dependencies,dependency-management,Java,Maven,Dependencies,Dependency Management,在我们的项目设置中,我们在运行时有一个spring boot应用程序,我们通过。这些JAR都位于实际的spring boot应用程序旁边的lib文件夹中。在开发过程中,spring boot应用程序本身提供给我们,我们只开发额外的JAR 因此,我们有许多依赖项是通过spring boot应用程序提供的,如spring context等。但例如,在一个jar中,我们依赖于spring webflux,而spring boot应用程序本身并没有提供这些依赖项。但是已经提供了SpringWebFlux
lib
文件夹中。在开发过程中,spring boot应用程序本身提供给我们,我们只开发额外的JAR
因此,我们有许多依赖项是通过spring boot应用程序提供的,如spring context
等。但例如,在一个jar中,我们依赖于spring webflux
,而spring boot应用程序本身并没有提供这些依赖项。但是已经提供了SpringWebFlux的一些依赖项。因此,我们只想复制那些spring boot应用程序未提供的spring webflux
依赖项,以避免在我们的lib
文件夹中有已经打包在spring boot应用程序中的JAR
更一般地说:如何复制工件A的依赖项,排除工件B依赖的所有依赖项
我试过使用:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-to-lib</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>runtime</includeScope>
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
/a/pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>a</artifactId>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>b</artifactId>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>c</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>a</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>b</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-to-lib</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>runtime</includeScope>
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
/c/pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>a</artifactId>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>b</artifactId>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>c</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>a</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>b</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-to-lib</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<includeScope>runtime</includeScope>
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.example
父母亲
0.0.1-快照
C
org.example
A.
${project.version}
假如
org.example
B
${project.version}
运行时
maven依赖插件
将依赖项复制到库
过程资源
复制依赖项
${project.build.directory}/lib
运行时
假如
在上面的例子中,我的期望是
/c/target/lib
只包含b-0.0.1-SNAPSHOT.jar
和commons-lang-2.6.jar
,因为commons io
是由模块a
提供的。但是目录仍然包含commons io
commons io
也是模块b
的运行时依赖项,这就是它被包含的原因。作为初始问题的简单解决方案,您可以手动排除使用Maven excludes函数为您提供的weblux
的依赖项dependencies@Ivan手动排除依赖项是我们目前的解决方法。但这是一个挑战,因为依赖关系可能在每个版本都会发生变化,因此它需要很高的维护工作。你可以精心制作它来复制你想要的东西。如果您不想深入挖掘,您可以考虑构建一个BOM表,在其中管理所有相关工件,以提供@JFMeier您能解释一下BOM表在这里有什么帮助吗?事实上,spring boot应用程序提供的工件是在bom中管理的。但是我不明白如何说“复制所有依赖项,不包括此bom中指定的依赖项”。您可以使用bom来确保bom中的所有依赖项实际上都是提供的
,并且该范围不会被项目中的其他依赖项覆盖。这可能会使按作用域进行过滤更加健壮。commons io
也是模块b
的运行时依赖项,这就是为什么它被包括在内。作为初始问题的简单解决方案,您可以手动排除使用Maven excludes函数为您提供的weblux
的依赖项dependencies@Ivan手动排除依赖项是我们目前的解决方法。但这是一个挑战,因为依赖关系可能在每个版本都会发生变化,因此它需要很高的维护工作。你可以精心制作它来复制你想要的东西。如果您不想深入挖掘,您可以考虑构建一个BOM表,在其中管理所有相关工件,以提供@JFMeier您能解释一下BOM表在这里有什么帮助吗?事实上,spring boot应用程序提供的工件是在bom中管理的。但是我不明白如何说“复制所有依赖项,不包括此bom中指定的依赖项”。您可以使用bom来确保bom中的所有依赖项实际上都是提供的
,并且该范围不会被项目中的其他依赖项覆盖。这可能会使按范围进行过滤更加健壮。