搭建私人服务器 Nexus

Maven仓库毕竟需要连接网络, 即使换了国内的源, 我们可以在本地搭建一个私有仓库, 以加快项目依赖下载速度。

下载并搭建Nexus服务器

下载最新版的nexus,下载地址:http://www.sonatype.org/downloads/nexus-latest-bundle.zip

下载完成后,解压到某个目录,进入bin下执行以下命令安装:

quanzaiyu@DESKTOP-3KIMAKO MINGW64 /d/Software/nexus/nexus-2.14.13-01/bin
$ ./nexus.bat install
wrapper  | nexus installed.

安装完成后,使用以下命令启动:

quanzaiyu@DESKTOP-3KIMAKO MINGW64 /d/Software/nexus/nexus-2.14.13-01/bin
$ ./nexus.bat start
wrapper  | Starting the nexus service...
wrapper  | Waiting to start...
wrapper  | Waiting to start...
wrapper  | Waiting to start...
wrapper  | Waiting to start...
wrapper  | Waiting to start...
wrapper  | nexus started.

启动后就可在浏览器中打开了:http://localhost:8081/nexus/

默认用户名/密码是 admin/admin123

下载jboss eap7.3的离线资源包

maven默认的官方资源库http://repo1.maven.org/maven2/,很多第三方的jar包没有,所以我再添加一个jboss的资源库,下载地址:https://developers.redhat.com/products/eap/download/

点击左侧的Repositories,然后再点击右侧的Add,会弹出下拉菜单,选择Proxy Repository

然后解压到指定目录,建议放在 %nexus_home%\sonatype-work\nexus\storage\ 下

然后add一个hosted repository,参考下图:

把刚才添加的二个respoitory添加到Public Repositories组

刚才我们添加了二个新的资源库,检查一下是否包括在默认的Public Repositories组里

修改本机当前用户下.m2目录下的settings.xml

参考配置:

<?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>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>
  <mirrors>
    <mirrors>
      <mirror>
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://http://localhost:8081/nexus/content/groups/public/</url>
      </mirror>
    </mirrors>
    <!-- 阿里云仓库 -->
    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
    <!-- 中央仓库1 -->
    <mirror>
      <id>repo1</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://repo1.maven.org/maven2/</url>
    </mirror>
    <!-- 中央仓库2 -->
    <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>nexus</id>
          <name>Nexus</name>
          <url>http://localhost:8081/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
            <name>Nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

然后随便创建一个maven项目,执行mvn clean package 之类,即可看到是从本地nexus服务器下载的依赖包

参考资料

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress