在局域網內搭建maven私有倉庫,可避免每次都從中央倉庫下載公共jar包,另外將A模塊作為二方庫發布到私有倉庫后,B模塊可以很方便的引用,今天我們就來實戰maven私有倉庫的搭建和使用;
原文地址:http://blog.csdn.net/boling_cavalry/article/details/79059021
環境信息
- 安裝私有倉庫的機器是ubuntu16,IP是192.168.119.155;
- maven私有倉庫的軟件是nexus-2.14.5,請在官網下載;
- 在window10環境上做驗證的使用,用到的maven版本是3.3.3,jdk版本1.8.0_111;
安裝和啟動nexus
- 打開/etc/profile,新增一行export RUN_AS_USER=root;
- 執行命令source /etc/profile,使剛才的配置生效;
- 從官網下載的是nexus-2.14.5-02-bundle.tar.gz,執行tar -zxvf nexus-2.14.5-02-bundle.tar.gz解壓后,里面有兩個目錄:nexus-2.14.5-02和sonatype-work;
- 進入nexus-2.14.5-02/bin,執行./nexus start啟動nexus;
- 進入nexus-2.14.5-02/logs,執行tail -f wrapper.log觀察日志滾動信息,看到如下信息時nexus啟動成功:
jvm 1 | 2018-01-13 21:38:44,671-0800 INFO [jetty-main-1] org.sonatype.nexus.webresources.internal.WebResourceServlet - Max-age: 30 days (2592000 seconds) jvm 1 | 2018-01-13 21:38:44,698-0800 INFO [jetty-main-1] org.sonatype.nexus.bootstrap.jetty.InstrumentedSelectChannelConnector - Metrics enabled jvm 1 | 2018-01-13 21:38:44,713-0800 INFO [jetty-main-1] org.eclipse.jetty.server.AbstractConnector - Started InstrumentedSelectChannelConnector@0.0.0.0:8081 jvm 1 | 2018-01-13 21:38:44,713-0800 INFO [jetty-main-1] org.sonatype.nexus.bootstrap.jetty.JettyServer - Running jvm 1 | 2018-01-13 21:38:44,713-0800 INFO [WrapperListener_start_runner] org.sonatype.nexus.bootstrap.jetty.JettyServer - Started jvm 1 | 2018-01-13 21:39:26,926-0800 INFO [qtp984089572-47] org.apache.shiro.nexus5727.FixedDefaultWebSessionManager - Global session timeout: 1800000 ms jvm 1 | 2018-01-13 21:39:26,931-0800 INFO [qtp984089572-47] org.apache.shiro.session.mgt.AbstractValidatingSessionManager - Enabling session validation scheduler... jvm 1 | 2018-01-13 21:39:26,944-0800 INFO [qtp984089572-47] org.apache.shiro.cache.ehcache.EhCacheManager - Using existing EHCache named [shiro-activeSessionCache] jvm 1 | 2018-01-13 21:39:26,945-0800 INFO [qtp984089572-45] org.apache.shiro.nexus5727.FixedDefaultWebSessionManager - Global session timeout: 1800000 ms
登錄
- 在瀏覽器輸入地址:http://192.168.119.155:8081/nexus/
- 進入nexus首頁后,點擊右上角的”Log In”按鈕,輸入用戶名”admin”,默認密碼”admin123”;
- 點擊左側的”Repositories”按鈕,看到已有的倉庫,如下圖:
目前nexus已經開始正常工作了,接下來我們開始使用私有倉庫;
使用私有倉庫,配置
- 我的windows10開發環境要使用私有倉庫,需要配置maven的信息,打開maven安裝目錄下的conf/settings.xml文件;
- 在mirrors節點下新增一個mirror節點,內容如下:
<mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://192.168.119.155:8081/nexus/content/groups/public/</url> </mirror>
192.168.119.155是nexus機器的IP;
3. 在profiles節點下新增一個profile節點,內容如下:
<profile> <id>nexus</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>
以上新增了中央倉庫的相關信息;
4. 新增一個activeProfiles節點,該節點和profiles節點一個層次,內容如下:
<activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
新建一個springboot工程
- 新建一個基於maven構建的springboot的web工程,里面只有一個最簡單的controller,在pom.xml所在目錄下執行以下命令:
mvn clean package -Dmaven.test.skip=true
- 可以看到如下信息,當前的maven已經在通過私有倉庫下載:
[INFO] Building jar: D:\temp\201801\06\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ helloworld --- Downloading: http://192.168.119.155:8081/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.9.RELEASE/spring-boot-loader-tools-1.5.9.RELEASE.pom Downloaded: http://192.168.119.155:8081/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.9.RELEASE/spring-boot-loader-tools-1.5.9.RELEASE.pom (0 B at 0.0 KB/sec) Downloading: http://192.168.119.155:8081/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.9.RELEASE/spring-boot-loader-tools-1.5.9.RELEASE.jar Downloaded: http://192.168.119.155:8081/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.9.RELEASE/spring-boot-loader-tools-1.5.9.RELEASE.jar (0 B at 0.0 KB/sec) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:21 min [INFO] Finished at: 2018-01-14T17:57:45+08:00 [INFO] Final Memory: 29M/164M [INFO] ------------------------------------------------------------------------
已緩存信息
此時再從瀏覽器查看“Central”倉庫的信息,發現里面已經緩存了前面的springboot工程所依賴的jar包,今后其他機器再需要這些jar包時,就可以直接從私有倉庫上下載了,而私有倉庫沒有的jar包,也會由私有倉庫下載並保存,然后提供給使用私有倉庫的機器;
至此,nexus的安裝和緩存jar包功能的使用實戰已經完成,接下來的章節,我們會將本機的工程打包並發布到私有倉庫;