maven 搭建私服

开发十年,就只剩下这套Java开发体系了 >>>   maven 搭建私服

1、私服简介

    私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。

maven 搭建私服

2、安装jdk

2.1 jdk下载地址

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

2.2 安装目录

/usr/local/java

2.3 配置

vim /etc/profile

添加配置

export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export PATH=$JAVA_HOME/bin:$PATH

3、安装私服

3.1 下载

    下载地址:http://www.sonatype.com/download-oss-sonatype

    下载最新的nexus:

maven 搭建私服

3.2 解压和创建链接

$ sudo tar xvf latest-unix.tar.gz -C /opt
$ sudo ln -s /opt/nexus-3.3.1-01/ /opt/nexus

3.3 创建 nexus 用户

$ sudo adduser nexus
$ sudo chown -R nexus:nexus /opt/nexus

3.4 授权

$ sudo chown -R nexus:nexus /opt/nexus
$ sudo chown -R nexus:nexus /opt/sonatype-work/

3.5 修改/opt/nexus/bin/nexus.rc

$ sudo vim /opt/nexus/bin/nexus.rc

run_as_user="nexus"   
:wq

3.6 安装服务

$ sudo ln -s /opt/nexus/bin/nexus /etc/init.d/nexus
$ sudo systemctl enable nexus
nexus.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig nexus on

3.7 启动服务

$ sudo systemctl start nexus

    此时可以访问 http://localhost:8081,用户名:admin ,密码:admin123。

3.8 报错

maven 搭建私服

    如果报这个错误,可以通过2步骤配置jdk的方式解决。

3.9 添加代理仓库

    由于访问*仓库有时候会比较慢,这里我添加一个阿里云的代理仓库,然后优先级放到默认*库之前,, 阿里云的maven仓库url为http://maven.aliyun.com/nexus/content/groups/public

maven 搭建私服

maven 搭建私服

maven 搭建私服

    然后再public组里面讲这个aliyun-proxy仓库加入,排在maven-central之前即可。

maven 搭建私服

maven 搭建私服

4、本地私服使用

4.1 maven 修改

    可在maven的默认配置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>

<mirrors>
    <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://ip:8081/repository/maven-public/</url>
    </mirror>
</mirrors>

<profiles>
    <profile>  
      <id>dev</id>
      <repositories>
        <repository>
          <id>Nexus</id>
          <url>http://ip:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <activation>
        <activeByDefault>true</activeByDefault>      
        <jdk>1.8</jdk>
      </activation>
      <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      </properties>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>

4.2 项目修改

<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Releases</name>
        <url>http://ip:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Snapshot</name>
        <url>http://ip:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

    注意:上面的repository的id值一定要跟settings.xml文件中配置的server一致。

    打包:mvn deploy