官网:https://www.sonatype.com/
nexus-3.30.1-01-unix.tar.gz
Centos7安装:
将nexus-3.30.1-01-unix.tar.gz 解压到/usr/local/nexus-3.30.1-01
创建jar包存放目录:/usr/local/sonatype-work
创建用户: useradd nexus
chown -R nexus:nexus /usr/local/nexus-3.30.1-01
chown -R nexus:nexus /usr/local/sonatype-work
切换到nexus用户:su nexus
启动nexus3
[root@centos local]# su nexus [nexus@centos local]$ cd nexus-3.30.1-01/bin/ [nexus@centos bin]$ ll total 32 drwxr-xr-x. 2 nexus nexus 4096 May 29 22:03 contrib -rwxr-xr-x. 1 nexus nexus 18620 Apr 19 17:44 nexus -rw-r--r--. 1 nexus nexus 15 Apr 19 17:44 nexus.rc -rw-r--r--. 1 nexus nexus 1538 Apr 19 17:44 nexus.vmoptions [nexus@centos bin]$ ./nexus start Starting nexus [nexus@centos bin]$ |
[nexus@centos bin]$ ps -ef|grep nexus root 50588 50485 0 22:05 pts/1 00:00:00 su nexus nexus 50589 50588 0 22:05 pts/1 00:00:00 bash nexus 50832 1 84 22:05 pts/1 00:00:16 /usr/local/jdk1.8/bin/java -server -Dinstall4j.jvmDir=/usr/local/jdk1.8 -Dexe4j.moduleName=/usr/local/nexus-3.30.1-01/bin/nexus -XX:+UnlockDiagnosticVMOptions -Dinstall4j.launcherId=245 -Dinstall4j.swt=false -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=../sonatype-work/nexus3/log/jvm.log -XX:-OmitStackTraceInFastThrow -Djava.net.preferIPv4Stack=true -Dkaraf.home=. -Dkaraf.base=. -Dkaraf.etc=etc/karaf -Djava.util.logging.config.file=etc/karaf/java.util.logging.properties -Dkaraf.data=../sonatype-work/nexus3 -Dkaraf.log=../sonatype-work/nexus3/log -Djava.io.tmpdir=../sonatype-work/nexus3/tmp -Dkaraf.startLocalConsole=false -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=lib/endorsed -Di4j.vpt=true -classpath /usr/local/nexus-3.30.1-01/.install4j/i4jruntime.jar:/usr/local/nexus-3.30.1-01/lib/boot/nexus-main.jar:/usr/local/nexus-3.30.1-01/lib/boot/activation-1.1.1.jar:/usr/local/nexus-3.30.1-01/lib/boot/jakarta.xml.bind-api-2.3.3.jar:/usr/local/nexus-3.30.1-01/lib/boot/jaxb-runtime-2.3.3.jar:/usr/local/nexus-3.30.1-01/lib/boot/txw2-2.3.3.jar:/usr/local/nexus-3.30.1-01/lib/boot/istack-commons-runtime-3.0.10.jar:/usr/local/nexus-3.30.1-01/lib/boot/org.apache.karaf.main-4.2.9.jar:/usr/local/nexus-3.30.1-01/lib/boot/osgi.core-6.0.0.jar:/usr/local/nexus-3.30.1-01/lib/boot/org.apache.karaf.specs.activator-4.2.9.jar:/usr/local/nexus-3.30.1-01/lib/boot/org.apache.karaf.diagnostic.boot-4.2.9.jar:/usr/local/nexus-3.30.1-01/lib/boot/org.apache.karaf.jaas.boot-4.2.9.jar com.install4j.runtime.launcher.UnixLauncher start 9d17dc87 0 0 org.sonatype.nexus.karaf.NexusMain nexus 50914 50589 0 22:05 pts/1 00:00:00 ps -ef nexus 50915 50589 0 22:05 pts/1 00:00:00 grep --color=auto nexus [nexus@centos bin]$ |
访问nexus:
点击登录:
用户名:admin , 密码在/usr/local/sonatype-work/nexus3/admin.password中
添加用户:
在<servers></servers>中添加。
<server> <id>releases</id> <username>admin123</username> <password>123456</password> </server> <server> <id>snapshots</id> <username>admin123</username> <password>123456</password> </server> |
发布(deploy)包
如果需要向仓库中发布包的,首先需要对仓库有权限才可以,没有权限的用户是没办法发布的。
打开 maven 的配置文件 setting.xml ,找到 servers 节点,然后在其中添加一个 server 节点。
<server> <id>releases</id> <username>admin123</username> <password>123456</password> </server> <server> <id>snapshots</id> <username>admin123</username> <password>123456</password> </server> |
用户名和密码也可以是管理员账号,或者管理员新创建的账号,只要对仓库有添加权限即可。
请注意上面的 id,一会儿在 pom 文件中还会用到。
复制对应的地址
<!-- 上传jar到maven私服 --> <distributionManagement> <repository> <id>releases</id> <url>http://192.168.8.151:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <url>http://192.168.8.151:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> <!-- 配置下载jar地址 --> <repositories> <repository> <id>nexus-releases</id> <url>http://192.168.8.151:8081/repository/maven-releases/</url> <releases> <enabled>true</enabled> </releases> </repository> <repository> <id>nexus-snapshots</id> <url>http://192.168.8.151:8081/repository/maven-snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> |
获取依赖包
如果是普通项目成员,不需要向私服发布包的,只需要在 pom 文件中加入以下配置即可。
<!-- 配置下载jar地址 --> <repositories> <repository> <id>nexus-releases</id> <url>http://192.168.8.151:8081/repository/maven-releases/</url> <releases> <enabled>true</enabled> </releases> </repository> <repository> <id>nexus-snapshots</id> <url>http://192.168.8.151:8081/repository/maven-snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> |
其中 id 要和你的仓库名称一致,url 就是私服的仓库地址,就是 type 为 group 的那个,它组合了一个 proxy 类型的和两个 hosted 类型的仓库。设置 snapshots 的 enabled 为ture,表示允许下载 snapshots 版本的包。
releases:线上版本(生产环境使用的)
snapshots:快照版本(开发过程中使用的)
maven 打包代码到私服根据version 后面是否带有“-SNAPSHOT” 来区分是打包线上版本还是快照版本,如果带有“-SNAPSHOT” 打包快照版本,否则即为线上版本
定义version 使用占位符
在properties中设置默认version
在profiles 中 根据不同的profile设置不同的version pom配置
mvn deploy 打包使用 默认版本 ; mvn deploy -p Prod 打包设置版本