Maven伟大而成熟。 几乎所有事物都总有解决方案。 您可能在组织项目上遇到的主要情况是依赖管理。 而不是每个项目都没有自己的依赖关系,您需要一种集中化的方式来继承那些依赖关系。
在这种情况下,您可以在父舞会上声明托管依赖项。 在我的示例中,我只想包含Akka流依赖项。
<? xml version = "1.0" encoding = "UTF-8" ?> < 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 http://maven.apache.org/xsd/maven-4.0.0.xsd " > < modelVersion >4.0.0</ modelVersion > < groupId >org.example</ groupId > < artifactId >maven-dependency-management</ artifactId > < packaging >pom</ packaging > < version >1.0-SNAPSHOT</ version > < properties > < akka.version >2.5.31</ akka.version > < akka.http.version >10.1.11</ akka.http.version > < scala.binary.version >2.12</ scala.binary.version > </ properties > < modules > < module >child-one</ module > </ modules > < dependencyManagement > < dependencies > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-stream_2.12</ artifactId > < version >${akka.version}</ version > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http_2.12</ artifactId > < version >${akka.http.version}</ version > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http-spray-json_2.12</ artifactId > < version >${akka.http.version}</ version > </ dependency > </ dependencies > </ dependencyManagement > </ project >
我使用的是依赖性管理模块。
现在,子项目无需指定版本即可包含这些库。 派生和管理版本至关重要。 如果版本不兼容,可能会带来许多不愉快的惊喜。
现在,在子模块上,由于版本是子模块,因此声明了没有版本的版本。
<? xml version = "1.0" encoding = "UTF-8" ?> < 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 http://maven.apache.org/xsd/maven-4.0.0.xsd " > < parent > < artifactId >maven-dependency-management</ artifactId > < groupId >org.example</ groupId > < version >1.0-SNAPSHOT</ version > </ parent > < modelVersion >4.0.0</ modelVersion > < artifactId >child-one</ artifactId > < dependencies > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-stream_2.12</ artifactId > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http_2.12</ artifactId > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http-spray-json_2.12</ artifactId > </ dependency > </ dependencies > </ project >
另一个需要注意的是,有时我们想使用另一个项目的依赖项管理,而不必将该项目作为父项。 在这些情况下,当您已经具有父项目时,您需要包括来自父项目的依赖关系管理。
<? xml version = "1.0" encoding = "UTF-8" ?> < 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 http://maven.apache.org/xsd/maven-4.0.0.xsd " > < modelVersion >4.0.0</ modelVersion > < groupId >org.example</ groupId > < artifactId >independent-project</ artifactId > < version >1.0-SNAPSHOT</ version > < dependencyManagement > < dependencies > < dependency > < artifactId >maven-dependency-management</ artifactId > < groupId >org.example</ groupId > < version >1.0-SNAPSHOT</ version > < type >pom</ type > < scope >import</ scope > </ dependency > </ dependencies > </ dependencyManagement > < dependencies > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-stream_2.12</ artifactId > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http_2.12</ artifactId > </ dependency > < dependency > < groupId >com.typesafe.akka</ groupId > < artifactId >akka-http-spray-json_2.12</ artifactId > </ dependency > </ dependencies > </ project >
如您所见
< dependencyManagement > < dependencies > < dependency > < artifactId >maven-dependency-management</ artifactId > < groupId >org.example</ groupId > < version >1.0-SNAPSHOT</ version > < type >pom</ type > < scope >import</ scope > </ dependency > </ dependencies > </ dependencyManagement >
我们包括了另一个项目的依赖性管理,可以将其应用于继承多个项目的依赖性。
翻译自: https://www.javacodegeeks.com/2020/06/dependency-management-and-maven.html