maven仓库分类apache
本地仓库、远程仓库。远程仓库又有私服、中央仓库、其它公共库。中央仓库是maven自带的核心仓库。maven
仓库配置
远程仓库能够配置多个,超级pom中定义的中央仓库布局
<repositories> <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> </repositories>
如下是jboss远程仓库配置this
<repositories> <repository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>false</enabled> <checksumPolicy>warn</checksumPolicy> </snapshots> <layout>default</layout> </repository> </repositories>
layout标签:使用default仓库布局,仓库布局就是构件在仓库中的存储路径,默认的构件仓库路径如com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT。maven二、maven3的布局同样,maven1和他们不同。url
<snapshots><enabled>false</enabled></snapshots>:不从该仓库下载快照版本,即不稳定版本SNAPSHOT。
updatePolicy 从远程仓库检查更新的频率,默认值为daily,天天更新一次;never,从不检查;always,每次构建都检查;interval:X,每间隔X分钟检查一次更新。X为任意整数。
checksumPolicy 忽略spa
maven的setting.xml不支持直接配置repositories和pluginRepositories。所幸maven还提供了profile机制,能让用户将仓库配置放到setting.xml 中的profile里,以下:插件
<profiles> <profile> <id>nexus</id> <repositories> <repository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>false</enabled> <checksumPolicy>warn</checksumPolicy> </snapshots> <layout>default</layout> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> <releases> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </releases> <snapshots> <enabled>false</enabled> <checksumPolicy>warn</checksumPolicy> </snapshots> <layout>default</layout> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
当执行maven构建的时候,激活的profile会将仓库配置应用到项目中去。命令行
maven会区别对待依赖的远程仓库与插件远程仓库代理
插件的仓库使用pluginRepositories来配置,除了pluginRepositories和pluginRepositorie标签不一样以外,其他全部子元素和配置依赖仓库彻底同样。code
私服不是maven的核心概念,它是一种衍生出来的特殊的maven仓库。
maven官方区别依赖仓库和插件仓库,虽然id、url全部的元素值都相同;nexus的 maven-central 代理仓库不区别依赖仓库和插件仓库,从这里便可如下载普通依赖,也能够下载maven插件。
配置镜像
settings文件
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
该mirrorOf的值为central,表示该配置为中央仓库镜像,任何对于中央仓库的请求都会转至此镜像。
另外三个元素与通常仓库配置无异,表示该镜像仓库的惟一标识符、名称及地址。
镜像一个更常见的用法是结合私服。由私服代理外部的公共仓库,本地配置镜像至私服以下。
<mirror>
<id>maven-nexus</id>
<name>this is my nexus</name>
<url>http://localhost:8081/repository/maven-nexus/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<mirrorOf>*</mirrorOf> 表示该配置是全部Maven仓库的镜像,对于全部远程仓库的请求都会被转移到http://
<mirrorOf>external:*</mirrorOf> 匹配全部远程仓库,除localhost、file://协议。
<mirrorOf>repo1,repo2</mirrorOf> 匹配仓库1和仓库2
<mirrorOf>*,!repo1</mirrorOf> 匹配全部远程仓库,repo1除外。
因为镜像仓库彻底屏蔽了被镜像仓库,当镜像仓库不稳定或者中止服务的时候,maven将没法访问被镜像仓库,没法下载构件。
部署构件至远程仓库
编辑pom文件
<distributionManagement> <repository> <id></id> <name></name> <url></url> </repository> <snapshotRepository> <id></id> <name></name> <url></url> </snapshotRepository> </distributionManagement>
repository表示发布版本构件的仓库,后者表示快照版本构件的仓库。
配置后,在命令行运行mvn clear deploy,若是当前项目是快照版本,就部署到快照版本仓库,反之部署到发布版本仓库。
私服、远程仓库、镜像若是须要认证,配置方法都同样。基于仓库或镜像id配置便可。
认证信息必须配置在settings.xml文件中
<servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>
这里的id必须和须要认证的仓库或镜像的id同样。