首页 > 技术文章 > 搭建私服

jinloooong 2017-10-19 18:23 原文

私服简介:

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

 

      我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:Apache ArchivaArtifactorySonatype Nexus。这里我们使用 Sonatype Nexus。

1.下载安装文件: https://www.sonatype.com/download-oss-sonatype

 

2.进入cmd,切换到安装文件nexus-3.6.0-02的bin目录;

3.执行nexus /run,看见如下内容即启动成功:

 

4.如果要安装为windows服务,执行nexus /install

5. 浏览器中输入:http://localhost:8081

 

6. 系统有两个默认账号admin、anonymous,其中admin具有全部权限默认密码admin123;anonymous作为匿名用户,只具有查看权限。

 

7. maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar 
maven-releases:私库发行版jar 
maven-snapshots:私库快照(调试版本)jar 
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

 

  • hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
  • proxy 代理仓库:代理公共的远程仓库;
  • virtual 虚拟仓库:用于适配 Maven 1;
  • group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。

 

 

8. 本地maven库配置settings.xml

<?xml version="1.0" encoding="utf-8"?>

 

<settings>

  <pluginGroups>

    <pluginGroup>org.sonatype.plugins</pluginGroup>

  </pluginGroups> 

  <servers>

    <server>

      <id>nexus</id> 

      <username>admin</username>  <!-- 用户名 -->

      <password>admin123</password> <!-- 密码 -->

    </server>

  </servers> 

  <mirrors>

    <mirror>

      <id>nexus</id> 

      <mirrorOf>*</mirrorOf> 

      <url>http://localhost:8081/repository/maven-public/</url> <!-- 路径 仓库组 -->

    </mirror> 

    <mirror>

      <id>repo2</id> 

      <mirrorOf>central</mirrorOf>  <!-- 代理仓库名称 -->

      <name>Human Readable Name for this Mirror.</name> 

      <url>http://repo2.maven.org/maven2/</url>

    </mirror>

  </mirrors> 

  <profiles>

    <profile>

      <id>nexus</id> 

      <repositories>

        <repository>

          <id>central</id> 

          <url>http://central</url> 

          <releases>

            <enabled>true</enabled>

          </releases> 

          <snapshots>

            <enabled>true</enabled>

          </snapshots>

        </repository>

      </repositories> 

      <pluginRepositories>

        <pluginRepository>

          <id>central</id> 

          <url>http://central</url> 

          <releases>

            <enabled>true</enabled>

          </releases> 

          <snapshots>

            <enabled>true</enabled>

          </snapshots>

        </pluginRepository>

      </pluginRepositories>

    </profile>

  </profiles> 

  <activeProfiles>

    <activeProfile>nexus</activeProfile>

  </activeProfiles>

</settings>

 

9.项目pom.xml文件

<?xml version="1.0" encoding="utf-8"?>

 

<distributionManagement>

  <repository>

    <id>nexus</id> 

    <name>Releases</name> 

    <url>http://localhost:8081/repository/maven-releases</url>

  </repository> 

  <snapshotRepository>

    <id>nexus</id> 

    <name>Snapshot</name> 

    <url>http://localhost:8081/repository/maven-snapshots</url>

  </snapshotRepository>

</distributionManagement>

<build>

  <defaultGoal>compile</defaultGoal> 

  <finalName>page</finalName> 

  <plugins>

    <plugin>

      <groupId>org.apache.maven.plugins</groupId> 

      <artifactId>maven-surefire-plugin</artifactId> 

      <configuration>

        <skip>true</skip>

      </configuration>

    </plugin> 

    <plugin>

      <groupId>org.apache.maven.plugins</groupId> 

      <artifactId>maven-compiler-plugin</artifactId> 

      <version>3.3</version> 

      <configuration>

        <source>1.8</source> 

        <target>1.8</target>

      </configuration>

    </plugin>

  </plugins>

</build>

 

10.编译到maven私库

deploy -e 
项目右单击->Run As->Maven build.. 
进入如下界面 

 

11.快照编译:pom.xml中版本设置

<version>0.0.1-SNAPSHOT</version>

       编译后在nexus中看到如下图结果,快照已经编译到nexus中Components-> maven-snapshots。 

 

12.发行版编译:pom.xml中版本设置

<version>0.0.1-RELEASE</version>

编译后在nexus中看到如下图结果,发行版已经编译到nexus中Components->maven-releases。 

 

推荐阅读