求助关于Nexus 建本地Maven库的基建安全问题标准库

9713人阅读
Maven(7)
&&&&&&& 使用Maven构建和管理项目是非常享受的一件事,我们可以从下载所需要的构件(artifact),但实际开发中由于种种原因我们需要在架设一个Maven本地代理仓库,如:不方便访问公网、节省带宽和时间、管理自家的共用artifact等等。本地地理仓库是我自己取的名字,为了不与下文的本地仓库想混淆。
&&&&&&& 获取构建的流程如下:用户使用Maven构建项目时,首先是要直接从本地仓库获取的,如果本地仓库没有,它会根据setting.xml的设置去首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。如果setting.xml设置的远程仓库是本地代理仓库,则本地代理仓库先尝试从自己的库中获取,如果没有再从远程仓库(比如中央仓库)下载构件至本地仓库。
&&&&&& 是一个优秀的Maven仓库管理器,还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。本文就使用Nexus搭建一个本地代理仓库。
2、下载和安装
&&&&&& Nexus官方下载地址:,目前最新的版本是2.7.2。
&&&&&& Nexu安装非常容易,因为它内嵌了Jetty,只要有JRE救能直接运行。解压Nexu包会得到两个目录nexus-2.7.2-03和sonatype-work,sonatype-work是默认仓库目录。运行、安装都是使用nexus-2.7.2-03/bin/nexus.bat文件,它的使用方式:
Usage:nexus.bat { console : start : stop : restart : install : uninstall }
&&&&&&&其中console是控制台方式运行,install是以windows
service寄存,uninstall是卸载windows service,start是运行windows&service,stop是停止windows&service,restart是重启windows&service。
&&&&&& Nexus默认端口是8081,可以在nexus-2.7.1-01/conf/nexus.properties中修改,启动后就可以通过地址:&来访问了。界面如下:
3、管理仓库
&&&&&& 管理仓库需要先登录,默认登录用户名/密码是admin/admin123。登录后就可以看到左栏菜单的管理项。
&&&&&& 这里,可以管理仓库,配置Nexus系统,管理任务,管理用户,角色,权限,查看系统的RSS源,管理及查看系统日志,等等。
&&&&& 1、设置Nexus代理上公网
&&&&&& 如果您的本地代理仓库服务器没有公网权限,Nexus为你留了使用代理的路子,点击右边菜单的“Server”,在右边找
&&&&&& 添加你的代理服务器即可。
&&&&&& 2、仓库管理
&&&&&& 点击左边导航栏的Repositories,界面的主面板会显示所有一个所有仓库及仓库组的列表,你会看到它们的Type字段的值有group,hosted,proxy,virtual。这里我们不关心virtual,只介绍下另外三种类型:
hosted,本地代理仓库,通常我们会部署自己的构件到这一类型的仓库。proxy,代理的远程仓库,它们被用来代理远程的公共仓库,如maven中央仓库。group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组。
如何管理、添加等操作,Nexus都写的很清楚了,我就不一一赘述了。
&&&&& 3、修改setting.xml配置Maven的仓库
&&&&&& 后默认的是使用中央仓库,这是为了能让Maven开箱即用。而Maven缺省的本地仓库地址为${user.home}/.m2/repository。也就是说,一个用户会对应的拥有一个本地仓库。你也可以自定义本地仓库的位置,修改${user.home}/.m2/settings.xml。
&&&&&& 首先,需要修改Mirrors:
&!-- mirror | Specifies a repository mirror site to use instead of a given
repository. The repository that | this mirror serves has an ID that matches
the mirrorOf element of this mirror. IDs are used | for inheritance and direct
lookup purposes, and must be unique across the set of mirrors. | --&
&id&nexusc&/id&
&mirrorOf&*&/mirrorOf&
&name&Nexus&/name&
&url&http://localhost:8081/nexus/content/groups/public/&/url&
&/mirrors&
&&&&&&& 在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向本地代理仓库的地址,修改如下:
&id&jdk-1.4&/id&
&activation&
&jdk&1.4&/jdk&
&/activation&
&repositories&
&repository&
&id&nexus&/id&
&name&local private nexus&/name&
&url&http://localhost:8081/nexus/content/groups/public/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/repository&
&/repositories&
&pluginRepositories&
&pluginRepository&
&id&nexus&/id&
&name&local private nexus&/name&
&url&http://localhost:8081/nexus/content/groups/public/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/pluginRepository&
&/pluginRepositories&
&/profile&
&&& 现在,你就可以使用本地仓库构建Maven项目了,踏上愉快的构建之旅!!
4、部署构件至Nexus
&&&&&& 1、通过Nexus UI部署
&&&&&&&有时候有个jar文件你无法从公共Maven仓库找到,但是你能从其它得到这个jar文件(甚至是POM),那么你完全可以将这个文件部署到Nexus中,使其成为标准流程的一部分。步骤如下:点击左边导航栏的&Repository&,在右边的仓库列表中选择一个仓库,如“3rd
Party”,然后在页面下方的tab选择“Artifact Upload Artifact(s)”,你会看到构件上传界面。选择你要上传的构件,并指定POM,(或者手工编写GAV等信息),最后点击Upload,该构件就直接被部署到了Nexus的&3rd Party&仓库中。
&&&&&&& 2、通过Maven部署
&&&&&& 更常见的用例是:团队在开发一个项目的各个模块,为了让自己开发的模块能够快速让其他人使用,你会想要将snapshot版本的构件部署到Maven仓库中,其他人只需要在POM添加一个对于你开发模块的依赖,就能随时拿到最新的snapshot。
以下的pom.xml配置和settings.xml能让你通过Maven自动化部署构件:
&distributionManagement&
&repository&
&id&nexus-releases&/id&
&name&Nexus Release Repository&/name&
&url&http://localhost:8081/nexus/content/repositories/releases/&/url&
&/repository&
&snapshotRepository&
&id&nexus-snapshots&/id&
&name&Nexus Snapshot Repository&/name&
&url&http://localhost:8081/nexus/content/repositories/snapshots/&/url&
&/snapshotRepository&
&/distributionManagement&
&/project&
settings.xml
&settings&
&id&nexus-releases&/id&
&username&admin&/username&
&password&admin123&/password&
&id&nexus-snapshots&/id&
&username&admin&/username&
&password&admin123&/password&
&/servers&
&/settings&
&&&&&&& 这里我们配置所有的snapshot版本构件部署到Nexus的Snapshots仓库中, 所有的release构件部署到Nexus的Releases仓库中。由于部署需要登陆,因为我们在settings.xml中配置对应Repository id的用户名和密码。
&&&&&&& 然后,在项目目录中执行mvn deploy ,你会看到maven将项目构件部署到Nexus中,浏览Nexus对应的仓库,就可以看到刚才部署的构件。当其他人构建其项目时,Maven就会从Nexus寻找依赖并下载。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:971071次
积分:12074
积分:12074
排名:第1242名
原创:144篇
转载:844篇
译文:32篇
评论:133条
(20)(28)(25)(22)(28)(25)(48)(39)(53)(3)(96)(74)(39)(64)(71)(57)(154)(132)(14)(2)(3)(3)(16)(2)(2)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'57445人阅读
Architecture Design(4)
Java(35)
Maven(2)
0.安装环境
windows 7 x86-64
jdk1.6.0_27 32-bit(假定该环境中已经设置了jdk的环境变量)
maven 3.1.1
nexus-2.9.1-02-bundle
1.安装maven3.1.1
1)下载maven
访问maven官方网站,下载maven包。下载链接为:
2)解压下载到的apache-maven-3.1.1-bin.zip到某个目录。(如E:\Eb\Apache Software Foundation\apache-maven-3.1.1)
3)设置环境变量。
第一,添加用户变量M2_HOME(实际应为M3_HOME,懒得改了,下面都用M2_吧),M2_HOME=E:\Eb\Apache Software Foundation\apache-maven-3.1.1
第二,添加用户变量M2,M2=%M2_HOME%\bin
第三,修改用户变量PATH(若没有此变量,则添加一个),PATH=%M2%;%PATH%
4)验证maven是否安装成功。打开命令行,输入mvn -v,显示如下信息即为安装成功。
2.安装nexus
1)下载nexus
访问nexus官方网站,下载nexus-2.9.1-02-bundle.zip。&
2)解压至某个目录(如E:\Eb\nexus-2.9.1-02-bundle)
3)开启jetty服务器。找到E:\Eb\nexus-2.9.1-02-bundle\nexus-2.9.1-02\bin\jsw\windows-x86-32(由于jdk安装的是32位版本,因此执行此目录下的文件)目录,执行console-nexus.bat批处理文件,等待命令行执行到如下情况:
不要关闭命令行窗口。
4)访问nexus管理页面。
打开浏览器,输入http://localhost:8081/nexus/,点击页面右上方的Log in超链接,输入默认用户名密码admin/admin123,登录nexus管理页面。
3.配置nexus
1)登录nexus管理页面,点击左侧的Repositories,显示相关的仓库信息,各个仓库的含义可自行百度之。
2)修改默认的存储路径。
安装nexus后,本地仓库默认的jar包存储位置是file:/E:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus/storage/central。由于Central仓库占用存储较大,我们可以单独设置其存储位置。
首先,将E:/Eb/nexus-2.9.1-02-bundle/sonatype-work/目录下的所有内容复制到H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/目录下;
第二,选中Central,打开Configuration选项卡,修改Override Local Storage Location项的值为file:/H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus/storage/central,点击页面下方的save按钮。
3)同步索引。
首先,选中Central,点击下方的Configuration选项卡。修改Download Remote Indexes项的值为True。点击页面下方的save按钮。
第二,右键点击Central,弹出菜单中点击Repair Index。Repair Index过程可能较慢,等待一会儿,进度可以从命令行窗口的日志中看到。
(注意以下问题:
关于Apache Snapshots 仓库无法拉取的问题:
nexus默认配置的是如下路径:
http://repository.apache.org/snapshots/
然后更新索引的时候日志中会提示:
jvm 1 & &|
19:23:34 INFO &[pxpool-1-thread-3] admin org.sonatype.nexus.index.DefaultIndexerManager - Cannot fetch remote index for repository &Apache Snapshots& [id=apache-snapshots] as it does not publish indexes.
正确的地址是如下这个:
https://repository.apache.org/content/repositories/releases/ &release版本
https://repository.apache.org/content/repositories/snapshots/ &非稳定版本
请在仓库配置中更新地址即可。--参照连接)
修改完成后的配置如下图:
4)Apache Snapshots和Codehaus&Snapshots两个仓库也按照上述1)2)3)步骤修改,只是Override Local Storage Location项分别为file:/H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus/storage/apache-snapshots和file:/H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus/storage/codehaus-snapshots
注:还有一个更彻底的方式,直接修改E:\Eb\nexus-2.9.1-02-bundle\nexus-2.9.1-02\conf\nexus.properties中的配置项nexus-work如下:
# Nexus section
#nexus-work=${bundleBasedir}/../sonatype-work/nexus
nexus-work=H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF
4.配置maven使用nexus
1)在maven安装目录中找到maven的全局配置文件。E:\Eb\Apache Software Foundation\apache-maven-3.1.1\conf目录下的settings.xml文件。
2)将settings.xml修改为如下配置:
&?xml version=&1.0& encoding=&UTF-8&?&
&settings xmlns=&http://maven.apache.org/SETTINGS/1.0.0&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xsi:schemaLocation=&http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd&&
&localRepository&E:/.m2/repository&/localRepository&
&pluginGroups&
&/pluginGroups&
&!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
&!-- proxy
| Specification for one proxy, to be used in connecting to the network.
&id&optional&/id&
&active&true&/active&
&protocol&http&/protocol&
&username&proxyuser&/username&
&password&proxypass&/password&
&host&proxy.host.net&/host&
&port&80&/port&
&nonProxyHosts&local.net|&/nonProxyHosts&
&/proxies&
&!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
&/servers&
&!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
&!--This is used to direct the public snapshots repo in the
profile below over to a different nexus group --&
&id&nexus-public-snapshots&/id&
&mirrorOf&public-snapshots&/mirrorOf&
&url&http://localhost:8081/nexus/content/groups/public-snapshots&/url&
&!--This sends everything else to /public --&
&id&nexus&/id&
&mirrorOf&*&/mirrorOf&
&url&http://localhost:8081/nexus/content/groups/public&/url&
&/mirrors&
&profiles&
&id&development&/id&
&repositories&
&repository&
&id&central&/id&
&url&http://central&/url&
&releases&&enabled&true&/enabled&&/releases&
&snapshots&&enabled&true&/enabled&&/snapshots&
&/repository&
&/repositories&
&pluginRepositories&
&pluginRepository&
&id&central&/id&
&url&http://central&/url&
&releases&&enabled&true&/enabled&&/releases&
&snapshots&&enabled&true&/enabled&&/snapshots&
&/pluginRepository&
&/pluginRepositories&
&/profile&
&!--this profile will allow snapshots to be searched when activated--&
&id&public-snapshots&/id&
&repositories&
&repository&
&id&public-snapshots&/id&
&url&http://public-snapshots&/url&
&releases&&enabled&false&/enabled&&/releases&
&snapshots&&enabled&true&/enabled&&/snapshots&
&/repository&
&/repositories&
&pluginRepositories&
&pluginRepository&
&id&public-snapshots&/id&
&url&http://public-snapshots&/url&
&releases&&enabled&false&/enabled&&/releases&
&snapshots&&enabled&true&/enabled&&/snapshots&
&/pluginRepository&
&/pluginRepositories&
&/profile&
&/profiles&
&!-- activeProfiles
| List of profiles that are active for all builds.
&activeProfiles&
&activeProfile&alwaysActiveProfile&/activeProfile&
&activeProfile&anotherAlwaysActiveProfile&/activeProfile&
&/activeProfiles&
&activeProfiles&
&activeProfile&development&/activeProfile&
&/activeProfiles&
&/settings&
3)验证是否设置成功。
打开命令行,执行如下语句:
mvn help:system
之后,查看E:/.m2/repository(&localRepository&E:/.m2/repository&/localRepository&将maven的默认配置改为该目录,默认配置为C:\Users\&用户名&\.m2\repository)和file:/H:/Eb/nexus-2.9.1-02-bundle/sonatype-work/nexus/storage/central两个目录下都有了新下载到的jar包。则表示配置成功。
5.nexus对archetype的支持
参考:/blog/1420634
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1227935次
积分:10257
积分:10257
排名:第1687名
原创:114篇
转载:25篇
译文:23篇
评论:179条
阅读:8265
(1)(1)(2)(1)(2)(1)(3)(1)(1)(1)(2)(2)(1)(2)(4)(1)(1)(3)(4)(2)(4)(1)(4)(16)(17)(4)(3)(8)(13)(8)(6)(11)(8)(19)(3)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'1 . 私服简介
  私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
& & & & & & & & & & & & & & & & & & & & & & & &&
  我们可以使用专门的 Maven 仓库管理软件来搭建私服,比如:,,。这里我们使用 Sonatype Nexus。
2 . 安装Nexus
2 . 1 . 下载Nexus
  Nexus 专业版是需要付费的,这里我们下载开源版 Nexus OSS。Nexus 提供两种安装包,一种是包含 Jetty 容器的 bundle 包,另一种是不包含容器的 war 包。下载地址:。
2 . 2 . 使用bundle安装包安装Nexus
解压安装包nexus-2.8.1-bundle.zip,打开命令提示符,进入/nexus-2.8.1-01目录,键入nexus命令(为方便启动和退出Nexus,可将bin目录添加到环境变量):
执行 nexus install 将Nexus安装为Windows服务。可将服务启动方式设为手动,以后通过 nexus start 即可启动Nexus ,通过 nexus stop 退出Nexus:
打开浏览器,访问:http://localhost:8081/nexus/:
点击右上角 Log In,使用用户名:admin ,密码:admin123 登录,可使用更多功能:
3 . Nexus预置的仓库
点击左侧 Repositories 链接,查看 Nexus 内置的仓库:
Nexus 的仓库分为这么几类:
hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)以及自己或第三方的项目构件;
proxy 代理仓库:代理公共的远程仓库;
virtual 虚拟仓库:用于适配 Maven 1;
group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库。
4 . 添加代理仓库
以 Sonatype 为例,添加一个代理仓库,用于代理 Sonatype 的公共远程仓库。点击菜单 Add - Proxy Repository :
填写Repository ID - sonatype;Repository Name - Sonatype Repository;
Remote Storage Location - http://repository.sonatype.org/content/groups/public/ ,save 保存:
将添加的 Sonatype 代理仓库加入 Public Repositories 仓库组。选中&Public Repositories,在 Configuration 选项卡中,将 Sonatype Repository 从右侧 Available Repositories 移到左侧 Ordered Group Repositories,save 保存:
5 . 搜索构件
为了更好的使用 Nexus 的搜索,我们可以设置所有 proxy 仓库的&Download Remote Indexes 为 true,即允许下载远程仓库索引。
索引下载成功之后,在 Browse Index 选项卡下,可以浏览到所有已被索引的构件信息,包括坐标、格式、Maven 依赖的 xml 代码:
有了索引,我们就可以搜索了:
6 . 配置Maven使用私服
私服搭建成功,我们就可以配置 Maven 使用私服,以后下载构件、部署构件,都通过私服来管理。
在 settings.xml 文件中,为所有仓库配置一个镜像仓库,镜像仓库的地址即私服的地址(这儿我们使用私服公共仓库组 Public Repositories 的地址):
&id&central&/id&
&mirrorOf&*&/mirrorOf& &!-- * 表示让所有仓库使用该镜像--&
&name&central-mirror&/name&
&url&http://localhost:8081/nexus/content/groups/public/&/url&
&/mirrors&
更多高级特性不再介绍(我也还不会)。
阅读(...) 评论()求解关于Maven和Nexus私服索引不完整的问题 - ITeye问答
首先set.xml:
&?xml version="1.0" encoding="UTF-8"?&
&settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"&
&localRepository&/home/pandy/dev/.m2/repository&/localRepository&
&pluginGroups&
&artifactId&tomcat-maven-plugin&/artifactId&
&/pluginGroups&
&/proxies&
&id&nexus&/id&
&username&deployment&/username&
&password&deployment123&/password&
&/servers&
&id&nexus&/id&
&url&http://192.168.0.196:8081/nexus/content/groups/public&/url&
&mirrorOf&*&/mirrorOf&
&/mirrors&
&profiles&
&repositories&
&repository&
&id&nexus&/id&
&url&http://192.168.0.196:8081/nexus/content/groups/public/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/repository&
&/repositories&
&pluginRepositories&
&pluginRepository&
&id&nexus&/id&
&url&http://192.168.0.196:8081/nexus/content/groups/public&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
&enabled&false&/enabled&
&/snapshots&
&/pluginRepository&
&/pluginRepositories&
&/profile&
&/profiles&
&activeProfiles&
&activeProfile&nexus&/activeProfile&
&/activeProfiles&
&/settings&
Nexus的地址是:
我在Nexus里面的Central/Browse Index里面能看见cxf-api的2.7.1版本,但是无法看见2.7.3版本.
然后在,却地功能Maven的官方仓库是可以找到2.7.3版本的,难道我的Nexus得到的信息不是最新的?
请问如何解决这个问题,谢谢.
问题补充:nexus的仓库地址:http://repo1.maven.org/maven2/
采纳的答案
你的私服 建代理了吗 可参考
已解决问题
未解决问题

我要回帖

更多关于 智库建设存在问题 的文章

 

随机推荐