04-maven仓库和坐标
1.maven仓库 ![]()
![maven.PNG]()
![]()
![]()
![mvn03.PNG]()
![mvn04.PNG]()
![mvn05.PNG]()
![mvn06.PNG]()
仓库指的是在某个位置统一存储所有maven项目共享构件.
可以这样理解:在电脑的某个位置存储构件.

依赖:项目对jar包的依赖.
本地仓库:之前有配置过E:\mavenRepository(settings.xml),缓存需要的构件.
中央仓库:所有的项目都指向maven的中央仓库;
私服:局域网内的一个仓库服务器.
中央仓库:(服务器在国外,有可能无法访问,或者访问比较慢)
pom-4.0.0.xml是maven最顶级的pom文件,所有的项目都继承自该pom文件,该文件配置了maven的中央仓库.
-<repository><id>central</id> //唯一的<name>Central Repository</name> //指定仓库的名字<url>https://repo.maven.apache.org/maven2</url> //指定仓库的路径<layout>default</layout> //布局-<snapshots><enabled>false</enabled> //默认不支持快照下载</snapshots></repository>
pom-4.0.0.xml所在位置:
E:\apache-maven-3.3.3\lib\maven-model-builder-3.3.3.jar\org\apache\maven\model\pom-4.0.0.xml
查看maven的中央仓库:https://repo.maven.apache.org/maven2 //存储所有的构件
镜像仓库:(更快,更稳定)
settings.xml配置镜像仓库文件
E:\apache-maven-3.3.3\conf\settings.xml
<mirrors><!-- mirror| Specifies a repository mirror site to use instead of a given repository. The repository that| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.|<mirror> //配置镜像仓库 如果需要配置的话将<mirror>...</mirror>拷贝出来即可<id>mirrorId</id> //如果配置了镜像仓库,不会使用中央仓库<mirrorOf>repositoryId</mirrorOf><name>Human Readable Name for this Mirror.</name><url>http://my.repository.com/repo/path</url></mirror>--></mirrors>
项目如何去寻找构件:

2.maven坐标
1)坐标定义
坐标是标识构件在仓库中的唯一位置.通过坐标可以在仓库中找到构件.
坐标属性:(从这里可以看出:在定义项目的时候已经定义好了坐标,定义项目的时候需要用到坐标的这三个属性)

通过坐标找到唯一的项目.
2)项目如何添加构件依赖

示例:
在之前导入到eclipse下的maven-01项目下演示:
1)到中央仓库搜索spring-test构件,并选择org.springframework : spring-test : 4.2.6.RELEASE
2)复制该构件的依赖,添加到项目maven-01的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> //标示了pom文件的版本号<groupId>com.sohu.train</groupId> //标识当前项目的坐标<artifactId>maven-01</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging> //项目打包的格式<name>maven-01</name> //指定项目名称<url>http://maven.apache.org</url><properties> //编码格式<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!-- 添加构件依赖 --><dependencies> //添加构件依赖<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 新添加进来的--> //添加成功会在Maven Dependencies 这里看到下载好的spring-test<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.6.RELEASE</version></dependency></dependencies></project>
3.如何发布项目到仓库
1) mvn install 发布到本地仓库
CMD进入maven-01工程目录下执行命令mvn install:
F:\mavenDemo\train\maven-01>mvn install[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building maven-01 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-01 ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory F:\mavenDemo\train\maven-01\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-01 ---[INFO] Nothing to compile - all classes are up to date[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-01 ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory F:\mavenDemo\train\maven-01\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-01 ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to F:\mavenDemo\train\maven-01\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-01 ---[INFO] Surefire report directory: F:\mavenDemo\train\maven-01\target\surefire-reports-------------------------------------------------------T E S T S-------------------------------------------------------Running com.sohu.train.AppTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-01 ---[INFO] Building jar: F:\mavenDemo\train\maven-01\target\maven-01-1.0-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-01 ---Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (3 KB at 1.2 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom (19 KB at 15.8 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (2 KB at 1.4 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5 KB at 5.9 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (8 KB at 5.5 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pomDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (8 KB at 4.0 KB/sec)Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jarDownloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jarDownloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 KB at 6.2 KB/sec)Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (226 KB at 53.2 KB/sec)//将打包好的jar文件放到本地的仓库目录文件夹下[INFO] Installing F:\mavenDemo\train\maven-01\target\maven-01-1.0-SNAPSHOT.jar to E:\mavenRepository\com\sohu\train\maven-01\1.0-SNAPSHOT\maven-01-1.0-SNAPSHOT.jar[INFO] Installing F:\mavenDemo\train\maven-01\pom.xml to E:\mavenRepository\com\sohu\train\maven-01\1.0-SNAPSHOT\maven-01-1.0-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 15.444 s[INFO] Finished at: 2016-05-13T19:32:34+08:00[INFO] Final Memory: 20M/225M[INFO] ------------------------------------------------------------------------
2) mvn deploy 发布到远程仓库
远程仓库地址配置和远程仓库验证信息配置
4. 依赖的特性
1) 依赖范围
通过scope标签去定义我们构建的范围.maven在运行的时候有三套classpath;classpath是为了所构建的程序运行在Java环境下.
2) 依赖传递
3) 依赖排除
示例:
排除项目maven0-01下commons-logging-1.2.jar依赖 (注意:启动eclipse的自动编译功能或者手动编译)
找到该依赖(commons-logging-1.2.jar)的pom.xml文件拷贝groupId和artifactId这两行,添加到maven-01项目下的pom.xml文件下
<!-- 添加构件依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.6.RELEASE</version><!-- 排除依赖 --><exclusions><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency>
4) 依赖的冲突
示例:
1.短路优先原则:
注释点maven-01的依赖排除;
maven-01依赖maven-03:
a.打包maven-03到本地仓库(执行命令:mvn install)
b.maven-01工程依赖maven-03工程:
配置maven-01的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.sohu.train</groupId><artifactId>maven-01</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>maven-01</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 添加构件依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.6.RELEASE</version><!-- 排除依赖 <exclusions> <exclusion> <groupId>commons-logging</groupId><artifactId>commons-logging</artifactId> </exclusion> </exclusions> --></dependency><!-- 添加对maven-03依赖 --><dependency><groupId>com.sohu.train</groupId><artifactId>maven-03</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>

配置maven-03的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.sohu.train</groupId><artifactId>maven-03</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>maven-03</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- commons-logging依赖 --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.3</version></dependency></dependencies></project>

查看项目依赖的树形结构:
F:\mavenDemo\train\maven-01>mvn dependency:tree[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building maven-01 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ maven-01 ---[INFO] com.sohu.train:maven-01:jar:1.0-SNAPSHOT[INFO] +- junit:junit:jar:3.8.1:test[INFO] +- org.springframework:spring-test:jar:4.2.6.RELEASE:compile[INFO] | \- org.springframework:spring-core:jar:4.2.6.RELEASE:compile[INFO] | \- commons-logging:commons-logging:jar:1.2:compile[INFO] \- com.sohu.train:maven-03:jar:0.0.1-SNAPSHOT:compile[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.168 s[INFO] Finished at: 2016-05-14T21:33:41+08:00[INFO] Final Memory: 12M/154M[INFO] ------------------------------------------------------------------------
2.先声明先优先原则
1)新建maven-02工程,
2)打包maven-02工程到本地仓库(mvn install)
3)maven-01对maven-02的依赖
配置maven-01的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.sohu.train</groupId><artifactId>maven-01</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>maven-01</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 添加构件依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.6.RELEASE</version><!-- 排除依赖 <exclusions> <exclusion> <groupId>commons-logging</groupId><artifactId>commons-logging</artifactId> </exclusion> </exclusions> --></dependency><!-- 添加maven-02依赖 --> //maven-02先声明<dependency><groupId>com.sohu.train</groupId><artifactId>maven-02</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- 添加对maven-03依赖 --><dependency><groupId>com.sohu.train</groupId><artifactId>maven-03</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>
配置maven-02的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.sohu.train</groupId><artifactId>maven-02</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>maven-02</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- commons-logging依赖 --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.2</version></dependency></dependencies></project>









浙公网安备 33010602011771号