天天看点

安装配置maven以及私服nexus

1、安装maven以及nexus

maven的作用:依赖管理+项目一键构建

1.1、安装maven

直接去官网下载,或:

百度云:链接:https://pan.baidu.com/s/12zO0giSNy5zyFXHsHkRPTw

提取码:nuxr

阿里云(不限速):https://www.aliyundrive.com/s/jw7taoSL7Q1

注意,网盘下载的版本是3.6.0的,里面有一些配置已经配置好,也可以根据下面的步骤自己配置。

下载完成后,直接解压就可以使用了。

如果想在DOS命令窗口使用,则配置一下环境变量:

安装配置maven以及私服nexus
安装配置maven以及私服nexus

最后在dos命令窗口输入,出现下图则表示安装配置成功。

mvn -version
           
安装配置maven以及私服nexus

1.2、安装nexus私服

去官网下载,或者网盘下载。

提取码:9a8u

  • 解压到自己的安装目录;
    安装配置maven以及私服nexus
  • 进入bin目录的dos窗口
    安装配置maven以及私服nexus
  • 执行 nexus.bat install(如果拒绝访问就以管理员身份运行cmd)
    安装配置maven以及私服nexus
  • 安装成功,启动nexus:在刚才的命令窗口输入 nexus.bat start或者右键这里启动。
    安装配置maven以及私服nexus
  • 访问:http://localhost:8081/nexus/
    安装配置maven以及私服nexus
  • 点击右上角login,默认账号密码:admin/admin123

2、maven及nexus的配置

2.1、配置maven

  • 找到下面的配置文件
    安装配置maven以及私服nexus
2.1.1、配置本地仓库的位置(之后所有依赖下载的位置)

不修改的话默认是在:C:User(用户)/${user.home}/.m2/repository

安装配置maven以及私服nexus
2.1.2、配置阿里镜像

maven默认的下载地址是国外的仓库,可以换成国内的阿里镜像提高下载速度。下图这两个都可以用。

安装配置maven以及私服nexus
<mirror>
		<id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>       
	  </mirror>

	<mirror>
        <id>aliyunmaven</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
           
2.1.3、设置项目的默认jdk版本
安装配置maven以及私服nexus
<!-- 配置maven编译jdk版本 -->
	<profile>    
		<id>jdk-1.8</id>    
		<activation>    
			<activeByDefault>true</activeByDefault>    
			<jdk>1.8</jdk>    
		</activation>    
		<properties>    
			<maven.compiler.source>1.8</maven.compiler.source>    
			<maven.compiler.target>1.8</maven.compiler.target>
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
		</properties>    
	</profile>
           
2.1.4、idea配置maven
安装配置maven以及私服nexus

2.2、nexus私服相关配置

3、maven的介绍

3.1、maven的生命周期

第一套:

Clean Lifecycle 在进行真正的构建之前进行一些清理工作。

第二套:

Default Lifecycle 构建的核心部分,编译,测试,打包,部署等等。

第三套:

Site Lifecycle 生成项目报告,站点,发布站点。

3.2、maven依赖的作用范围

A 依赖 B,需要在 A 的 pom.xml 文件中添加 B 的坐标,添加坐标时需要指定依赖范围,依赖范围包括:

  • compile:编译范围,指 A 在编译时依赖 B,此范围为默认依赖范围。编译范围的依赖会用在编译、测试、运行,由于运行时需要所以编译范围的依赖会被打包。
  • provided:provided 依赖只有在当 JDK 或者一个容器已提供该依赖之后才使用, provided 依 赖在编译和测试时需要,在运行时不需要,比如:servlet api 被 tomcat 容器提供。
  • runtime:runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如:jdbc的驱动包。由于运行时需要所以 runtime 范围的依赖会被打包。
  • test:test 范围依赖 在编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用,

    比如:junit。由于运行时不需要所以 test范围依赖不会被打包。

  • system:system 范围依赖与 provided 类似,但是你必须显式的提供一个对于本地系统中 JAR

    文件的路径,需要指定 systemPath 磁盘路径,system依赖不推荐使用。

安装配置maven以及私服nexus

总结:

  • 默认引入 的 jar 包 ------- compile 【默认范围 可以不写】(编译、测试、运行 都有效 )
  • servlet-api 、jsp-api ------- provided (编译、测试 有效, 运行时无效 防止和 tomcat 下 jar 冲突)
  • jdbc 驱动 jar 包 ---- runtime (测试、运行 有效 )
  • junit ----- test (测试有效)
  • 依赖范围由强到弱的顺序是:compile>provided>runtime>test

3.3、pom文件的主要标签

pom.xml 是 Maven 项目的核心配置文件,位于每个工程的根目录,基本配置如下:

<project > :文件的根节点 .
<modelversion > : pom.xml 使用的对象模型版本
<groupId > :项目名称,一般写项目的域名
<artifactId > :模块名称,子项目名或模块名称
<version > :产品的版本号 . 
<packaging > :打包类型,一般有 jar、war、pom 等
<name > :项目的显示名,常用于 Maven 生成的文档。 
<description > :项目描述,常用于 Maven 生成的文档
<dependencies> :项目依赖构件配置,配置项目依赖构件的坐标
<build> :项目构建配置,配置编译、运行插件等。
           

4、nexus的介绍

4.1、仓库类型

仓库的位置默认是在安装目录下的 sonatype-work 目录中

安装配置maven以及私服nexus
安装配置maven以及私服nexus
1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 	 snapshot 两部
分,Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私
服自动去中央仓库下载 jar 包或者插件。
3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓
库组。
4. virtual(虚拟):兼容 Maven1 版本的 jar 或者插件
           

4.2、将项目发布到nexus私服

  • 1、先配置maven的settings.xml,可以配置多个,但是id不能重复
    安装配置maven以及私服nexus
<server>
		 <id>releases</id>
		 <username>admin</username>
		 <password>admin123</password>
	</server>
	<server>
		 <id>snapshots</id>
		 <username>admin</username>
		 <password>admin123</password>
	 </server>
           
  • 2、在自己的maven项目pom文件中配置如下信息,这里的id要和刚才maven的配置文件中的id对相应上。
<distributionManagement>
 <repository>
 <id>releases</id>
 
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
 </repository>
 <snapshotRepository>
 <id>snapshots</id>
 
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
 </snapshotRepository>
</distributionManagement>
           

4.3、从nexus私服下载jar包

  • 1、配置settings.xml文件
    安装配置maven以及私服nexus
<profile> 
	<!--profile 的 id-->
		<id>dev</id> 
		 <repositories> 
			 <repository> 
				<!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
				 <id>nexus</id> 
				<!--仓库地址,即 nexus 仓库组的地址-->
				 <url>http://localhost:8081/nexus/content/groups/public/</url> 
				<!--是否下载 releases 构件-->
				 <releases> 
					<enabled>true</enabled> 
				 </releases> 
				<!--是否下载 snapshots 构件-->
				 <snapshots> 
					<enabled>true</enabled> 
				 </snapshots> 
			 </repository> 
		 </repositories> 
		<pluginRepositories> 
		 <!-- 插件仓库,maven 的运行依赖插件,也需要从私服下载插件 -->
		 <pluginRepository> 
		 <!-- 插件仓库的 id 不允许重复,如果重复后边配置会覆盖前边 -->
		 <id>public</id> 
		 <name>Public Repositories</name> 
		 <url>http://localhost:8081/nexus/content/groups/public/</url> 
		 </pluginRepository> 
		 </pluginRepositories> 
	 </profile> 
           
安装配置maven以及私服nexus
<activeProfiles>
		<activeProfile>dev</activeProfile>
	 </activeProfiles>
           
  • 2、配置项目的pom文件
<repositories>

	 <repository>
	 
		 <releases>
			 <enabled>true</enabled>
		 </releases>
	 
		 <snapshots>
		 	<enabled>true</enabled>
		 </snapshots>
		 
		 <id>public</id>
		 <name>Public Repositories</name>
		 <url>http://localhost:8081/nexus/content/groups/public/</url>
		 
	 </repository>
	 
	 <repository>
	 
		 <snapshots>
		 	<enabled>false</enabled>
		 </snapshots>
		 
		 <id>central</id>
		 <name>Central Repository</name>
		 <url>https://repo.maven.apache.org/maven2</url>
		 
	 </repository>
	 
 </repositories>
 
 
 <pluginRepositories>
 
	 <pluginRepository>
		 <id>public</id>
		 <name>Public Repositories</name>
		 <url>http://localhost:8081/nexus/content/groups/public/</url>
		 </pluginRepository>
		 <pluginRepository>
		 <releases>
		 	<updatePolicy>never</updatePolicy>
		 </releases>
		 
		 <snapshots>
		 	<enabled>false</enabled>
		 </snapshots>
		 
		 <id>central</id>
		 <name>Central Repository</name>
		 <url>https://repo.maven.apache.org/maven2</url>
		 
	 </pluginRepository>
	 
 </pluginRepositories>
           

5、将第三方jar包安装到maven和nexus仓库

5.1、安装到maven仓库

参数说明:

mvn install:install-file    

# windows下jar包所在的目录
-Dfile=C:\Users\华硕\Desktop\ojdbc7.jar

# 分组id    
 -DgroupId=com.oracle

# artifact的名称   

-DartifactId=ojdbc14

# jar 包版本,根据自己的情况定义    
 -Dversion=10.2.0.4.0

# 包类型如jar/zip    
 -Dpackaging=jar
 
# 是否创建pom文件    
 -DgeneratePom=true

注意:每个 -D 前面有个空格
           

5.2、安装到私服

  • 先在maven的settingsxml中配置私服第三方仓库的相关信息。
    安装配置maven以及私服nexus
<server> 
		 <id>thirdparty</id> 
		 <username>admin</username>
		 <password>admin123</password> 
	 </server>
           
  • 执行安装命令
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc7.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty
           

参数说明

mvn deploy:deploy-file

# windows下jar包所在的目录
-Dfile=C:\Users\华硕\Desktop\ojdbc7.jar

# 分组id    
 -DgroupId=com.oracle

# artifact的名称   

-DartifactId=ojdbc14

# jar 包版本,根据自己的情况定义    
 -Dversion=10.2.0.4.0

# 包类型如jar/zip    
 -Dpackaging=jar
 
# 仓库地址
Durl=http://localhost:8081/nexus/content/repositories/thirdparty

# 仓库id
-DrepositoryId=thirdparty

注意:每个 -D 前面有个空格