用于实现Maven构建私人仓库的一个工具。html
可使用nexus 统一管理咱们到依赖,再也不须要再从中央仓库下载对应的jar包java
私服是在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服以后,当 Maven 须要下载jar包时,先请求私服,私服上若是存在则下载到本地仓库。不然,私服直接请求外部的远程仓库,将jar包下载到私服,再提供给本地仓库下载。apache
<!-- more -->缓存
简介:依赖是maven最为用户熟知的特性之一,单个项目的依赖管理并不难,可是要管理几个或者几十个模块的时,那这个依赖应该怎么管理服务器
依赖的传递性oracle
依赖的做用范围maven
compileide
provided工具
runtime测试
test
system
系统范围与provided相似,不过你必须显式指定一个本地系统路径的JAR,此类依赖应该一直有效,Maven也不会去仓库中寻找它。
<project> ... <dependencies> <dependency> <groupId>sun.jdk</groupId> <artifactId>tools</artifactId> <version>1.5.0</version> <scope>system</scope> <systemPath>${java.home}/../lib/tools.jar</systemPath> </dependency> </dependencies> ... </project>
import
依赖的两大原则
路径近者优先
A > B > C-1.0 A > C-2.0
第一声明优先
A > B > D-1.0 A > C > D-2.0
依赖的管理
依赖排除
依赖可选
简介:当出现jar包冲突时,咱们应该如何快速定位和处理jar包冲突问题
mvn dependency:tree -Dverbose
简介:介绍nexus服务器预置的仓库
类型介绍
预置仓库
“GET PREPOSITORY OSS”
OSS2
cmd
, 进入目录D:\java\Nexus\nexus-latest-bundle\nexus-2.14.15-01\bin
nexus.bat start
,能够发现会拒绝访问
使用管理员模式运行
nexus.bat install
安装nexus.bat start
启动服务Log In
登录系统admin
以及 admin123
,输入帐户名和密码以后登录系统add
,选择 Hosted Repository
改含义会在后面进行解释save
按钮便可建立一个仓库这里以一个本身的项目做为测试
Maven依赖以下:
<dependency> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <version>1.0.0</version> </dependency>
咱们须要先登陆Maven Nexus,具有管理员的权限,而后点击3rd party
(三方依赖),截图内容以下
依照截图填入以下的依赖
GAV Definition: 定义GAVAuto Guess:自动猜想
Group:同Maven 到 group定义
Artifact:同Maven 到 Artifact定义
Version:版本号
Packaging:打包方式
下方须要上传依赖对应到jar包
上传以后须要添加到maven Nexus
等待上传,上传成功以后会给予对应到提示信息
上传完成以后,咱们能够点击Browse Index
看到本身以前上传的依赖
默认状况下咱们到公共访问地址以下:http://localhost:8081/nexus/content/groups/public 公共仓库的访问地址
把jar包添加到nexus以后,咱们能够尝试让nexus来管理咱们到项目依赖了
接下来介绍如何使用maven nexus 来关联咱们到依赖
注意:事先查看nexus公有仓库是否存在依赖
conf
下面增长配置,以我的为例进入D:\java\apach\apache-maven-3.6.0\conf
,修改setting.xml
,添加以下内容<!-- 添加 nexus 访问权限 --> <servers> <server> <id>xdclass</id> <username>admin</username> <password>admin123</password> </server> </servers> <mirrors> <!-- 自定义 nexus --> <mirror> <id>xdclass</id> <mirrorOf>nexus,central</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public/</url> <name>local nexus</name> </mirror> <!-- 阿里云配置 --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>xd</id> <repositories> <repository> <id>local-nexus</id> <!-- 本地仓库路径 --> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <!-- 开启快照 --> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <!-- 须要 开启 Profile 配置 --> <activeProfiles> <activeProfile>xd</activeProfile> </activeProfiles>
本节介绍如何把本身的项目发布到nexus ,这里单独开了一个仓库进行配置
在进行本节内容以前,查看Maven 到 setting.xml
内容
<!-- 用于发布正式版本 --> <server> <id>public</id> <username>admin</username> <password>admin123</password> </server> <!-- 测试 --> <server> <id>lazytime</id> <username>admin</username> <password>admin123</password> </server>
pom.xml
添加以下内容<build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> </plugins> </pluginManagement> </build> <distributionManagement> <repository> <!-- 此id 必须对应setting 里面到 server id 不然会没有权限没法部署 --> <id>public</id> <url>http://localhost:8081/nexus/content/repositories/lazytime</url> </repository> </distributionManagement>
deloyer
项目到 maven Nexus
运行Maven Deloy
发布项目到nexus
Maven Nexus
,查看发布的项目内容若是不能发布,请查看仓库是否容许从新部署:
使用上一节办法存在很大到缺点:
如何解决如上问题呢?
那么咱们就须要使用快照
,快照至关于项目到一个副本,咱们能够在开发到时候使用快照,发布到时候在使用正式到版本号进行处理,使nexus的管理更加方便
Maven
的setting
文件当中添加以下内容,加入到对应的帐号<server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
snaphoto
规则,不然部署快照版本会失败<modelVersion>4.0.0</modelVersion> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <!-- 写法必定要规范 --> <version>1.0-SNAPSHOT</version>
maven deloy
部署配置注意待发布项目的pom.xml
配置以下
<distributionManagement> <snapshotRepository> <id>snapshots</id> <url>http://localhost:8081/nexus/content/repositories/snapshots</url> </snapshotRepository> </distributionManagement>
Maven Nexus
是否发布成功再次强调,若是部署失败,请查看仓库是否容许重复部署
因为使用了快照版本,须要更改依赖以下:
<dependency> <groupId>org.smart4j</groupId> <artifactId>smart-framwork</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
deploy
,部署快照版本的项目