有个maven私服可以很方便地管理我们的jar包和发布构建到远程仓库,本文就介绍了如何在linux下一步步使用nexus搭建maven私服
原文链接:
下载安装
解压后会在同级目录中,出现两个文件夹:nexus-2.11.4-01和sonatype-work,前者包含了nexus的运行环境和应用程序,后者包含了你自己的配置和数据。
解压命令:unzip nexus-2.11.4-01-bundle.zip
启动nexus
# cd nexus-2.11.4-01/bin/
# ls
jsw nexus nexus.bat
# ./nexus
Usage: ./nexus { console | start | stop | restart | status | dump }
# ./nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.
查看控制台:
# ./nexus console
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Running Nexus OSS...
wrapper | --> Wrapper Started as Console
wrapper | Launching a JVM...
wrapper | JVM exited while loading the application.
jvm 1 | wrapper | Unable to start JVM: No such file or directory (2)
wrapper | Reloading Wrapper configuration...
wrapper | Launching a JVM...
wrapper | JVM exited while loading the application.
jvm 2 | wrapper | Unable to start JVM: No such file or directory (2)
wrapper | Reloading Wrapper configuration...
wrapper | Launching a JVM...
wrapper | JVM exited while loading the application.
jvm 3 | wrapper | Unable to start JVM: No such file or directory (2)
wrapper | Reloading Wrapper configuration...
wrapper | Launching a JVM...
wrapper | JVM exited while loading the application.
jvm 4 | wrapper | Unable to start JVM: No such file or directory (2)
wrapper | Reloading Wrapper configuration...
wrapper | Launching a JVM...
wrapper | JVM exited while loading the application.
jvm 5 | wrapper | Unable to start JVM: No such file or directory (2)
wrapper | There were 5 failed launches in a row, each lasting less than 300 seconds. Giving up.
wrapper | There may be a configuration problem: please check the logs.
wrapper |
启动失败,原因:未安装JDK
安装JDK后,查看控制台:
# ./nexus console
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Running Nexus OSS...
Nexus OSS is already running.
控制台显示启动成功。
查看nexus日志:
# cd ../logs/
# ls -l
total 48
-rw-r--r-- 1 root root 43311 2015-09-16 19:46 wrapper.log
# tail -f wrapper.log
配置nexus
右上角以admin登陆,默认用户名/密码:admin/admin123。
3rd party、Snapshots、Releases这三个,分别用来保存第三方jar、项目组内部的快照、项目组内部的发布版.
手动添加第三方jar
将第三方的jar上传到nexus上面:
点击Upload Artifact(s)按钮提交后即上传。
查看上传的jar包如下:
在项目中使用私服的jar包配置pom.xml如下:
4.0.0 com.vclk.mkt.crawler MarketingCrawler jar 0.3 MarketingCrawler http://maven.apache.org nexus Team Nexus Repository http://yourhostname:8081/nexus/content/groups/public nexus Team Nexus Repository http://yourhostname:8081/nexus/content/groups/public de.innosystec java-unrar 0.5
Maven在项目根目录下执行mvn eclipse:eclipse命令时,所依赖的jar包都会从私服中下载到本地并关联上项目,私服中没有就会从网络上下载到私服,本地再从私服下载。
自动发布构件到远程仓库
在工程的pom.xml中添加:
nexus-releases http://yourhostname:8081/nexus/content/repositories/releases/ nexus-snapshots http://yourhostname:8081/nexus/content/repositories/snapshots/
进入maven的安装目录apache-maven-3.1.1\conf目录下,向settings.xml配置文件中的语句块中添加如下所示:
nexus-releases admin admin123 nexus-snapshots admin admin123
进入windows命令行,在工程所在目录下执行
mvn deploy
所部署的包就自动上传到了nexus安装目录下的 /maven/nexus/sonatype-work/nexus/storage/releases/com/vclk/mkt/crawler/MarketingCrawler/0.3 目录
nexus仓库中各目录介绍
项目中的各种jar包和项目快照等都放在 /nexus/sonatype-work/nexus/storage/ 目录下,在这个目录下包括以下各种目录和存放相应文件。
/nexus/sonatype-work/nexus/storage/central - 用于放置maven从中央仓库中下载下来的项目pom.xml中配置到的相关jar包;
/nexus/sonatype-work/nexus/storage/thirdparty - 用于放置自己手动上传的第三方jar包;
/nexus/sonatype-work/nexus/storage/releases - 用于放置项目deploy后的发布版。