需求扯淡
新入职一家公司,公司Java技术这块起步比较晚,没有自己成熟的框架,入职后一直忙于基础框架的搭建,框架搭建好后,领导又催着使用新框架重构现有系统,重构工作完成了差不多的时候,领导又让去做一个新系统的开发,只好使用程序员必备技能并行开发。
分析新系统的需求时,发现世界不友好了,两个项目都要依赖基础框架,但是在用户权限控制这块又有一些差别,我要是直接把框架改了又会影响到之前做好的项目,不改又满足不了新系统的要求,莫非要使用万能的复制大法,复制一份出去修改,可是这么low的方法我一向都看不上啊,真是进退两难啊。
如果我把之前的框架发布一个v1.0.0的正式版本,新的需求,在v1.0.1版本上实现,岂不是就搞定了,可是公司目前的环境支持不了这个操作啊,那就自动手动来实现了。
扯了一大堆,下面进入正题,第一步,先搭建maven本地私服,最先使用的是国外的maven仓库,项目第一次构建竟然花了1个小时,后面改用阿里的maven仓库,也没有多大的改善。公司一代新人换旧人,每次搭建项目开发环境的时候都要吐槽一下。况且咋还需要发布自己的项目到maven仓库,搭建maven私服势在必行啊。
工具准备
- jdk1.8 下载地址:http://www.oracle.com/technet...
- apache-maven-3.3.9 下载地址:http://maven.apache.org/downl...
- nexus2.x 下载、安装、配置
在浏览器输入下载地址:https://www.sonatype.com/nexu...
点击红色框选中的按钮,选择你需要下载的版本,我下载的是2.x版本
[root@9-VLA ~]# cd /usr/local
[root@9-VLA local]# mkdir nexus
[root@9-VLA local]# cd nexus
[root@9-VLA nexus]# ls
nexus-2.14.8-01-bundle.tar.gz
[root@9-VLA nexus]# tar xf nexus-2.14.8-01-bundle.tar.gz
[root@9-VLA nexus]# ls
nexus-2.14.8-01 nexus-2.14.8-01-bundle.tar.gz sonatype-work
[root@9-VLA nexus]# rm -f nexus-2.14.8-01-bundle.tar.gz
[root@9-VLA nexus]# ls
nexus-2.14.8-01 sonatype-work
[root@9-VLA nexus]# cd nexus-2.14.8-01/bin
[root@9-VLA bin]# ls
jsw nexus nexus.bat
[root@9-VLA bin]# ./nexus
Usage: ./nexus { console | start | stop | restart | status | dump }
[root@9-VLA bin]# ./nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
[root@9-VLA bin]# vi /etc/profile ##在末尾添加如下:
export RUN_AS_USER=root
[root@9-VLA bin]# source /etc/profile
[root@9-VLA bin]# ./nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
Starting Nexus OSS...
Started Nexus OSS.
[root@9-VLA bin]#
浏览器输入:http://192.168.66.66:8081/nexus,界面如下,说明已经安装成功。默认端口是8081可以到conf目录下的nexus.properties文件中去修改。
右上角有一个“Log In”按钮,可以登录,默认用户名为:admin 密码:admin123,登录进去之后,可以修改密码,点击“Profile”
点击Repositories->Configuration可以看到Releases、Snapshots、3rd party、Central这四个,分别用来保存项目组内部的发布版、项目组内部的快照、第三方jar、公共jar。
设置一个用户,在maven的settings.xml中使用
Maven配置
1)修改settings.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- <localRepository>D:/mavenRepository</localRepository> -->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>releases</id>
<username>deployment</username> ##使用nexus中设置的用户名和密码
<password>deployment</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>deployment</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://192.168.66.66:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://192.168.66.66:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-osc</id>
<name>Nexus osc</name>
<url>http://192.168.66.66:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
</settings>
配置好之后,maven下载jar包都会从私服下载,私服上没有就会去中央仓库下载保存到私服上。
2) 修改项目里面的pom.xml文件配置
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.66.66:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<uniqueVersion>false</uniqueVersion>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.66.66:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
这里的<id>releases</id>、<id>snapshots</id>和要Maven的settings.xml文件中的id对应起来,否则在发布的时候会报401错误
3) 发布snapshots版本和releases版本到私服上
<groupId>com.test.deploy</groupId>
<artifactId>deploy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
- 发布snapshots版本,进入需要发布的maven项目的目录,运行mvn deploy命令即可.
- 发布releases版本,去掉pom.xml中<version>0.0.1-SNAPSHOT</version>的-SNAPSHOT,再运行mvn deploy命令即可。
结尾
有了自己的maven私服,总算能够暂时愉快的敲代码了,且行且珍惜呐。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。