Maven 使用技巧

Maven安装手动下载的jar包

我下载的这个 jar 包是放到了 D:\mvn 目录下(D:\mvn\spring-context-support-3.1.0.RELEASE.jar)
那么我在 cmd 中敲入的命令就应该是:

mvn install:install-file -Dfile=D:\mvn\spring-context-support-3.1.0.RELEASE.jar -DgroupId=org.springframework

-DartifactId=spring-context-support -Dversion=3.1.0.RELEASE -Dpackaging=jar

 

安装到私服

mvn deploy:deploy-file -DgroupId=com.bea.xml -DartifactId=jsr173-ri -Dversion=1.0 -Dpackaging=jar -Dfile=[path to file] -Durl=[url] -DrepositoryId=[id]  

转载:https://huanyue.iteye.com/blog/485774

 

配置私服:

转载:https://schy-hqh.iteye.com/blog/1950126

本地仓库

设置本地仓库到指定目录,而不使用Maven默认的配置(默认放在C:/user/m2.目录下)

打开Maven的解压目录E:\soft\apache-maven-3.1.0\conf,修改settings.xml

配置localRepository即可完成本地仓库的设置:

Xml代码 

 收藏代码

  1. <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!

Xml代码 

 收藏代码

  1. <repositories>  
  2.     <repository>  
  3.         <id>central</id>  
  4.         <name>My Central Repository</name>  
  5.         <url>http://repo.maven.apache.org/maven2</url>  
  6.         <layout>default</layout>  
  7.         <snapshots>  
  8.             <enabled>false</enabled>  
  9.         </snapshots>  
  10.     </repository>  
  11. </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包或插件)都将从这里开始下载

Xml代码 

 收藏代码

  1. <project>  
  2.     
  3.   ...  
  4.     
  5.   <!-- 配置私服地址 -->  
  6.   <repositories>  
  7.     <repository>  
  8.       <id>nexus</id>  
  9.       <url>http://localhost:8081/nexus/content/groups/public/</url>  
  10.       <snapshots><enabled>true</enabled></snapshots>  
  11.       <releases><enabled>true</enabled></releases>  
  12.     </repository>  
  13.   </repositories>  
  14.   <pluginRepositories>  
  15.     <pluginRepository>  
  16.       <id>nexus</id>  
  17.       <url>http://localhost:8081/nexus/content/groups/public/</url>  
  18.       <snapshots><enabled>true</enabled></snapshots>  
  19.       <releases><enabled>true</enabled></releases>  
  20.     </pluginRepository>  
  21.   </pluginRepositories>  
  22.   
  23.   ...  
  24.   
  25. <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

Xml代码 

 收藏代码

  1. <settings>  
  2.   
  3.   ...  
  4.   
  5.   <profiles>  
  6.      <profile>  
  7.       <id>profile-nexus</id>  
  8.   
  9.       <repositories>  
  10.         <repository>  
  11.           <id>nexus</id>  
  12.           <url>http://localhost:8081/nexus/content/groups/public/</url>  
  13.           <snapshots><enabled>true</enabled></snapshots>  
  14.           <releases><enabled>true</enabled></releases>  
  15.         </repository>  
  16.       </repositories>  
  17.       <pluginRepositories>  
  18.         <pluginRepository>  
  19.           <id>nexus</id>  
  20.           <url>http://localhost:8081/nexus/content/groups/public/</url>  
  21.           <snapshots><enabled>true</enabled></snapshots>  
  22.           <releases><enabled>true</enabled></releases>  
  23.         </pluginRepository>  
  24.       </pluginRepositories>  
  25.     </profile>  
  26.   </profiles>  
  27.   
  28.   ...  
  29.   
  30. </settings>  

 

然后,使用<activeProfiles>对上面的配置进行激活(通过配置的id标识进行激活)

 Xml代码 

 收藏代码

  1. <activeProfiles>  
  2.   <activeProfile>profile-nexus</activeProfile>  
  3. </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/

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值