Maven私服搭建——nexus ╰半橙微兮° 2022-07-15 06:15 197阅读 0赞 ## 1、下载nexus ## https://www.sonatype.com/download-oss-sonatype bundle自带了jetty容器 下载其中一个,解压后内容是一样的,tar.gz下载更快,因为压缩比更高。 ![20161105163551157][] ## 2、安装 nexus ## ### 2.1解压压缩包 ### ![20161105164414549][] 得到 ![20161105164452190][] ![20161105164647692][] ### 2,2安装nexus ### 由于下载的版本bundle自带jetty容器,故不需要额外的配置就可以启动。 在windows环境安装 找到路径:D:\\nexus2.8\\nexus-2.8.1-01\\bin\\jsw\\windows-x86-32 事实上,你需要先安装好jdk,你的jdk是32位的就选择windows-x86-32,你的jdk是64位的就选择windows-x86-64 可以参考:[JDK安装和JAVA环境变量的设置][JDK_JAVA] ![20161105201804439][] 然后,先尝试一下环境是否正常: 点击 console-nexus.bat 如果没有报错,访问 [http://localhost:8081/nexus/\#welcome][http_localhost_8081_nexus_welcome] 8081端口是默认的,如果要修改,打开路径 D:\\nexus2.8\\nexus-2.8.1-01\\conf 下的nexus.properties 文件进行修改 ![20161105202446083][] 点击右上角的 Log In 登陆, 默认登陆用户名:admin,密码:admin123 ## 3、nexus 仓库分类 ## maven会向nexus请求jar包,nexus可以有多个仓库,每个仓库都有一个地址,maven的配置文件可以像从maven的中央仓库请求资源一样从nexus请求资源。 maven的中央仓库缺少一些特定版权的jar包,本地的正在开发的jar包,nexus则可以进行对这些归类管理,比如apache的,Oracle jdbc驱动的 ,包括本地项目等。 nexus即可以建立多个仓库,也可以把已存的仓库组合起来作为一个仓库。 分类示意图如下: ![20161106134248240][] ## 4、maven的依赖范围(scope) ## ![20161106134041583][] 当存在依赖关系时 ![20161106134151536][] ## 5、maven的快照(SNAPSHOT)版本和发布(release)版本 ## 当项目处于开发阶段,版本要被声明为类似于 2.1-SNAPSHOT的模式,当该项目版本稳定则可将版本写为 2.1 开发阶段的版本不应对外发布,发布版本的不应依赖快照(SNAPSHOT)版本。 此外,maven还有一个 最新版本,即latest,这个是取最新的版本,包含快照版本和发布版本。 ## 6、maven镜像 ## 如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都能够从它的镜像中获取。 比如,http://maven.net.cn/content/groups/public/是中央仓库http://repo1.maven.org/maven2/在中国的镜像,同样,我们也可以使用私服来作为镜像。 所以,目前就是这样的结构,nexus负责组织各种仓库作为私服,对外提供访问地址,然后在maven的配置文件settings.xml中配置<mirror> <mirror> <id>internal-respository</id> <name>Internal Repository manager</name> <url>http://localhost:8081/maven2/</url> <mirrorOf>*</mirrorOf> </mirror> ![20161106133705691][] ## 7、在neuxs中新建宿主仓库, ## ![20161105220320677][] 双击新建宿主仓库,在下面面板填写配置信息 ![20161105220531352][] ## 8、新建代理仓库 ## ![20161106135439823][] 代理仓库的背后是远程仓库。 ![20161106135717528][] ## 9、创建仓库组 ## ![20161106135821294][] 然后选择已经配置好的宿主仓库和代理仓库 ![20161106140008713][] ### 9.1、解决国内访问maven中央仓库速度慢的问题-将aliyun的maven镜像作为一个代理仓库添加到仓库组 ### 在国内访问中央仓库时,有时会遇到速度比较慢的情况,而且有时候最终请求不到,尽管等待时间是可以设置的。 为此,我们可以讲aliyun的仓库镜像以代理仓库的形式添加到仓库组,并且最为第一的位置,最后将我们项目的请求设置为请求我们的私服即可。 首先是为nexus添加代理仓库 ![20170116104922065][] 其中所有的设置可以参考Central(即nexus自带的中央镜像),唯一不同的是名字和地址 ![20170116105016457][] 地址为 http://maven.aliyun.com/nexus/content/groups/public/ <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> ![20170116105038192][] 然后是为仓库组增加这个aliyunmaven镜像,保存就ok了 ![20170116105053020][] ## 10、在maven中配置nexus ## ### 10.1 在maven项目的pom.xml文件中配置远程仓库(或者私服),maven默认的中央仓库id为central,如果重写,会直接覆盖。 ### 依赖的远程仓库配置 <repositories> <repository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> ![20161106153341774][] 以及插件的远程仓库配置 <pluginRepositories> <pluginRepository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </pluginRepository> </pluginRepositories> ### 10.2 如果访问私服需要设置权限 ### 则在maven的settings.xml中配置neuxs的用户名和密码 <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> ### 10.3 将本地项目部署到远程仓库(或者私服),供团队其他成员使用 ### 编写完毕后,运行 mvn clean deploy 就会部署到私服,具体操作请看目录11,mynexus是有含义的,在setting.xml文件中配置了nexus的用户名和密码 <distributionManagement> <repository> <id>mynexus</id> <name>Releases</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>mynexus</id> <name>Snapshots</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> 需要注意的是,nexus的release模式默认不允许重复上传,注意修改为如下所示的值 ![20170613235439551][] ### 10.4 手动部署 ### ![20161106162747005][] ## 10.5 maven在settings.xml文件中配置nexus ## <profiles> <!--配置mynexus--> <profile> <id>mynexus</id> <repositories> <repository> <id>public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> 然后是激活我们的nexus配置,同样是在maven的settings.xml文件中 <activeProfiles> <activeProfile>mynexus</activeProfile> </activeProfiles> ### 10.6 如果仅仅是10.5,maven还是会去时不时的请求中央仓库 ### 在settings.xml中增加如下配置即可使maven的所有的请求都被nexus拦截。 <mirrors> <mirror> <id>mynexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> </mirrors> ## 11、一个实际maven项目的私服配置文件 ## ### 11.1当安装好nexus后,运行console-nexus.bat ### 访问 http://localhost:8081/nexus/\#welcome 登陆 admin/admin123 之后选择Repositories 右边显示的7个仓库是nexus自带的,右边的链接是maven项目访问私服的具体链接 ![20161106213827559][] 点击开 Public Repositories,可以看到这个仓库组包含了其他仓库 ![20161106214335873][] ### 11.2、我们的maven项目的配置文件会直接显示的表达出和其中的3个仓库(组)打交道 ### 11.2.1 Repository的名字为 Public repositories,其作为替代maven中央仓库和其他仓库的和作为私服的对外接口, 其配置的位置位于 maven的settings.xml文件,尤其注意其id的值,不单在settings。xml中保持一致,在maven项目的pom.xml文件中的部署配置中 也要保持id值与之一致 (1)maven的settings.xml声明nexus的仓库组,这里id自定义为 mynexus 注意里面的url路径,是 http://localhost:8081/nexus/content/groups/public/ 这是Public repositories <profiles> <!--配置mynexus--> <profile> <id>mynexus</id> <repositories> <repository> <id>public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> (2)maven的settings.xml激活仓库组,注意这里id自定义为 mynexus,与上面的声明一致 <!--激活 mynexus--> <activeProfiles> <activeProfile>mynexus</activeProfile> </activeProfiles> (3) maven的settings.xml 声明的仓库组拦截一切maven的jar包请求(不再访问maven的默认的central即中央仓库) 注意这里id自定义为 mynexus,与上面的声明一致 <mirrors> <mirror> <id>mynexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public/</url> </mirror> </mirrors> (4)这是权限,nexus有多种权限,其中用户名为admin的具有所有权限,默认密码为admin123 <servers> <server> <id>mynexus</id> <username>admin</username> <password>admin123</password> </server> </servers> 11.2.2 Releases 是发布版本,当maven项目开发为稳定版本时,可以部署到这个仓库 11.2.3 Snapshots 是不稳定版本,如果上传可以选择上传到私服的这个仓库 11.2.4 决定上传时版本状态的是maven项目的pom.xml文件的<version></version>标签 ![20161106215959367][] 注意,不稳定版本有-SNAPSHOT后缀 11.2.5 maven的pom.xml部署到私服的配置 当项目需要部署到私服时,需要上传,在下面的配置文件之后,运行 mvn clean deploy命令即可 注意id为mynexus,和settings.xml文件中的一致 <distributionManagement> <repository> <id>mynexus</id> <name>Releases</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>mynexus</id> <name>Snapshots</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> 运行deploy命令后,我们可以到nexus的仓库中查看 ![20161106220343883][] 是一个jar包了 ![20161106220437164][] 而我的项目结构为 ![20161106220506977][] ## 12 附属可能遇到的报错,这个是在eclipse中操作的,jdk1.7 ## [在eclipse中使用m2e构建maven项目-可能遇到的问题][eclipse_m2e_maven_-] ## 新增nexus的最佳实践 ## 由于国内网络的问题,访问maven中央仓库会比较慢 所以可以使用阿里提供的私服 需要的配置是, 1、你的maven指向nexus的public Repositories 2、新增阿里的maven私服 3、然后把nexus中默认的包含的私服都去掉,新增2中的阿里Maven私服 对于2 ![20170315213222618][] 然后 ![20170315213525314][] 下面的内容 ![20170315213631550][] 涉及到的字符 aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ 然后是修改 Public Repositories,如下图 ![20170315213807513][] 此外,因为本地上传到的地址是 http://localhost:8081/nexus/content/repositories/releases/ 和 http://localhost:8081/nexus/content/repositories/snapshots/ 所以,group还应该包含 Releases和Snapshots 如下图 ![20170613235224439][] [20161105163551157]: /images/20220715/645d675cd4bc4a21b4361fa6d2b31347.png [20161105164414549]: /images/20220715/b46dcbf66ac246e6ac14f94ea1a5f4fd.png [20161105164452190]: /images/20220715/b8ba89bcff4c44658117b6b1e6dd1cad.png [20161105164647692]: /images/20220715/2883c963167045c093cf49b820600722.png [JDK_JAVA]: http://blog.csdn.net/bestcxx/article/details/52300559 [20161105201804439]: /images/20220715/2156b5e02f934a51ae4e7a982006b76a.png [http_localhost_8081_nexus_welcome]: http://localhost:8081/nexus/#welcome [20161105202446083]: /images/20220715/247d07eb95fd42dc80846fc42b8e77a5.png [20161106134248240]: /images/20220715/940ac57c3d3a4e218fecfd36b6ea9646.png [20161106134041583]: /images/20220715/a5356c01226f439dab9353a8ea7f93f5.png [20161106134151536]: /images/20220715/723b99ad1b3942aebbfc7c249433502d.png [20161106133705691]: /images/20220715/e2eff7dedcd842a4a5b2190f7d708136.png [20161105220320677]: /images/20220715/86b9747e8df8457b9cd67b91a974abc0.png [20161105220531352]: /images/20220715/4c8ca07e29114780ae77140649330fe9.png [20161106135439823]: /images/20220715/6a9f66feb26f4d31854f7f9518eadd40.png [20161106135717528]: /images/20220715/95efd18c9f104ef4a2fcbbd72c8036a3.png [20161106135821294]: /images/20220715/7a1c7d54a0904ae4b95d57848d3fd1b1.png [20161106140008713]: /images/20220715/213d2dd231b24d5f82ad40eec2a9abb9.png [20170116104922065]: /images/20220715/fd1477e8c0524f19ab1c9d23b21a4bf5.png [20170116105016457]: /images/20220715/6ce99762e5b542b3ac10538e45f2c585.png [20170116105038192]: /images/20220715/ba373c1f36d14fa2b45a8b69f7c818aa.png [20170116105053020]: /images/20220715/f5c360d9749848ce8ab2b291b46525ff.png [20161106153341774]: /images/20220715/ca45abb4b12d4fd49bd928cefae994fa.png [20170613235439551]: /images/20220715/893ca89d77bf4052930974aabde5d401.png [20161106162747005]: /images/20220715/046d8aee42fb43439f0e70952caa3872.png [20161106213827559]: /images/20220715/d0dec82736cb4f8d88af9da42523f105.png [20161106214335873]: /images/20220715/5088e745ae3442c8a72b4bbd4b1d1e39.png [20161106215959367]: /images/20220715/faa54ab05b734321acc77ed56b106822.png [20161106220343883]: /images/20220715/a0f3df5909c44962ae86160cfb0c5a03.png [20161106220437164]: /images/20220715/8e1fda4ba9dd4ddb9dc6e97fd9e1b89c.png [20161106220506977]: /images/20220715/33938f1130c047618cdd005a6eaaa40a.png [eclipse_m2e_maven_-]: http://blog.csdn.net/bestcxx/article/details/52144766 [20170315213222618]: /images/20220715/e279a74b3d944324b8a3011121445ed5.png [20170315213525314]: /images/20220715/6d0aacc71a2746e39d135c411ba1229e.png [20170315213631550]: /images/20220715/05d4737128e04ad7bc4c15e3d596d603.png [20170315213807513]: /images/20220715/df6ea3fed8074430ad96f55435e2052b.png [20170613235224439]: /images/20220715/13728dd70fff4b6698099727b205db30.png
还没有评论,来说两句吧...