![](/img/trans.png)
[英]maven install and deploy 3rd party dependencies with simple command line
[英]easy way to deploy 3rd party dependencies to a maven repository
我為一個非常嚴格控制的組織工作,絕對不允許訪問外部存儲庫。
我想為我們的應用程序使用Spring和Hibernate框架,並使用maven來解決依賴關系並構建項目。
我在本地服務器上建立了一個nexus“第三方”存儲庫,現在實際上是添加庫的一部分。 Hibernate 3.6.7.Final附帶以下罐子:
./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附帶以下內容:
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
據我所知,部署依賴項的兩種首選方法是
1)設置一個pom; 使用命令“mvn deploy”
2)使用命令行,對於個別罐子:
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個pom。 例如,cglib-2.2.jar可以是它自己的pom,因為我知道spring有一個類似的依賴,所以為了沒有(2)x cglib(s),我有1個cglib然后包含它分別是我的org.spring和org.hibernate poms中的依賴項。
對於2)我想我可以編寫一個shell腳本來讀取文件列表並從某個地方解析出另一個列表中的GAV信息並將它們轉儲到1個1的repo,然后每個jar將在項目的POM中列為依賴項。
最后一部分是我覺得令人困惑的......如果我將整個Spring版本部署為1組 - org.Spring和Hibernate在另一個org.Hibernate中,那么依賴項之間是否會發生沖突(例如cglib)?
所以我想我的問題是:
將下載的Spring和Hibernate版本部署到我的本地nexus存儲庫所需的最簡單,最快速,最省力的方法是什么,以便我可以在項目中快速使用它們? (無需在太多poms中列出太多依賴項。)
編輯:
對我想要的東西稍作澄清。 我有1個Nexus管理器在本地服務器上運行(可以訪問Internet)。 我希望能夠從Internet存儲庫下載運行Spring和Hibernate所需的所有依賴項。 我想這樣做一次 ,然后讓他們坐在我的內部存儲庫(的Nexus),我希望永遠不要跟中央時,JBoss,Spring或其他公共倉庫,直到我的特別允許。 我希望能像開關那樣打開和關閉它。
ON模式=“ 你可以從公共回購中獲取依賴關系”
關閉模式=“你不能連接到任何外部”
在我看來,通過執行以下操作,這是可以實現的:
在Nexus
1)建立一個小組
2)為您需要的每個外部存儲庫設置代理
3)將所有代理放在一個組中
在開發工作站上 (這是我不確定的部分)
1)編輯settings.xml以覆蓋中心位置並將存儲庫指向本地nexus組,如下所述:
和這里
和這里
http://www.sonatype.com/books/nexus-book/reference/maven-sect-single-group.html
2)運行mvn install或compile或者其他什么,以便它抓取依賴項。
在那之后,我不確定它是如何工作的。 以某種方式告訴maven在我當地的一個Nexus存儲庫中鏡像外部組? 我想我會試試並回來並發布我的解決方案,如果成功的話。 在此期間,請隨時評論或建議。
Nexus有一個采購功能,可以從外部嚴格控制您的回購中的內容,同時仍然使用上游的原始poms。 如果沒有它們,您將丟失所有傳遞依賴性信息,並且很可能在將來破壞構建或運行時部署。
JFrog Artifactory具有類似的功能(可以定義包含/排除模式)免費。
不確定這是否有幫助,但我們已經使用codehaus中的maven-proxy了一段時間了。 我建議運行代理,設置settings.xml
文件(見下文),並使用mvn eclipse:evlipse
通過緩存下載所有依賴項。 您也可以手動填充緩存。 然后,您可以調整緩存以專門為所需的包提供服務,而不是其他任何內容。
如果您獲得了不需要的軟件包,那么您需要在pom.xml
文件中為它們添加排除項:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
代理是一個在我們的dev框中運行的java應用程序。 在我們的settings.xml文件中,我們有類似以下內容:
<settings>
<mirrors>
<mirror>
<id>maven-proxy</id>
<name>Maven-Proxy Mirror</name>
<url>http://mvnproxy.some-host-name.com:9999/repository</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>maven-proxy</id>
<name>Maven-Proxy Mirror</name>
<url>http://mvnproxy.some-host-name.com:9999/repository</url>
<mirrorOf>plugins</mirrorOf>
</mirror>
</mirrors>
...
這是我們的啟動腳本,我不認為它與版本一起出現:
#!/bin/sh
umask 022
cd /var/www/maven-proxy
java -jar maven-proxy-standalone-0.2-app.jar maven-proxy.prop >| maven-proxy.log 2>&1 &
echo $! > mp.pid
我會為這些項目重用現有的pom,以便有效地創建公共存儲庫的鏡像。
弄清楚了。
這是它的工作原理。 我將從Nexus設置開始。
我們有2個“組”,其中包含以下存儲庫:
1)我們的回購(在我們的服務器上安裝nexus)
第三方
-Releases
-Snapshots
2)外部代理
- 中央
-彈簧
-HIBERNATE
-等等。
在開發工作站上,我在conf / settings.xml(mvn的主設置文件)中有以下內容:2個組,2個鏡像,2個配置文件。 看起來像這樣:
<mirrors>
<mirror>
<!--This sends everything else to nexus repo -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://<ip>:8081/nexus/content/groups/nexus</url>
</mirror>
<mirror>
<!--This sends everything else to /public -->
<id>external</id>
<mirrorOf>*</mirrorOf>
<url>http://<ip>:8081/nexus/content/groups/external</url>
</mirror>
</mirrors>
和:
<profiles>
<profile>
<id>external</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<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>
</profiles>
和:
<activeProfiles>
<activeProfile>external</activeProfile>
<activeProfile>nexus</activeProfile>
</activeProfiles>
工作流程是這樣的。
Maven以先到先得的方式使用鏡子。 也就是說,如果您正在鏡像*,那么無論您首先列出的是什么(第一個鏡像),它都是唯一能夠檢查的東西。 所以我所做的是,我將nexus(本地服務器)列為默認為鏡像*的第一個repo。 這樣,它將被強制僅使用本地存儲庫中的包。 如果包丟失會怎么樣? 我告訴maven使用“外部”鏡子。 我編輯
的conf / settings.xml中
並把外部作為第一面鏡子。 這將做的是告訴maven從外部存儲庫獲取軟件包並將它們緩存在nexus機器上的存儲中。 一旦我有了包,我看到構建過程有效,我會執行以下操作。
1)使nexus離線。
2)在nexus安裝中,找到這個文件夾:
Sonatype的工作/關系/存儲/
這是nexus緩存存儲庫包的地方。 請注意,它不會按群組緩存它們,而是按單個存儲庫緩存它們。 這將在一秒鍾內變得很重要。 既然我需要外部代理我需要的軟件包,我需要通過它們,確保它們全部有效並將它們移動到nexus上的“第三方”分支。 我執行以下操作(您可以編寫一個shell腳本來執行此操作):
1)使nexus脫機
2)對於每個外部代理
從sonatype-work / nexus / storage /到sonatype-work / nexus / storage / thirdparty的cp *
3)在線提供nexus
4)在GUI右鍵單擊第三方回購並執行:
a)重建元數據
b)修復指數
完成。
您現在可以在本地使用外部依賴項,從而可以控制對外部工件的訪問。 將存儲庫維護的角色委派給團隊負責人或負責人可能是個好主意。 因為項目第一次可能需要來自中心的東西而沒有它,所以有人必須經歷這個過程。
編輯 :
有關插件主題的一點需要注意,我更改了conf / settings.xml文件以允許maven從中心獲取插件。 所以在兩個配置文件中都做
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
這不是100%最優,因為我沒有完全控制maven在這里做什么,但看起來90%的maven確實依賴於管理復雜的插件依賴關系,所以我想我會讓它管理那個方面事物本身。
如果有人就如何更好地簡化這一過程提出建議,我會全力以赴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.