Maven 是一個項目管理和構建自動化工具,是Apache Fundation下的一個Java項目。經常使用於Java項目中依賴管理nginx
下載直接去官網數據庫
已經編譯的二進制包 直接解壓到安裝目錄便可apache
$ cd /path/to/Download $ tar -xf apache-maven-3.3.9-bin.tar.gz $ sudo mv apache-maven-3.3.9 /usr/local/ $ sudo ln -s /usr/local/apache-maven-3.3.9/bin/* /usr/bin/
maven會首先查詢 pom.xml
引入的包,查找本地倉庫是及工程目錄,如不存在相應的包就會從maven的遠程apache倉庫下載vim
打包工程瀏覽器
mvn package
發佈包到本地倉庫緩存
mvn install
Nexus是一個強大的Maven倉庫管理器,它極大地簡化了本身內部倉庫的維護和外部倉庫的訪問。利用Nexus你能夠只在一個地方就可以徹底控制訪問 和部署在你所維護倉庫中的每一個Artifact。Nexus是一套「開箱即用」的系統不須要數據庫,它使用文件系統加Lucene來組織數據。Nexus 使用ExtJS來開發界面,利用Restlet來提供完整的REST APIs,經過m2eclipse與Eclipse集成使用。Nexus支持WebDAV與LDAP安全身份認證。 Nexus分爲專業收費的Nexus Pro與免費的Nexus OSS兩個版本。本文以Nexus OSS爲主體安全
下載Nexus OSS Nexus OSS 3.0 已經不支持Maven格式,因此這裏咱們下載 2.x 版本, 下載地址bash
安裝Nexus OSSeclipse
$ cd /path/to/Downloads $ tar -xf nexus-2.14.1-01-bundle.tar.gz $ cd nexus-2.14.1-01-bundle $ sudo mv nexus-2.14.1-01 /usr/local/ # 創建倉庫存放目的 $ sudo mkdir /Data $ sudo mv sonatype-work /Data/ # 創建bin運行軟鏈 # sudo ln -s /usr/local/nexus /usr/local/nexus-2.14.1-01 # sudo ln -s /usr/loca/nexus-2.14.1-01/bin/nexus /usr/local/bin/
配置Nexus OSSmaven
$ cd /usr/local/nexus-2.14.1-01/conf $ sudo vim nexus-propertities # 修改如下內容 nexus-work=/Data/nexus-repos/sonatype-work/nexus
測試運行
$ nexus start # 中止 $ nexus stop # 重啓 $ nexus restart
瀏覽器打開http://localhost:8081/nexus
,便可看到nexus oss的歡迎界面
登陸用戶:admin,默認密碼:admin123,登陸後可在我的中心修改。
nginx反代方式
$ cd /path/to/nginx/conf/vhosts $ sudo vim nexus.local.me.conf # 內容以下: server { server_name nexus.aeonluck.me; location / { proxy_pass http://127.0.0.1:8081; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
添加本地host
# sudo vim /etc/hosts # 增長如下內容 127.0.0.1 nexus.local.me
測試反代
$ sudo nginx -s reload
瀏覽器打開http://nexus.local.me/nexus
便可見到nexus oss歡迎界面
Nexus的倉庫分爲如下幾種:
首先,咱們要對幾個重要倉庫的配置進行修改,如 central
,Apache Snapshots
等,你也能夠本身添加外部倉庫。
小提示:
修改Maven配置,加入私庫
$ vim ~/.m2/settings.xml # servers 內增長咱們的私庫用戶 <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> # mirrors增長私庫 <mirror> <id>nexus</id> <name>Local nexus repo</name> <url>http://nexus.aeonluck.me/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror>
特別要注意的是:
必定要配置爲 <mirrorOf>*</mirrorOf>
,即讓全部的倉庫都先通過私庫,這樣能夠將遠程下載的包緩存到本地
小提示:
添加個國內倉庫 默認的apache的倉庫下載速度比較慢,這裏選擇阿里雲的Nexus倉庫
$ vim ~/.m2/setting2.xml # 在mirrors中增長如下內容 # 注意,位置於咱們的私庫以後,不然私服不能緩存到下載的包。 <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
關於Maven的mirror:
通常狀況下,一個使用Maven的Java團隊,都會有本身的項目倉庫,當開發完成相應的功能以後,都會發布相應的jar/war包到私服。供其它項目成員使用
發佈配置 在Maven工程的pom.xml中增長如下內容
<!-- 工程自述 --> <groupId>org.aeonluck</groupId> <artifactId>org.aeonluck.hello</artifactId> <packaging>jar</packaging> <version>0.1.0-SNAPSHOT</version> <!-- 發佈配置 --> <distributionManagement> <repository> <id>snapshots</id> <name>Snapshots</name> <url>http://nexus.local.me/nexus/content/repositories/snapshots/</url> </repository> </distributionManagement>
重要提示:
snapshot
的倉庫時,工程自述說明小節中的 <version>
,須要帶上後綴 SNAPSHOT
,不然會當成 release
發佈。會收到 400 Bad Request
錯誤。<url>
地址必定要區分清楚。並在 release
與 snapshot
倉庫裏配置不一樣的地址。此外,id、name 要與Nexus中倉庫的配置 configuration 中的內容一致。發佈jar到的私庫
$ cd /path/to/myproject $ mvn deploy
包發佈以後,咱們便可在Nexus OSS的管理倉庫中看到咱們剛纔發佈的包
小提示
$ mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar # 發佈到本地倉庫 mvn install:install-file -DgroupId=com.bea.xml -DartifactId=jsr173-ri -Dversion=1.0 -Dpackaging=jar -Dfile=[path to file] # 發佈到私服 mvn deploy:deploy-file -DgroupId=com.bea.xml -DartifactId=jsr173-ri -Dversion=1.0 -Dpackaging=jar -Dfile=[path to file] -Durl=[url] -DrepositoryId=[id]