Ubuntu搭建Nexus私服

下载安装

网络好的好可以从官网下载: https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud

如果访问不了官网,可以从csdn的资源进行下载:

https://download.csdn.net/download/haveqing/12281011

https://download.csdn.net/download/hgtocsdn/12106646

这里下载的是nexus-3.20.1-01-unix.tar.gz

首先在用户目录下(或者任何你想要放置的位置)创建一个nexus文件夹, 并将下载的nexus-3.20.1-01-unix.tar.gz复制到该文件夹,然后进行解压:

$ cd ~
$ mkdir nexus
$ cd nexus
$ tar -zxvf nexus-3.20.1-01-unix.tar.gz

解压完成后会生成nexus-3.20.1-01 和 sonatype-work两个目录, neuxs的可执行文件存放在nexus-3.20.1-01/bin/nexus目录下,在/usr/bin 目录下创建nexus可执行文件的软连接:

$cd /usr/bin
$sudo ln -s ~/nexus/nexus-3.20.1-01/bin/nexus nexus

nexus的默认启动端口号为8081,为了避免与其他服务的端口号冲突,可以在启动前修改一下端口号,nexus3的配置文件在nexus-3.20.1-01/etc/nexus-default.properties, 将端口号修改为8060:  application-port=8060

$ vim ~/nexus/nexus-3.20.1-01/etc/nexus-default.properties

修改/etc/profile文件,在文件末尾添加:export RUN_AS_USER=root,执行source /etx/profile使改动生效.这步操作的目的是为了避免启动时出现以下warning信息(这个warning信息并不会影响nexus的启动和使用):

****************************************
WARNING – NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
$ sudo vim /etc/profile
$ source /etc/profile

Ubuntu搭建Nexus私服_第1张图片

启动nexus: 由于配置了软连接,在任意目录下可以启动nexus server

$ nexus start

启动成功后,在浏览器输入 http://localhost:8060/ 即可访问nexus服务器:

Ubuntu搭建Nexus私服_第2张图片

点击左侧菜单栏的Browse即可查看nexus的仓库信息

Ubuntu搭建Nexus私服_第3张图片

Nexus server配置

点击右上角的Sign in可以进行登录,默认的用户名是admin,初始化密码存放在解压的sonatype-work/nexus3/admin.password文件中,使用该密码进行登录.

Ubuntu搭建Nexus私服_第4张图片

登录成功后会提示对密码进行修改:

Ubuntu搭建Nexus私服_第5张图片

修改密码成功后点击Next对匿名用户的访问进行配置,这里不允许匿名用户访问:

Ubuntu搭建Nexus私服_第6张图片

最后点击Next完成配置.

Maven settings配置

修改maven的settings.xml文件,该文件位于maven安装目录的conf目录下:

$ vim conf/settings.xml

在servers的tag中配置nexus server的登录用户信息, username和password即为nexus server登录的用户名和密码

  
   
      releases
      admin
      ****
    
    
      snapshots
      admin
      ****
    
  

发布本地项目的package到私服

修改本地项目的pom.xml文件,添加distributionManagement,repository的URL可以从nexus server上进行查看,打开nexus的browser页面:http://localhost:8060/#browse/browse,点击copy按钮即可查看:

Ubuntu搭建Nexus私服_第7张图片

url与nexus server上的release和snapshot的仓库的url相对应:

   
        
            releases
            User Project Release
            http://localhost:8060/repository/maven-releases/
        

        
            snapshots
            User Project SNAPSHOTS
            http://localhost:8060/repository/maven-snapshots/
        
    

在本地项目的根目录下执行 mvn deploy 命令将当前的项目打包发布到nexus server: -Dmaven.test.skip=true参数是为了跳过测试

$ mvn deploy -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< com.jessica:jenkins-deploy-test >-------------------
[INFO] Building jenkins-deploy-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ jenkins-deploy-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jenkins-deploy-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ jenkins-deploy-test ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ jenkins-deploy-test ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ jenkins-deploy-test ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.2.3:war (default-war) @ jenkins-deploy-test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [jenkins-deploy-test] in [/home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test]
[INFO] Processing war project
[INFO] Webapp assembled in [456 msecs]
[INFO] Building war: /home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test.war
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ jenkins-deploy-test ---
[INFO] Installing /home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test.war to /home/xin/.m2/repository/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-SNAPSHOT.war
[INFO] Installing /home/xin/IdeaWorkspace/jenkins-deploy-test/pom.xml to /home/xin/.m2/repository/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ jenkins-deploy-test ---
Downloading from snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/maven-metadata.xml
Uploading to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-20200604.014023-1.war
Uploaded to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-20200604.014023-1.war (22 MB at 8.7 MB/s)
Uploading to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-20200604.014023-1.pom
Uploaded to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/jenkins-deploy-test-0.0.1-20200604.014023-1.pom (2.0 kB at 10 kB/s)
Downloading from snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/maven-metadata.xml
Uploading to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/0.0.1-SNAPSHOT/maven-metadata.xml (780 B at 3.9 kB/s)
Uploading to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/maven-metadata.xml
Uploaded to snapshots: http://localhost:8060/repository/maven-snapshots/com/jessica/jenkins-deploy-test/maven-metadata.xml (290 B at 2.4 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.813 s
[INFO] Finished at: 2020-06-04T09:40:26+08:00
[INFO] ------------------------------------------------------------------------

如果当前项目的版本号是以SNAPSHAOT结尾的,则jar包会发布到私服的snapshot仓库, 否则会发布到release仓库,可以使用versions-maven-plugin插件对项目的版本进行管理.

发布完成后可以在nexus server上查看snapshot仓库,可以看到刚发布的包:

Ubuntu搭建Nexus私服_第8张图片发布指定版本号的jar包

进入本地项目的根目录,运行 mvn versions:set -DnewVersion=0.1 命令对版本号进行修改,该命令会统一修改父模块和所有子模块的版本号:

$mvn versions:set -DnewVersion=0.1
$ ..........
[INFO] 
[INFO] ------------------< com.jessica:jenkins-deploy-test >-------------------
[INFO] Building jenkins-deploy-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:2.7:set (default-cli) @ jenkins-deploy-test ---
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: /home/xin/IdeaWorkspace/jenkins-deploy-test
[INFO] Processing change of com.jessica:jenkins-deploy-test:0.0.1-SNAPSHOT -> 0.1
[INFO] Processing com.jessica:jenkins-deploy-test
[INFO]     Updating project com.jessica:jenkins-deploy-test
[INFO]         from version 0.0.1-SNAPSHOT to 0.1
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.427 s
[INFO] Finished at: 2020-06-04T09:44:24+08:00
[INFO] ------------------------------------------------------------------------

修改成功后再次运行 mvn deploy 命令将当前的项目打包发布到nexus server:

$ mvn deploy -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< com.jessica:jenkins-deploy-test >-------------------
[INFO] Building jenkins-deploy-test 0.1
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ jenkins-deploy-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ jenkins-deploy-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ jenkins-deploy-test ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ jenkins-deploy-test ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ jenkins-deploy-test ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.2.3:war (default-war) @ jenkins-deploy-test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [jenkins-deploy-test] in [/home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test]
[INFO] Processing war project
[INFO] Webapp assembled in [110 msecs]
[INFO] Building war: /home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test.war
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ jenkins-deploy-test ---
[INFO] Installing /home/xin/IdeaWorkspace/jenkins-deploy-test/target/spring-boot-test.war to /home/xin/.m2/repository/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.war
[INFO] Installing /home/xin/IdeaWorkspace/jenkins-deploy-test/pom.xml to /home/xin/.m2/repository/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ jenkins-deploy-test ---
Uploading to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.war
Uploaded to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.war (22 MB at 16 MB/s)
Uploading to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.pom
Uploaded to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/0.1/jenkins-deploy-test-0.1.pom (2.0 kB at 21 kB/s)
Downloading from releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/maven-metadata.xml
Uploading to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/maven-metadata.xml
Uploaded to releases: http://localhost:8060/repository/maven-releases/com/jessica/jenkins-deploy-test/maven-metadata.xml (306 B at 3.5 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.221 s
[INFO] Finished at: 2020-06-04T09:46:42+08:00
[INFO] ------------------------------------------------------------------------

此时package会发布到releases仓库,发布完成后可以在nexus server的release仓库中查看:

Ubuntu搭建Nexus私服_第9张图片

执行mvn versions:set 命令之后会生成一个pom.xml.versionsBackup备份文件,生成该文件的原因是versions:set 命令的-DgenerateBackupPoms参数默认值为true, 该文件可以用于版本回退, 如果没有该文件则无法回退版本号, -DgenerateBackupPoms=true时需要执行mvn versions:commit进行确认,在确认之前可以使用mvn versions:revert进行版本回退.

执行mvn versions:set 命令时,如果使用 -DgenerateBackupPoms=false,则不会生成备份文件,因此无须执行mvn versions:commit进行确认,也无法使用mvn versions:revert进行版本回退.

回退版本号

可以使用mvn versions:revert 命令对版本进行回退

$ mvn versions:revert

确认修改过的版本号

可以使用mvn versions:commit命令对版本进行确认,此时会将pom.xml.versionsBackup备份文件删除,无法再进行版本回退操作

mvn versions:commit

项目地址

https://github.com/JessicaWin/jenkins-deploy-test

你可能感兴趣的:(Maven,maven,nexus)