title: Day69-Maven
date: 2021-05-10 09:55:16
author: Liu_zimo
Maven介绍
- maven是一个项目管理工具,主要作用是在项目开发阶段对Java项目进行依赖管理和项目构建
- 依赖管理:就是对jar包的管理。通过导入maven坐标,就相当于将仓库中的jar包导入了当前项目中
- 项目构建:通过maven的一个命令就可以完成项目从清理、编译、测试、报告、打包,部署整个过程
- 清理 → 编译 → 测试 → 报告 → 打包 → 部署
maven的仓库类型
- 本地仓库
- 远程仓库
- maven中央仓库(地址: https://repo1.maven.org/maven2/)
- maven私服(公司局域网内的仓库,需要自己搭建)
- 其他公共远程仓库(例apache提供的远程仓库,地址: https://repo.maven.apache.org/maven2/)
maven常用命令
- clean:清理
- compile:编译
- test:测试
- package:打包
- install:安装
maven书写规范
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
maven依赖范围
依赖范围 | 对于编译classpath有效 | 对于测试classpath有效 | 对于运行时classpath有效 | 例子 |
---|---|---|---|---|
compile | Y | Y | Y | spring-core |
test | - | Y | - | Junit |
provided | Y | Y | - | servlet-api |
runtime | - | Y | Y | JDBC驱动 |
system | Y | Y | - | 本地的,maven仓库之外的类库 |
Maven依赖传递
在maven中,依赖是可以传递的,假设存在三个项目,分别是项目A,项目B以及项目C。假设C依赖B,B依赖A,那么我们可以根据maven项目依赖的特征不难推出项目C也依赖A(C间接依赖A)
依赖冲突
由于依赖传递现象的存在,spring-webmvc-4.2.4依赖spring-beans-4.2.4,spring-aop-5.0.2依赖spring-beans-5.0.2,但是发现spring-beans-4.2.4加入到了工程中,而我们希望spring-beans-5.0.2加入工程。这就造成了依赖冲突
解决依赖冲突
- 使用maven提供的依赖调解原则
- 第一声明者优先原则
- 路径近者优先原则
- 排除依赖
- 锁定版本
第一声明者优先原则
在pom文件中定义依赖,以先声明的依赖为准。其实就是根据坐标导入的顺序来确定最终使用哪个传递过来的依赖
路径近者优先原则
在pom文件定义依赖,以路径近者为准
还是上述情况,spring-aop和spring-webmvc都会传递过来spring-beans,那如果直接把spring-beans的依赖直接写到pom文件中,那么项目就不会再使用其他依赖传递来的spring-beans,因为自己直接在pom中定义spring-beans要比其他依赖传递过来的路径要近
排除依赖
可以使用exclusions标签将传递过来的依赖排除出去
锁定版本 荐
采用直接锁定版本的方法确定依赖jar包的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本为准添加到工程中,此方法在企业开发中经常使用
- 版本锁定的使用方式:
- 第一步:在dependencyManagement标签中锁定依赖的版本
- 第二步:在dependencies标签中声明需要导入的maven坐标
- 在dependencyManagement标签中锁定依赖的版本
<!--依赖jar包版本锁定-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org. springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
- 注意:pom文件中使用dependencyManagement标签进行依赖jar的版本锁定,并不会真正将jar包导入到项目中,只是对这些jar的版本进行锁定。项目中使用哪些jar包,还需要在dependencies标签中进行声明。
- 在dependencies标签中声明需要导入的maven坐标
<dependencies>
<!--由于前面已经在dependencyManagement标签中锁定了spring-beans和spring-context的版本,
此处只需要导入groupId和lartifactId即可,无须再指定version-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupld>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
基于Maven构建SSM工程案例
需求分析
本案例基于maven构建SSM (Spring+SpringMVC+Mybatis)工程,通过maven坐标进行依赖管理。最终实现根据id查询商品信息的功能。
实现步骤
-
数据库环境搭建
- 创建数据库test
- 创建商品表item
DROP TABLE IF EXISTS `item`; CREATE TABLE `item`( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, `price` float default NULL, `createtime` datetime default NULL, `detail` varchar(255) default NULL, PRIMARY KEY(`id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `item` VALUES('1','电视机','3999','2021-05-10 14:30:18','电视机');
-
maven项目构建
- 创建maven web项目
- 配置pom.xml
- 实现spring + mybatis整合
- 创建POJO类
- 持久层DAO接口编写
- Mapper映射文件编写
- 业务层Service编写
- spring配置文件applicationContext-dao.xml编写
- spring配置文件applicationContext-service.xml编写
- 加入springmvc相关配置
- 表现层Controller编写
- springmvc.xml文件编写
- jsp页面编写
- 配置web.xml文件
- pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zimo</groupId>
<artifactId>ssmweb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssmweb Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
<springmvc.version>5.0.5.RELEASE</springmvc.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springmvc.version}</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Mybatis和spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!--springmvc核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
- Spring + mybatis整合
# POJO创建
package com.zimo.ssm.pojo;
import java.util.Date;
/**
* @author Liu_zimo
* @version v0.1 by 2021/5/10 15:37
*/
public class Item {
private int id;
private String name;
private float price;
private Date createtime;
private String detail;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public float getPrice() { return price; }
public void setPrice(float price) { this.price = price; }
public Date getCreatetime() { return createtime; }
public void setCreatetime(Date createtime) { this.createtime = createtime; }
public String getDetail() { return detail; }
public void setDetail(String detail) { this.detail = detail; }
@Override
public String toString() { return "Item{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", createtime=" + createtime + ", detail='" + detail + '\'' + '}';
}
}
- dao创建
package com.zimo.ssm.dao;
import com.zimo.ssm.pojo.Item;
/**
* @author Liu_zimo
* @version v0.1 by 2021/5/10 15:38
*/
public interface ItemMapper {
public Item findById(int id);
}
- Mapper映射文件编写
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zimo.ssm.dao.ItemMapper">
<select id="findById" parameterType="int" resultType="Item">
select * from item where id = #{id}
</select>
</mapper>
- 业务层Service编写
package com.zimo.ssm.service;
import com.zimo.ssm.pojo.Item;
/**
* Service接口
* @author Liu_zimo
* @version v0.1 by 2021/5/10 15:46
*/
public interface ItemService {
public Item findById(int id);
}
package com.zimo.ssm.service.impl;
import com.zimo.ssm.dao.ItemMapper;
import com.zimo.ssm.pojo.Item;
import com.zimo.ssm.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Service实现类
* @author Liu_zimo
* @version v0.1 by 2021/5/10 15:47
*/
@Service
@Transactional
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
public Item findById(int id) {
return itemMapper.findById(id);
}
}
- spring配置文件applicationContext-dao.xml编写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置数据源信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!--配置spring整合mybaits框架的SQLSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--扫描pojo包,为实体类创建别名-->
<property name="typeAliasesPackage" value="com.zimo.ssm.pojo" />
</bean>
<!--配置扫描接口 mapper扫描器,用于产生代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zimo.ssm.dao" />
</bean>
</beans>
- spring配置文件applicationContext-service.xml编写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描器,扫描service-->
<context:component-scan base-package="com.zimo.ssm.service" />
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--事务注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
- 表现层Controller编写
package com.zimo.ssm.controller;
import com.zimo.ssm.pojo.Item;
import com.zimo.ssm.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Liu_zimo
* @version v0.1 by 2021/5/10 16:18
*/
@Controller
@RequestMapping("/item")
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/showItem/{id}")
public String findById(@PathVariable("id") int id, Model model){
Item item = itemService.findById(id);
model.addAttribute("item", item);
return "item";
}
}
- springmvc.xml文件编写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置扫描器,扫描controller-->
<context:component-scan base-package="com.zimo.ssm.controller" />
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
- jsp页面编写
<%--启动EL表达式 isELIgnored="false" --%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
<title>item info</title>
</head>
<body>
item info : ${item}
</body>
</html>
- 配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--指定Spring配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!--配置Spring框架启动时使用的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定SpringMVC配置文件位置,如果不指定他会去找WEB-INF/spring-mvc.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
分模块构建Maven工程
分模块构建maven工程分析
在现实生活中,汽车厂家进行汽车生产时,由于整个生产过程非常复杂和繁琐,工作量非常大,所以车场都会将整个汽车的部件分开生产,最终再将生产好的部件进行组装,形成一台完整的汽车
- 常见的拆分方式有两种:
- 第一种:按照业务模块进行拆分,每个模块拆分成一个maven工程,例将一个项目分为用户模块、订单模块、购物车模块等,每个模块对应就是一个maven工程
- 第二种:按照层进行拆分,例如持久层、业务层、表现层等,每个层对应就是一个maven工程
- 不管是上面哪种拆分方式,通常都会提供一个父工程,将一些公共的代码和配置提取到父工程中进行统一管理和配置
Maven工程的继承
在Java语言中,类之间是可以继承的,通过继承,子类就可以用父类中非private的属性和方法。同样,在maven工程之间也可以继承,子工程继承父工程后,就可以使用在父工程中引入的依赖。继承的目的是为了消除重复代码
- 被继承的Maven项目中的POM部分定义是
<groupId>com.company</groupId>
<atrifactId>conpany-project-parent</atrifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
被继承的maven工程通常称为父工程,父工程的打包方式必须为pom,所以我们区分某个maven工程是否为父工程就看这个工程的打包方式是否为pom
- 继承的Maven项目中的POM的关键部分就是
<parent>
<groupId>com.taotao</groupId>
<atrifactId>conpany-project-parent</atrifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<atrifactId>conpany-project-children</atrifactId>
继承其他maven父工程的工程通常称为子工程,在pom.xml文件中通过parent标签进行父工程的继承
- 父工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zimo</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>son</module>
</modules>
<!-- 父工程的打包方式必须为pom -->
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
</project>
- 子工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--通过parent标签进行maven工程的继承-->
<parent>
<artifactId>parent</artifactId>
<groupId>com.zimo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>son</artifactId>
</project>
maven工程的聚合
在maven工程的pom.xml文件中可以使用<modules>标签将其他maven工程聚合到一起,聚合的目的是为了进行统一操作
例如拆分后的maven工程有多个,如果要进行打包,就需要针对每个工程分别执行打包命令,操作起来非常繁琐。这时就可以使用<modules>标签将这些工程统一聚合到maven工程中,需要打包的时候,只需要在此工程中执行一次打包命令,其下被聚合的工程就都会被打包了
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zimo</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>son</module>
</modules>
<!-- 父工程的打包方式必须为pom -->
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
<!--聚合其他maven工程-->
<modules>
<module>../son</module>
<module>../ssmweb</module>
</modules>
</project>
- 父工程用来锁定版本,子工程使用时导报
Maven私服安装和启动
-
安装步骤:
- 下载nexus私服安装包
- 安装cmd:nexus.bat install/uninstall
- 启动cmd:nexus.bat start
- http://lcoalhost:8081/nexus
- user:admin password:admin123
-
配置私服
-
修改apache-maven-3.5.2\config\setting.xml
<settings> <servers> <server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers> </settings>
-
-
配置项目上传
# pom.xml <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>
-
配置项目下载
# setting.xml <settings> <!--配置下载--> <profiles> <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> </profiles> <!-- 开启下载配置 --> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings>
nexus仓库类型
通过前面的仓库列表可以看到,nexus默认内置了很多仓库,这些仓库可以划分为4种类型,每种类型的仓库用于存放特定的jar包,具体说明如下:
- hosted,宿主仓库,部署自己的jar到这个类型的仓库,包括Releases和Snapshots两部分,Releases为公司内部发布版本仓库、Snapshots为公司内部测试版本仓库
- proxy,代理仓库,用于代理远程的公共仓库,如maven中央仓库,用户连接私服,私服自动去中央仓库下载jar包或者插件
- group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置自己的maven连接仓库组
- virtual(虚拟):兼容Maven1版本的jar或者插件
将项目发布到maven私服
- 将项目发布到maven私服操作步骤如下:
- 配置maven的settinas.xml文件
- 配置项目的pom.xml文件
- 执行mvn deploy命令
- 从私服下载jar到本地操作步骤如下:
- 在maven的settings.xml文件中配置下载模板
- 在maven的settings.xml文件中配置激活下载模板
将第三方jar安装到本地仓库和maven私服
-
下载Oracle的jar包
-
在maven的settings.xml配置文件中配置第三方仓库的server信息
<server> <id>thirdparty</id> <username>admin</username> <password>admin123</password> </server>
-
执行mvn deploy命令进行安装
mvn deploy:deploy-file-Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14-Dversion=10.2.0.4.0 -Dpackaging=jar-Durl=http://localhost"8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty