莫大人

maven 依赖管理

1.maven项目POM.XML配置parent标签
当多个项目依赖同一批包时,就可以创建一个父工程来管理这些公共的包。
在需要依赖这些包的子项目中就可以使用 parent 引入这批包了,并且不需要写版本号,
他使用的是父工程的版本号,这样当父工程的版本号变化时,子项目的版本将可以同步改变。

2.使用import scope解决maven继承(单)问题

maven parent 标签一个项目只能继承一个父工程。当多个子项目依赖的包不同时,如 A B 依赖 M,
C D 依赖N,E F 依赖 M,N,那么我该如何做呢,为了解决多继承的问题。我们使用标签 dependencyManagement 管理包,
通过 <scope>import</scope> 引入依赖

 父工程 pom.xml

<?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>
	<parent>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-dependencies-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/>
	</parent>
	<artifactId>spring-cloud-dependencies</artifactId>
	<version>Finchley.RELEASE</version>
	<name>spring-cloud-dependencies</name>
	<description>Spring Cloud Dependencies</description>
	<packaging>pom</packaging>
	<properties>
		<main.basedir>${basedir}/../..</main.basedir>
		<spring-cloud-aws.version>2.0.0.RELEASE</spring-cloud-aws.version>
		<spring-cloud-bus.version>2.0.0.RELEASE</spring-cloud-bus.version>
		<spring-cloud-cloudfoundry.version>2.0.0.RELEASE</spring-cloud-cloudfoundry.version>
		<spring-cloud-commons.version>2.0.0.RELEASE</spring-cloud-commons.version>
		<spring-cloud-config.version>2.0.0.RELEASE</spring-cloud-config.version>
		<spring-cloud-consul.version>2.0.0.RELEASE</spring-cloud-consul.version>
		<spring-cloud-contract.version>2.0.0.RELEASE</spring-cloud-contract.version>
		<spring-cloud-function.version>1.0.0.RELEASE</spring-cloud-function.version>
		<spring-cloud-gateway.version>2.0.0.RELEASE</spring-cloud-gateway.version>
		<spring-cloud-netflix.version>2.0.0.RELEASE</spring-cloud-netflix.version>
		<spring-cloud-openfeign.version>2.0.0.RELEASE</spring-cloud-openfeign.version>
		<spring-cloud-security.version>2.0.0.RELEASE</spring-cloud-security.version>
		<spring-cloud-sleuth.version>2.0.0.RELEASE</spring-cloud-sleuth.version>
		<spring-cloud-stream.version>Elmhurst.RELEASE</spring-cloud-stream.version>
		<spring-cloud-task.version>2.0.0.RELEASE</spring-cloud-task.version>
		<spring-cloud-vault.version>2.0.0.RELEASE</spring-cloud-vault.version>
		<spring-cloud-zookeeper.version>2.0.0.RELEASE</spring-cloud-zookeeper.version>
	</properties>
	<dependencyManagement>
		<dependencies>
	        <!-- bom dependencies at the bottom so they can be overridden above -->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-commons-dependencies</artifactId>
				<version>${spring-cloud-commons.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<profiles>
		<profile>
			<id>spring</id>
			<repositories>
				<repository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/release</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
</project>

 

 

子工程 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.guo</groupId>
  <artifactId>springcloud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  

 

posted on 2019-02-15 20:32  莫大人  阅读(282)  评论(0编辑  收藏  举报

导航