`
schy_hqh
  • 浏览: 542305 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Maven的本地仓库、中心仓库、私服(NEW)

 
阅读更多
 本地仓库
设置本地仓库到指定目录,而不使用Maven默认的配置(默认放在C:/user/m2.目录下)
打开Maven的解压目录E:\soft\apache-maven-3.1.0\conf,修改settings.xml
配置localRepository即可完成本地仓库的设置:
<localRepository>E:/repository/maven/repos</localRepository>
 
==================================================================
 
中心仓库
即,告诉Maven从外网的哪个地方下载jar包
Maven的安装目录中,在lib目录下,maven-model-builder-3.1.0.jar中,有一个默认的pom.xml文件
其中就配置了Maven默认连接的中心仓库
修改中心仓库
直接在POM.xml中加入repository的配置,指定一个新的url即可
注意:这里仍然使用<id>central</id>,目的在于覆盖Maven中的配置的id为central的repository!
	<repositories>
		<repository>
			<id>central</id>
			<name>My Central Repository</name>
			<url>http://repo.maven.apache.org/maven2</url>
			<layout>default</layout>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
 
==================================================================
 
 私服
配置在局域网环境中,为局域网中所有开发人员提供jar包的统一管理
本地仓库(本机)--->私服(局域网)--->中心仓库(外部网络)
 
私服的安装
1.下载NEXUS,http://www.sonatype.org
2.解压
3.配置环境变量:
新建环境变量:NEXUS_HOME = E:\soft\nexus-2.5.1-01
加入到path中:%NEXUS_HOME%\bin;
4.打开CMD命令行
C:\Users\Administrator>nexus install      安装服务
C:\Users\Administrator>nexus start         启动服务
C:\Users\Administrator>nexus uninstall  卸载服务
5.访问私服
使用默认账户:admin 密码:admin123
NEXUS内部使用Jetty作为服务器
 http://localhost:8081/nexus   【界面用extjs开发的】
 
仓库的分类
查看Repository
 
host仓库--->内部项目的发布仓库
Snapshots   发布内部snapshots版本的仓库
Releases     发布内部release版本的仓库
3rd party      发布第3方jar包的仓库,如oracle数据库驱动,open-189.jar
 
proxy仓库--->从远程中心仓库查找jar包的仓库
Apache Snapshots    查找Apache项目的快照版本的仓库
Central   中心仓库http://repo1.maven.org/maven2/
Codehaus Snapshots    查找Codehaus 的快照版本的仓库
 
group仓库--->把仓库按组划分,以组为单位进行管理
 
virtual仓库  
 
私服的配置 / Repository的配置
在parent模块的pom.xml中加入私服的配置,让Maven从私服下载jar包,而不直接去远程仓库下载。
默认情况下,Maven下载jar包将直接连接到外网http://repo1.maven.org/maven2/去下载;
安装私服之后,让Maven下载jar包先从私服查找,如果没有,再从外网下载并保存在私服上
在POM在加入下面的配置,其中url为NEXUS私服的Public Repository对外的地址
以后,Maven下载构建(jar包或插件)都将从这里开始下载
<project>
  
  ...
  
  <!-- 配置私服地址 -->
  <repositories>
    <repository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
      <snapshots><enabled>true</enabled></snapshots>
      <releases><enabled>true</enabled></releases>
    </pluginRepository>
  </pluginRepositories>

  ...

<project>
 
通过settings.xml来配置私服
由于所有的Maven项目都会用settings.xml中的配置进行解析,如果将Repository配置到这个文件中,那么对所有的Maven项目都将生效。
此时,Maven项目中的POM文件就不需要再配置私服地址了!
注意:修改settings.xml文件时,看IDE中关联的是哪个settings文件。
如C:\user\.m2目录下可能存在,Maven的解压目录下也存在,具体修改哪个根据实际情况而定。如,Eclipse下,查看Maven的User Settings选项即能看到关联。
我的IDE关联的是Maven\conf目录下的settings.xml:
E:\soft\apache-maven-3.1.0\conf\settings.xml
首先,通过<profile/>添加Repository和pluginRepository
<settings>

  ...

  <profiles>
     <profile>
      <id>profile-nexus</id>

	  <repositories>
		<repository>
		  <id>nexus</id>
		  <url>http://localhost:8081/nexus/content/groups/public/</url>
		  <snapshots><enabled>true</enabled></snapshots>
		  <releases><enabled>true</enabled></releases>
		</repository>
	  </repositories>
	  <pluginRepositories>
		<pluginRepository>
		  <id>nexus</id>
		  <url>http://localhost:8081/nexus/content/groups/public/</url>
		  <snapshots><enabled>true</enabled></snapshots>
		  <releases><enabled>true</enabled></releases>
		</pluginRepository>
	  </pluginRepositories>
    </profile>
  </profiles>

  ...

</settings>
 
然后,使用<activeProfiles>对上面的配置进行激活(通过配置的id标识进行激活)
 
  <activeProfiles>
    <activeProfile>profile-nexus</activeProfile>
  </activeProfiles>
 
 
现在,本地机器上创建Maven项目,都会使用settings中有关仓库的配置了
本地仓库:
<localRepository>E:/repository/maven/repos</localRepository>
本地Maven下载的依赖包和插件都将放到E:/repository/maven/repos目录中
私服:
本地所有Maven项目,下载构建都统一从http://localhost:8081/nexus/content/groups/public/ 下载!
【私服上不存在某个构建时,再从远程下载】
远程仓库:
如果远程仓库连接不上,则通过nexus修改central的地址即可!
当前使用Maven的默认配置:http://repo1.maven.org/maven2/
 
 
 
 
 
  • 大小: 89.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics