maven工具,搭建私服nexus

root
233
文章
0
评论
2020年2月21日22:35:09 评论 7912字阅读26分22秒

maven项目管理工具

Maven是一个项目管理的综合工具。Maven提供给开发人员构建一个完整的生命周期框架。

开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期

apache的开源项目主要服务与JAVA平台的构建,依赖管理,项目管理

Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件,该文件用于管理:源代码,配置文件,开发者的信息和角色,问题追踪系统,组织信息,项目授权等待,开发维护的,我们运维人员不用关心这些,我们关心

  • 如何安装maven
  • 如何进行打包
  • 如何下载依赖的时候速度快

java的项目想发布到线上必须通过maven

1.下载Maven 3安装包

官网:http://maven.apache.org/download.cgi

清华镜像:https://mirrors.tuna.tsinghua.edu.cn

2.安装maven

[root@nfs01 tools]# ll
total 11220
drwxr-xr-x 6 root root      99 Feb 20 11:56 apache-maven-3.6.3
-rw-r--r-- 1 root root 9506321 Feb 20 11:56 apache-maven-3.6.3-bin.tar.gz

[root@nfs01 tools]# ln -s apache-maven-3.6.3 maven
[root@nfs01 tools]# ll
total 11220
drwxr-xr-x 6 root root      99 Feb 20 11:56 apache-maven-3.6.3
-rw-r--r-- 1 root root 9506321 Feb 20 11:56 apache-maven-3.6.3-bin.tar.gz
drwxr-xr-x 2 root root      79 Feb 13 23:41 jenkins
lrwxrwxrwx 1 root root      18 Feb 20 11:58 maven -> apache-maven-3.6.3
drwxr-xr-x 2 root root      99 Oct 29 17:31 sersync-master
-rw-r--r-- 1 root root 1981010 Oct 29 17:09 sersync-master.zip

3.把maven命令放入环境变量

/etc/profile

export PATH=/server/tools/maven/bin/:$PATH

[root@nfs01 /]# source /etc/profile

[root@nfs01 /]# mvn
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.078 s
[INFO] Finished at: 2020-02-20T12:09:35+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

maven命令

 

[root@nfs01 lib]# cd /server/tools/maven/lib/
[root@nfs01 lib]# jar xf maven-model-builder-3.6.3.jar 

 

在项目目录 

mvn package------------打包(回去maven的中央仓库下载需要的依赖包和插件放到.m2目录内)

mvn clean-----------清除包

先清除后打包

创建maven私服nexus

Nexus是一个强大的Maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问。
如果使用了公共的Maven仓库服务器,可以从Maven中央仓库下载所需要的构件(Artifact),但这通常不是一个好的做法。
正常做法是在本地架设一个Maven仓库服务器,即利用Nexus私服可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。
Nexus在代理远程仓库的同时维护本地仓库,以降低中央仓库的负荷,节省外网带宽和时间,Nexus私服就可以满足这样的需要。
Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。
Nexus使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。
Nexus支持WebDAV与LDAP安全身份认证。
Nexus还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。

为什么要构建Nexus私服?

如果没有Nexus私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的Maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载和浪费了外网带宽,如果网速慢的话,还会影响项目的进程。很多情况下项目的开发都是在内网进行的,连接不到maven仓库怎么办呢?开发的公共构件怎么让其它项目使用?这个时候我们不得不为自己的团队搭建属于自己的maven私服,这样既节省了网络带宽也会加速项目搭建的进程,当然前提条件就是你的私服中拥有项目所需的所有构件。

本地构建nexus私服的好处有:

  • 1)加速构建;
  • 2)节省带宽;
  • 3)节省中央maven仓库的带宽;
  • 4)稳定(应付一旦中央服务器出问题的情况);
  • 5)控制和审计;
  • 6)能够部署第三方构件;
  • 7)可以建立本地内部仓库;
  • 8)可以建立公共仓库

这些优点使得Nexus日趋成为最流行的Maven仓库管理器。

 

部署事项:

需要单独服务器部署私服nexus 

服务器IP:192.168.5.41

下载JDK:https://www.oracle.com/java/technologies/javase-downloads.html

nexus下载:https://www.sonatype.com/download-oss-sonatype

https://www.sonatype.com/download-nexus-repo-oss?submissionGuid=d00ed769-8287-4aa0-b847-4e6116a15433

nexus服务器上操作

配置仓库两个选项

  • 项目下的pom.xml配置,只生效当前项目
  • 在maven配置全局所有项目生效

上传JDK和nexus安装包 

[root@backup ~]# ll
total 317308
-rw-rwx---+ 1 root root      1764 Sep  3 08:28 anaconda-ks.cfg
-rw-rwx---+ 1 root root   5222400 Sep 25 02:30 core.1946
-rw-------  1 root root      3399 Oct 24 20:55 dead.letter
drwxrwxr-x+ 2 root root         6 Sep 25 02:30 Desktop
drwxrwxr-x+ 2 root root         6 Sep 25 02:30 Documents
drwxrwxr-x+ 2 root root         6 Sep 25 02:30 Downloads
-rw-rwxr--+ 1 root root        28 Sep 14 13:33 he.mysql.sh
-rw-rwxr--+ 1 root root       285 Sep 10 23:04 he.sh
-rw-rwxr--+ 1 root root      1812 Sep  3 08:32 initial-setup-ks.cfg
-rw-r--r--  1 root root 178939634 Feb 20 12:58 jdk-8u241-linux-x64.rpm
drwxrwxr-x+ 2 root root         6 Sep 25 02:30 Music
-rw-r--r--  1 root root 140732562 Feb 20 16:10 nexus-3.21.1-01-unix.tar.gz

rpm -ivh jdk-8u241-linux-x64.rpm

tar xf nexus-3.21.1-01-unix.tar.gz

做个软链接

[root@backup ~]# mv nexus-3.21.1-01 /server/tools/
[root@backup ~]# cd /server/tools/
[root@backup tools]# ln -s nexus-3.21.1-01/ nexus
[root@backup tools]# ll
total 0
lrwxrwxrwx 1 root root  16 Feb 20 16:35 nexus -> nexus-3.21.1-01/
drwxr-xr-x 9 root root 163 Feb 20 16:33 nexus-3.21.1-01

进入到nexus/bin/目录执行./nexus查看可以执行的命令

[root@backup bin]# ./nexus
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}

启动nexus

[root@backup bin]# ./nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus

现在登陆nexus的web页面进行查看

192.168.5.41:8081

右上角登录admin账户 发现之前说的默认密码admin/admin123登录不上了!!!!!!!!!
各种查询资料才知道3.17版本密码改成随即的了,而且登录时候提示密码在/nexus-data/admin.password里

找一下这个文件

[root@backup bin]# find / -type f -name "admin.password"
/server/tools/sonatype-work/nexus3/admin.password
[root@backup bin]# cat /server/tools/sonatype-work/nexus3/admin.password
c2ae12e6-a5c2-4d83-b3ec-5f8d3f362f6f

进入就可以改密码啦

不给匿名用户权限

在nexus上把中央仓库地址改为阿里云的

Maven用到的Repositories说明:

1)maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar (改阿里云
2)maven-releases:私库发行版jar
3)maven-snapshots:私库快照(调试版本)jar
4)maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。

jenkins服务器上操作

编辑/maven/conf/settings.xml,还是在132行的下面面

找到<servers>标签,添加Nexus默认认证信息:

    <server>
      <id>my-nexus-releases</id>
      <username>admin</username>
      <password>30772818</password>
    </server>
    <server>
      <id>my-nexus-snapshot</id>
      <username>admin</username>
      <password>30772818</password>
    </server>

编辑/maven/conf/settings.xml,在158行的下面面(没改前)

添加<server>标签后,在168行的下面面

找到<mirrors>标签,添加镜像信息:

    <mirror>
       <id>nexus</id>
       <mirrorOf>*</mirrorOf>
        <name>Nexus maven</name>
       <url>http://192.168.5.41:8081/repository/maven-public/</url>
    </mirror>

编辑/maven/conf/settings.xml,在261行的下面(修改后)

找到<profile>标签,添加仓库信息:

url与nexus服务器一致

   <profile>
         <id>central</id>
          <repositories>
              <repositories>
                 <id>central</id>
                  <url>http://192.168.5.41:8081/repository/maven-public/</url>
                  <releases><enabled>true</enabled></releases>
                  <snapshots><enabled>true</enabled></snapshots>
              </repositories>
         </repositories>
   <pluginRepositories>
     <pluginRepository>
       <id>central</id>
       <url>http://192.168.5.41:8081/repository/maven-public/</url>
       <releases><enabled>true</enabled></releases>
       <snapshots><enabled>true</enabled></snapshots>
      </pluginRepository>
    </pluginRepositories>
   </profile>

编辑/maven/conf/settings.xml,在倒数第二行下面

激活仓库:

<activeProfiles>
  <activeProfile>nexus</activeProfile>
</activeProfiles>

 

配置完成,重新启动nexus服务

报错:

一:

[WARNING] The requested profile "nexus" could not be activated because it does not exist.

解决方法:

在pom.xml中添加如下配置

<profiles>
        <profile>
            <id>nexus</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
</profiles>

 

maven配置阿里云maven nexus

在jenkins上

需要单独准备一台服务器配置nexus

备份/maven/conf/settings.xml

[root@nfs01 conf]# cp -a settings.xml settings.xml.bak
[root@nfs01 conf]# ll
total 28
drwxr-xr-x 2 root root    37 Nov  7 20:32 logging
-rw-r--r-- 1 root root 10468 Nov  7 20:32 settings.xml
-rw-r--r-- 1 root root 10468 Nov  7 20:32 settings.xml.bak
-rw-r--r-- 1 root root  3747 Nov  7 20:32 toolchains.xml

将settings.xml改为阿里云的maven

158行的下面添加

    <mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>*</mirrorOf>
      <name>Nexus maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>

这个才是解决方法

152     <mirror>$
153       <id>mirrorId</id>$
154       <mirrorOf>repositoryId</mirrorOf>$
155       <name>Human Readable Name for this Mirror.</name>$
156       <url>http://my.repository.com/repo/path</url>$
157     </mirror>$
158      -->$
159     <mirror>      $
160       <id>nexus-aliyun</id>      $
161       <mirrorOf>*</mirrorOf>        $
162       <name>Nexus maven</name>      $
163       <url>http://maven.aliyun.com/nexus/content/groups/public</url>      $
164     </mirror>$
165   </mirrors>$

注意:6个空格引发的惨案

添加好以后报错,mvn package进行编译

解决方案

 

 

继续阅读
历史上的今天
2月
21
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年2月21日22:35:09
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Jenkins的Pipeline项目 Devops

Jenkins的Pipeline项目

Pipeline项目 CI/CD持续集成/持续部署 持续集成是一种软件开发实践,团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次,也就意味着每天可能发生多次集成。每次集成都通过自动化的构建...
jeesns项目代码部署and自动上线 Devops

jeesns项目代码部署and自动上线

jeesns项目代码自动上线 经过上一次失败的教训,这一次通过学习了maven项目管理工具,肯定能部署成功的。失败案例jeesns项目上线(失败案例),这对java项目的部署是一次很好的学习 Jenk...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: