Maven 基础配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ldz_wolf/article/details/53821513

参考一些博文:

  1. Maven 仓库、镜像
  2. Maven基础-默认中央仓库[settings.xml 配置详解 ]

一、下载安装

  1. 下载地址:http://maven.apache.org/download.cgi。选择一个适合自己开发环境的版本.
  2. 解压到:D:\dev\apache-maven-3.3.9;
  3. 配置环境变量:新建MAVEN_HOME:D:\dev\apache-maven-3.3.9;
  4. 修改Path变量:在最后添加“;%MAVEN_HOME%\bin;”
  5. 检查配置是否成功,打开cmd,输入:”mvn -version” ,显示以下内容表示安装成功
    这里写图片描述

二、配置setting.xml

  1. 默认情况下,是没有任何配置内容的,要将maven用起来,第一,要配置个中央仓库位置,让maven知道在哪里下载jar包,第二,配置jar下载后存储位置,默认在C盘用户目录下面,我一般不喜欢默认 ,自己指定的一存储位置,一切由自己掌控。下面给出最基本的setting配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups></pluginGroups>
  <proxies></proxies>
  <servers></servers>
  <profiles>
        <repositories>        
      <repository>        
        <id>central</id>        
        <name>Maven Repository Switchboard</name>        
        <layout>default</layout>        
        <url>http://repo1.maven.org/maven2</url>        
        <snapshots>        
          <enabled> false</enabled>        
        </snapshots>        
      </repository>       
      <repository>        
        <id>restlet</id>        
        <name>restlet</name>        
        <layout>default</layout>        
        <url>http://maven.restlet.org</url>        
        <snapshots>        
          <enabled> false</enabled>        
        </snapshots>        
      </repository>
      <repository>        
        <id>sonatype</id>        
        <name>Maven Repository sonatype</name>        
        <layout>default</layout>        
        <url>http://repository.sonatype.org/content/groups/public/</url>        
        <snapshots>        
          <enabled>false</enabled>        
        </snapshots>        
      </repository>        
    </repositories> 
  </profiles>
</settings>

三、Maven本地中央仓库搭建:http://www.linuxidc.com/Linux/2016-08/134617.htm

本地maven私服搭建起后,在setting.xml文件中添加本地仓库路径;配制镜像和本地仓库路径,镜像的意思,我这里理解的是:可以看到下面mirrorOf指像的是远程中央仓库的唯一标识名称,那么就是说当需要从远程中央库更新包的时候,我会先从地址为:192.168.2.200这个仓库中去找(这个是我的本地仓库)。就不会每次都去远程去更新了,可以节约很多时间。

<mirrors>
 <mirror>
      <id>personal-central</id>
      <mirrorOf>central</mirrorOf>
      <name>personal-central for this Mirror.</name>
      <url>http://192.168.2.200:8081/nexus/content/repositories/central/</url>
    </mirror>
 </mirrors>
<repositories>     
.
.
. 
<repository>        
        <id>thirdparty</id>        
        <name>Maven Repository Swithcboard</name>        
        <layout>default</layout>        
        <url>http://192.168.2.200:8081/nexus/content/repositories/thirdparty/</url>        
        <snapshots>        
          <enabled>false</enabled>        
        </snapshots>        
</repository>        
<repositories>      

如何把我们自己打的包上传到Maven库上?执行下面两条命令即可。

1、首先是添加到本地库目录下

mvn install:install-file -Dfile=G:\IntellijIdea\lib\framework\guava-13.0.1.jar -DgroupId=guava -DartifactId=guava -Dversion=13.0.1 -Dpackaging=jar

– DgroupId和DartifactId构成了该jar包在pom.xml的坐标, 对应依赖的DgroupId和DartifactId
– Dfile表示需要上传的jar包的绝对路径
– Dpackaging 为安装文件的种类

2、上传到maven私服上,如果要上传到Maven 上,那么setting.xml需要配置server账号密码。server中id指的是你仓库id,

  <servers>
    <server>
      <id>thirdparty</id>
      <username>admin</username>
      <password>*******</password>
    </server>
  </servers>

mvn deploy:deploy-file -DgroupId=guava -DartifactId=guava -Dversion=13.0.1 -Dpackaging=jar -Dfile=G:\IntellijIdea\lib\framework\guava-13.0.1.jar -Durl=http://192.168.2.200:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

– DgroupId和DartifactId构成了该jar包在pom.xml的坐标, 对应依赖的DgroupId和DartifactId
– Dfile表示需要上传的jar包的绝对路径
– Durl私服上仓库的url精确地址(打开nexus左侧repositories菜单,可以看到该路径)
– DrepositoryId服务器的表示id,在nexus的configuration可以看到

猜你喜欢

转载自blog.csdn.net/ldz_wolf/article/details/53821513