Maven私服搭建使用

By timebusker on September 5, 2019

近段时间考虑使用内网nexus私服,但外网下载的仓库直接拷贝到内网,发现总是部分包下载不了,可能因为索引没有下载的缘故,为此现在外网搭建一个私服,再把仓库拷贝到内网使用。

nexus下载

官网现在太慢,快速下载地址官网nexus

修改配置

修改配置文件:nexus-2.14.8-01/conf/nexus.properties

# Jetty section
application-port=10000
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus

# Nexus section
nexus-work=${bundleBasedir}/sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF

# orientdb buffer size in megabytes
storage.diskCache.bufferSize=4096

启动服务

  • Windows平台
# 先安装服务到系统

nexus-2.14.8-01/bin/nexus.bat install

# 启动服务
nexus-2.14.8-01/bin/nexus.bat start
  • Linux平台
# 启动服务
nexus-2.14.8-01/bin/nexus start

仓库配置

  • 中央仓库代理对象为阿里云

  • 中央仓库索引设置为自动下载更新

  • 私服仓库releasessnapshots等允许发布包到私服

  • 配置public仓库组的私服仓库访问优先排序

发布本地jar到私服

  • 在本地maven的settings.xml配置仓库管理员账户密码

注意:以下配置的id重要

<server>  
    <id>nexus-releases</id>  
    <username>admin</username>  
    <password>admin123</password>  
</server>  
<server>  
    <id>nexus-snapshots</id>  
    <username>admin</username>  
    <password>admin123</password>  
</server>
  • 项目pom.xml配置public仓库组的私服仓库访问优先排序

注意:以下配置的id需要与maven的settings.xml配置仓库管理员账户密码的关系匹配

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <name>Nexus Release Repository</name>
        <url>http://localhost:10000/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://localhost:10000/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
  • IDEA一键发布

项目版本的后缀RELEASESSNAPSHOTS决定发布的仓库版本

快照版本可以一个版本发布多次、RELEASES一个版本只能发布一下

mvn clean deploy
  • 打包源码发布到私服
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <attach>true</attach>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

遇到问题

  • 有些包使用阿里云下载不了,可能是因为索引导致可以暂时切换到http://repo1.maven.org/maven2/