将 3rd 方依赖项部署到 Maven 存储库的简单方法
我在一个控制非常严格的组织工作,该组织绝对不允许访问外部存储库。
我想使用 Spring &我们的应用程序采用 Hibernate 框架,并使用 Maven 来解决依赖关系并构建项目。
我已经在本地服务器上设置了一个 Nexus“第三方”存储库,现在是实际添加库的部分。 Hibernate 3.6.7.Final 附带以下 jar:
./hibernate-testing.jar
./hibernate3.jar
./lib/required/commons-collections-3.1.jar
./lib/required/dom4j-1.6.1.jar
./lib/required/jta-1.1.jar
./lib/required/slf4j-api-1.6.1.jar
./lib/required/antlr-2.7.6.jar
./lib/required/javassist-3.12.0.GA.jar
./lib/jpa/hibernate-jpa-2.0-api-1.0.1.Final.jar
./lib/optional/c3p0/c3p0-0.9.1.jar
./lib/optional/swarmcache/swarmcache-1.0RC2.jar
./lib/optional/proxool/proxool-0.8.3.jar
./lib/optional/jbosscache/jbosscache-core-3.2.1.GA.jar
./lib/optional/oscache/oscache-2.1.jar
./lib/optional/infinispan/infinispan-core-4.2.1.CR1.jar
./lib/bytecode/javassist/javassist-3.12.0.GA.jar
./lib/bytecode/cglib/cglib-2.2.jar
Spring 3.0.6.RELEASE 附带以下 jar:
org.springframework.aop-3.0.6.RELEASE.jar
org.springframework.jdbc-3.0.6.RELEASE.jar
org.springframework.asm-3.0.6.RELEASE.jar
org.springframework.jms-3.0.6.RELEASE.jar
org.springframework.aspects-3.0.6.RELEASE.jar
org.springframework.orm-3.0.6.RELEASE.jar
org.springframework.beans-3.0.6.RELEASE.jar
org.springframework.oxm-3.0.6.RELEASE.jar
org.springframework.context-3.0.6.RELEASE.jar
org.springframework.test-3.0.6.RELEASE.jar
org.springframework.context.support-3.0.6.RELEASE.jar
org.springframework.transaction-3.0.6.RELEASE.jar
org.springframework.core-3.0.6.RELEASE.jar
org.springframework.web-3.0.6.RELEASE.jar
org.springframework.expression-3.0.6.RELEASE.jar
org.springframework.web.portlet-3.0.6.RELEASE.jar
org.springframework.instrument-3.0.6.RELEASE.jar
org.springframework.web.servlet-3.0.6.RELEASE.jar
org.springframework.instrument.tomcat-3.0.6.RELEASE.jar
org.springframework.web.struts-3.0.6.RELEASE.jar
我知道部署依赖项的 2 种首选方法是
1)设置 pom;使用命令“mvn部署”
2)像这样使用命令行,对于单个jar:
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
-DrepositoryId=some.id \
-Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=test] \
[-DgeneratePom=true] \
[-DgeneratePom.description="My Project Description"] \
[-DrepositoryLayout=legacy] \
[-DuniqueVersion=false]
对于1)我知道我可以为整个hibernate项目创建一个pom,或者创建多个pom,我猜每个外部库1个。例如,cglib-2.2.jar可以是它自己的pom,因为我知道spring有类似的依赖关系,所以为了没有(2)x cglib(s),我有1个cglib,然后将其包含为分别在我的 org.spring 和 org.hibernate poms 中的依赖项。
对于2)我想我可以编写一个shell脚本来读取文件列表并从某个地方的另一个列表中解析出GAV信息并将它们转储到repo 1 by 1,然后每个jar将在我的项目的POM中列为依赖项。
最后一部分是我感到困惑的地方...如果我将整个 Spring 版本部署为 1 个组 - org.Spring 和 Hibernate 在另一个 org.Hibernate 中,依赖项之间是否会发生冲突(例如 cglib)?
所以我想我的问题是:
将下载的 Spring 和 Hibernate 版本部署到我的本地 Nexus 存储库,以便我可以在我的项目中快速使用它们,最简单、最快、最省力的方法是什么? (无需在太多 pom 中列出太多依赖项。)
编辑:
对我想要的内容进行一些澄清。我有 1 个 Nexus 管理器在本地服务器上运行(可以访问互联网)。我希望能够从互联网存储库下载运行 Spring 和 Spring 所需的所有依赖项。冬眠。我只想一次执行此操作,然后将它们放在我的内部存储库 (Nexus) 中,我希望永远不会与中央存储库对话、jboss、spring 或任何其他公共存储库,直到我特别允许。我希望能够像开关一样打开和关闭它。
ON 模式 =“你可以从公共存储库中获取依赖项”
OFF 模式 =“你不得连接到任何外部”
在我看来,这可以通过执行以下操作来完成以下内容:
在 Nexus 中
1) 设置一个组
2) 为您需要的每个外部存储库设置代理
3) 将所有代理放入 1 个组
在开发工作站上 (这是我不确定的部分关于)
1)编辑settings.xml以覆盖中心位置并将存储库指向本地nexus组,如下所述:
和这里
和这里
http://www.sonatype.com/books/ nexus-book/reference/maven-sect-single-group.html
2)运行 mvn install 或compile 或其他什么,以便它获取依赖项。
之后我不太确定它会如何运作。以某种方式告诉 Maven 在我的本地 Nexus 存储库之一中镜像外部组?我想我会尝试一下,如果成功的话会回来发布我的解决方案。在此期间,请随时发表评论或提出建议。
I work for a very tightly controlled organization, which absolutely will not allow access to outside repositories.
I would like to use Spring & Hibernate framework for our applications and use maven to resolve dependencies and build a project.
I've set up a nexus "3rd party" repository on a local server, now comes the part of actually adding libraries. Hibernate 3.6.7.Final comes with the following jars:
./hibernate-testing.jar
./hibernate3.jar
./lib/required/commons-collections-3.1.jar
./lib/required/dom4j-1.6.1.jar
./lib/required/jta-1.1.jar
./lib/required/slf4j-api-1.6.1.jar
./lib/required/antlr-2.7.6.jar
./lib/required/javassist-3.12.0.GA.jar
./lib/jpa/hibernate-jpa-2.0-api-1.0.1.Final.jar
./lib/optional/c3p0/c3p0-0.9.1.jar
./lib/optional/swarmcache/swarmcache-1.0RC2.jar
./lib/optional/proxool/proxool-0.8.3.jar
./lib/optional/jbosscache/jbosscache-core-3.2.1.GA.jar
./lib/optional/oscache/oscache-2.1.jar
./lib/optional/infinispan/infinispan-core-4.2.1.CR1.jar
./lib/bytecode/javassist/javassist-3.12.0.GA.jar
./lib/bytecode/cglib/cglib-2.2.jar
Spring 3.0.6.RELEASE comes with the following:
org.springframework.aop-3.0.6.RELEASE.jar
org.springframework.jdbc-3.0.6.RELEASE.jar
org.springframework.asm-3.0.6.RELEASE.jar
org.springframework.jms-3.0.6.RELEASE.jar
org.springframework.aspects-3.0.6.RELEASE.jar
org.springframework.orm-3.0.6.RELEASE.jar
org.springframework.beans-3.0.6.RELEASE.jar
org.springframework.oxm-3.0.6.RELEASE.jar
org.springframework.context-3.0.6.RELEASE.jar
org.springframework.test-3.0.6.RELEASE.jar
org.springframework.context.support-3.0.6.RELEASE.jar
org.springframework.transaction-3.0.6.RELEASE.jar
org.springframework.core-3.0.6.RELEASE.jar
org.springframework.web-3.0.6.RELEASE.jar
org.springframework.expression-3.0.6.RELEASE.jar
org.springframework.web.portlet-3.0.6.RELEASE.jar
org.springframework.instrument-3.0.6.RELEASE.jar
org.springframework.web.servlet-3.0.6.RELEASE.jar
org.springframework.instrument.tomcat-3.0.6.RELEASE.jar
org.springframework.web.struts-3.0.6.RELEASE.jar
I understand that the 2 preferred methods for deploying dependencies are
1) set up a pom; use command "mvn deploy"
2) use command-line like so, for individual jars:
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
-DrepositoryId=some.id \
-Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=test] \
[-DgeneratePom=true] \
[-DgeneratePom.description="My Project Description"] \
[-DrepositoryLayout=legacy] \
[-DuniqueVersion=false]
For 1) I understand I can either create a single pom for the entire hibernate project, or create multiple poms, I guess 1 for each external library. For example, cglib-2.2.jar can be its own pom, because I know spring has a similar dependency, so for the sake of not having (2)x cglib(s), I'd have 1 cglib and then include it as a dependency in my org.spring and org.hibernate poms, respectively.
For 2) I guess I could write a shell script to read the file list and parse out GAV info from maybe another list somewhere and dump them to repo 1 by 1, then each jar will be listed as dependency in my project's POM.
This last part is what I find confusing... If I deploy entire Spring release as 1 group - org.Spring and Hibernate in another org.Hibernate, could there be collisions between dependencies (ex. cglib)?
So I guess my question is:
What is the easiest, fastest, least effort required way to deploy downloaded Spring and Hibernate releases to my local nexus repository, so that I can use them in my project quickly? (Without having to list too many dependencies in too many poms.)
Edit:
A little clarification on what I want. I have 1 Nexus manager running on a local server (with access to the Internet). I want to be able to download from the Internet repositories all dependencies that I need to run Spring & Hibernate. I want to do this ONLY ONCE, then have them sit in my INTERNAL REPOSITORY (Nexus), which I want to NEVER EVER talk to central, jboss, spring or any other PUBLIC REPOSITORY until I SPECIFICALLY allow it. I want to be able to turn this on and off like a switch.
ON mode = "you can grab dependencies from public repos"
OFF mode = "you must not connect to anything external"
It seems to me like this is accomplish-able by doing the following:
in Nexus
1) Set up a group
2) Set up proxie for each external repository you need
3) Put all of your proxies in 1 group
on the dev workstation (this is part I'm not sure about)
1) edit settings.xml to override central location and point repository to local nexus group as described here:
and here
Disable Maven central repository
and here
http://www.sonatype.com/books/nexus-book/reference/maven-sect-single-group.html
2) run mvn install or compile or whatever, so that it grabs the dependencies.
After that I'm not really sure how it's going to work. Somehow tell maven to mirror the external group in one of my local Nexus repositories? I guess I will try that and come back and post my solution if successful. In the meantime, feel free to comment or advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Nexus 具有采购功能,可以严格控制从外部进入仓库的内容,同时仍然使用来自上游的原始 pom。如果没有它们,您将丢失所有传递依赖信息,并且很可能会破坏您将来的构建或运行时部署。
JFrog Artifactory 有类似的免费功能(可以定义包含/排除模式)。
Nexus has a procurement feature to tightly control what comes in your repo from the outside while still using the original poms from the upstream. Without them you are loosing all transitive dependency information and will most likely break you build or runtime deployment in the future.
JFrog Artifactory has a similar feature (can define includes/exclude patterns) for free.
不确定这是否有帮助,但我们已经使用 codehaus 的 maven-proxy 一段时间了。我建议运行代理,设置
settings.xml
文件(见下文),然后执行mvn eclipse:evlipse
通过缓存下载所有依赖项。您也可以手动填充缓存。然后,您可以调整缓存以专门提供您想要的包,而不是其他包。如果您获得不需要的软件包,则需要在
pom.xml
文件中添加排除项:代理是一个在我们的开发盒上运行的 Java 应用程序。在我们的 settings.xml 文件中,我们有如下内容:
这是我们的启动脚本,我认为该版本不会附带:
Not sure if this helps but we've been using the maven-proxy from codehaus for a while now. I'd suggest running the proxy, setting up your
settings.xml
file (see below), and doing yourmvn eclipse:evlipse
to download all of the dependencies through the cache. You could also fill the cache by hand. You can then tune the cache to specifically serve the packages that you want and nothing else.If you get packages that you don't want then you'll need to add exclusions for them in your
pom.xml
files:The proxy is a java application that is running on one of our dev boxes. In our settings.xml file we have something like the following:
Here's our start script which I don't think comes with the release:
我会为这些项目重用现有的 pom,以便有效地创建公共存储库的镜像。
I'd reuse existing pom's for these projects in order to create effectively a mirror of the public repositories.
想通了。
这是它的工作原理。我将从设置 Nexus 开始。
我们有 2 个“组”,其中包含以下存储库:
1)我们的存储库(我们服务器上的 nexus 安装)
-第 3 方
-Releases
-Snapshots
2)外部代理
-Mavencentral
-Spring
-Hibernate
-等。
在开发工作站上,我在conf/settings.xml(mvn的主要设置文件)中有以下内容:2个组、2个镜像、2个配置文件。看起来像这样:
并且:
并且:
工作流程是这样的。
Maven 按照先到先得的原则使用镜像。也就是说,如果您正在镜像*,那么您首先列出的任何内容(第一个镜像)都是它唯一会检查的内容。所以我所做的是,默认将 Nexus(本地服务器)列为第一个存储库以镜像 *。这样它将被迫仅使用本地存储库中的包。如果包裹丢失怎么办?我告诉 Maven 使用“外部”镜像。我编辑
conf/settings.xml
并将外部作为第一个镜像。这将告诉 maven 从外部存储库获取包并将它们缓存在 Nexus 机器上的存储中。一旦我有了这些包并且我看到构建过程有效,我就会执行以下操作。
1) 使 Nexus 离线。
2)在nexus安装中,找到这个文件夹:
sonatype-work/nexus/storage/
这是nexus缓存存储库包的地方。请注意,它不会按组缓存它们,而是按单个存储库缓存它们。稍后这将很重要。现在我已经从外部代理获得了所需的软件包,我需要仔细检查它们,确保它们全部有效并将它们移动到我在 nexus 上的“第三方”分支。我执行以下操作(您可以编写 shell 脚本来执行此操作):
1) 使 nexus 脱机
2) 对于每个外部代理
cp * 从 sonatype-work/nexus/storage/ 到 from sonatype-work/nexus/storage/thirdparty
3 ) 使 Nexus 联机
4) 在 GUI 中右键单击第三方存储库并执行以下操作:
a) 重建元数据
b) 修复索引
完成。
您现在在本地拥有可用的外部依赖项,因此可以控制对外部工件的访问。将存储库维护的角色委托给团队领导或负责人可能是个好主意。因为第一次项目可能需要中央提供的东西但没有,所以必须有人经历这个过程。
编辑:
关于插件主题需要注意的一件事是,我更改了conf/settings.xml 文件以允许maven 从中心获取插件。所以在两个配置文件中都是这样:
这并不是真正的100%最佳,因为我无法完全控制maven在这里所做的事情,但似乎90%的maven确实依赖于管理复杂的插件依赖关系,所以我想我让它自己管理这方面的事情。
如果有人对如何更好地简化这个过程有建议,我会洗耳恭听。
Figured it out.
Here's how it works. I'll start with Nexus set up.
We have 2 "groups" with the following repositories in them following :
1) Our Repo (nexus installation on our server)
-3rd party
-Releases
-Snapshots
2) External Proxies
-Maven central
-Spring
-Hibernate
-etc.
On the dev workstation I have the following in conf/settings.xml (main settings file for mvn): 2 groups, 2 mirrors, 2 profiles. Looks like so:
And:
And:
The workflow goes like this.
Maven uses mirrors on a first-come-first-serve basis. That is, if you're mirroring *, whatever you list first (first mirror) is the only thing that it will check. So what I do is, I list nexus (local server) as first repo by default to mirror *. That way it will be forced to use only packages in local repositories. What happens if a package is missing? I tell maven to use "external" mirror. I edit
conf/settings.xml
and put external as first mirror. What this will do is tell maven to get packages from external repositories and cache them in storage on nexus machine. Once I have the packages and I see that the build process works, I do the following.
1) Take nexus offline.
2) in nexus installation, find this folder:
sonatype-work/nexus/storage/
This is where nexus caches packages for repositories. Note, it doesn't cache them per group, but per individual repository. This will be important in a second. Now that I've got packages I need from external proxies, I need to go through them, make sure it's all valid and move them to my "3rd parties" branch on nexus. I do the following (you can write a shell script to do this):
1) Bring nexus offline
2) for each external proxie
cp * from sonatype-work/nexus/storage/ to from sonatype-work/nexus/storage/thirdparty
3) Bring nexus online
4) In the GUI rightclick thirdparty repo and do:
a)Rebuild metadata
b)Repair Index
Done.
You now have external dependencies available locally and can thus control access to external artifacts. Probably good idea to delegate role of repository upkeep to a team lead or someone responsible. Because first time a project might require something from central and not have it, someone will have to go through this process.
Edit:
One thing to note on topic of plugins, I changed my the conf/settings.xml file to allow maven to grab plugins from central. So in both profiles do:
This is not really 100% optimal, as I don't have full control over what maven is doing here, but it seems like 90% of maven does depends on managing complicated plugin dependency relationships, so I figure I would let it manage that aspect of things on its own.
If anyone has an advice on how to better streamline this process, I'm all ears.