Maven私服(Nexus)的简介和使用

私服安装

https://blog.csdn.net/javanbme/article/details/113336338

1. 仓库介绍

1)maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar

2)maven-releases:私库发行版jar

3)maven-snapshots:私库快照(调试版本)jar

4)maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配settings.xml中使用。

Nexus仓库类型介绍

默认安装有以下这几个仓库,在控制台也可以修改远程仓库的地址,第三方仓库等。

  • hosted(宿主仓库库) :存放本公司开发的jar包(正式版本、测试版本)
  • proxy(代理仓库):代理中央仓库、Apache下测试版本的jar包
  • group(组仓库):使用时连接组仓库,包含Hosted(宿主仓库)和Proxy(代理仓库)

找依赖包的流程: 首先在本地仓库中找,如果没命中,那么就找远程私服;远程私服的查找规则同样是先找host属性的私有库,然后再去找proxy属性的远程仓库;可以配置多个proxy;

2. 仓库介绍、创建

2.1 创建jCenter阿里云仓库

http://maven.aliyun.com/nexus/content/groups/public/

2.2 maven中央仓库(无需修改)

https://repo1.maven.org/maven2/

2.3 将Jcenter仓库移至maven-public 组仓库

3. 配置依赖

3.1 添加maven仓库镜像

创建好组仓库之后,修改setting.xml文件,添加maven仓库镜像,如下

<mirrors>
    <mirror>
      <id>nexus-myself</id>
      <!--*指的是访问任何仓库都使用我们的私服-->
      <mirrorOf>*</mirrorOf>
      <name>Nexus myself</name>
      <url>http://121.4.207.231:8081/repository/maven-public/</url>
    </mirror>
</mirrors>

3.2 全局配置下载依赖(即项目pom无需配置)

在maven的setting.xml文件中配置私服配置,这种方式配置后所有本地使用该配置的maven项目的pom文件都无需配置私服下载相关配置。

<profiles>
  <profile>
     <id>mycof</id>
        <repositories>
        <!-- 私有库地址-->
          <repository>
          <id>nexus</id>
          <url>http://121.4.207.231:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>      
      <pluginRepositories>
        <!--插件库地址-->
        <pluginRepository>
          <id>nexus</id>
          <url>http://121.4.207.231:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
           </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
</profiles>

3.3 激活使用上面的配置

<!--激活profile-->
<activeProfiles>
  <activeProfile>mycof</activeProfile>
</activeProfiles>

3.4 单独项目下载依赖(即项目pom文件中配置) (扩展)

这种配置是修改单个项目的pom文件,无需修改maven的setting配置

<repositories>
  <repository>
    <id>nexus</id>
    <url>http://121.4.207.231:8081/repository/maven-public/</url>
    <releases>
      <enabled>true</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
  </repository>
</repositories>

4. 上传Jar包到私服

对于中央仓库没有的jar包,需要我们自己将jar包发布到私服中去,其中jar包主要分为两类,

一类是本地自己开发供给项目组其余同事使用,这种直接配置项目的pom文件和maven的setting文件,之后deploy发布即可发布到;

另一类是第三方jar包,可以直接使用web页面上传并设置对应GAV即可;

4.1 本地maven开发的项目上传配置

maven的setting文件配置

<servers>
    <server>  
        <id>maven-releases</id>  
        <username>admin</username>  
        <password>admin123</password>  
    </server>  
    <server>  
        <id>maven-snapshots</id>  
        <username>admin</username>  
        <password>admin123</password>  
    </server>
  </servers>

项目中的pom文件配置

<distributionManagement>
    <repository>
        <id>maven-releases</id>
        <name>Nexus Release Repository</name>
        <url>http://121.4.207.231:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://121.4.207.231:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

<profiles>
    <profile>
        <id>java8-doclint-disabled</id>
        <activation>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <javadoc.opts>-Xdoclint:none</javadoc.opts>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!--编译插件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <!--解决JDK7以后,带com.sun.*的类库将不会被支持-->
                <!--<compilerArgument>-XDignore.symbol.file</compilerArgument>-->
                <compilerArguments>
                    <bootclasspath>${java.home}/lib/rt.jar:${java.home}/lib/jce.jar</bootclasspath>
                </compilerArguments>
            </configuration>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
        </plugin>
        <!-- 生成javadoc文档包的插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9</version>
            <executions>
                <execution>
                    <id>attach-javadocs</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <additionalparam>${javadoc.opts}</additionalparam>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 生成sources源码包的插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <attach>true</attach>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!--部署jar包到远程仓库-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
        </plugin>
    </plugins>
</build>

  执行maven的deploy命令

  ps: 第一次初始化有点慢 耐心等待  deploy成功之后查看依赖

  Release仓库默认不支持重复发布 如果项目中使用的是SNAPSHOT后缀 即不需要配置  快照版本会根据上传时间检测新的快照版本

4.2 第三方jar包上传

image.png

5. 引入私服Jar包

猜你喜欢

转载自blog.csdn.net/javanbme/article/details/113345312