一、Maven  Nexus私服的原理

Nexus就是Maven的私服

从项目实际开发来看:

1.一些无法从外部仓库下载的构件,例如内部的项目还能部署到私服上,以便供其他依赖项目使用。

2. 为了节省带宽和时间,在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库。当本地Maven项目需要下载构件时,先去私服请求,如果私服没有,则再去远程仓库请求,从远程仓库下载构件后,把构件缓存在私服上。这样,及时暂时没有Internet链接,由于私服已经缓存了大量构件,整个项目还是可以正常使用的。同时,也降低了中央仓库的负荷。

Maven私服环境搭建以及使用(基于docker)_docker

二、什么场景用Maven私服

在实际开发中,项目中可能会用到第三方的jar、内部通讯的服务接口都会打入到公司的私服中。

三、基于Docker搭建Maven私服

下载一个nexus3的镜像

docker pull sonatype/nexus3

将容器内部/var/nexus-data挂载到主机/root/nexus-data目录。

docker run -d -p 8081:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3

访问http://ip:8081 

Maven私服启动容器稍微比较慢,等待1分钟即可。

默认登陆账号 admin admin123

4、创建Maven私服仓库

创建私服仓库

创建仓库,点击Create repository,然后选择maven2(hosted)然后输入仓库名称(test-release)。在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要).

创建私服账号

点击左侧菜单栏的Users菜单,然后点击Create local user.我这里创建了一个用户,账号密码都是:liwc

 liwc-release

本地settings.xml

<servers>
      <server>
        <id>haha</id>
        <username>liwc</username>
        <password>liwc</password>
      </server>
  </servers>

创建一个Maven工程

创建一个maven工程,并且打包到maven私服。

相关配置

<!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE -->
      <!--指定仓库地址 -->
      <distributionManagement>
            <repository>
                  <!--此名称要和.m2/settings.xml中设置的ID一致 -->
                  <id>haha</id>
                  <url>http://xxxx/repository/liwc-release/</url>
            </repository>
      </distributionManagement>
      <build>
            <plugins>
                  <!--发布代码Jar插件 -->
                  <plugin>
                       <groupId>org.apache.maven.plugins</groupId>
                       <artifactId>maven-deploy-plugin</artifactId>
                       <version>2.7</version>
                  </plugin>
                  <!--发布源码插件 -->
                  <plugin>
                       <groupId>org.apache.maven.plugins</groupId>
                       <artifactId>maven-source-plugin</artifactId>
                       <version>2.2.1</version>
                       <executions>
                             <execution>
                                   <phase>package</phase>
                                   <goals>
                                         <goal>jar</goal>
                                   </goals>
                             </execution>
                       </executions>
                  </plugin>
            </plugins>
      </build>
mvn deploy

测试依赖信息

<dependencies>
            <dependency>
                  <groupId>com.test</groupId>
                  <artifactId>test_springboot</artifactId>
                  <version>0.0.1-RELEASE</version>
            </dependency>
      </dependencies>
      <repositories>
            <repository>
                  <id>haha</id>
                  <url>http://xxxx/repository/liwc-release/</url>
            </repository>
      </repositories>

如何判断文件是否发生改变

如何知道一个文件是否改变了呢?当然是用比较文件hash值的方法,文件hash又叫文件签名,文件中哪怕一个bit位被改变了,文件hash就会不同。比较常用的文件hash算法有MD5和SHA-1。