Maven初级
Maven全新的项目构建方式,一款服务于java平台的自动化构建工具。
Maven主要做两件事情
1. 一键构建:统一开发规范与工具,自身集成了Tomcat的插件,可以对项目进行编译、测试、打包、安装、发布
2. 依赖管理:统一管理jar包。
仓库的种类:本地仓库 远程仓库(私服) 中央仓库
仓库之间的关系:当我们启动一个maven工程时,maven会通过pom文件中的jar包的坐标去本地仓库找jar包。默认情况下,本地仓库没有对应的jar时,自动去中央仓库下载jar包到本地仓库。在公司中,本地中没有时就去私服,再没有本地上传或者去中央仓库。
使用maven的直接好处就是统一管理jar包,他们都统一放置在本地仓库中,默认在Users/用户名.m2目录下(也可以修改这个默认地址)在使用Maven之前首先要配置环境变量。
pom.xml:Project Object Model项目对象模型,他是Maven的核心配置文件,所有的构建的配置都在这里设置。
在Idea中,首先需要勾选Create from archetype,然后选择maven-archetype-webapp.
关于groupId和artifactId: cn.sly + projectname
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
比如我创建一个项目,我一般会将groupId设置为cn.snowin,cn表示域为中国,snowin是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.zr.testProj打头的,如果有个StudentDao,它的全路径就是cn.snowin.testProj.dao.StudentDao
maven常用命令:clean compile test package install deploy
maven三套生命周期:清理生命周期 默认生命周期 站点生命周期
导入jar包时冲突的解决:
方式一:第一声明有限原则,先声明的jar包坐标下的依赖包,可以优先进入项目中
方式二:路径近者优先原则,直接依赖比传递依赖的路径近。
直接依赖--项目中直接导入的jar包就是项目的直接依赖包。
传递依赖--项目中没有直接导入的jar包,可以通过项目的直接依赖传递进项目中。
方式三:直接排除法(推荐),<exclusions>中写明要排除的jar包,当我们要排除某个jar包下依赖包时,在exclusions中可以不写版本号。
一个可以直接使用的pom.xml文件
<!-- 统一管理jar包版本 -->
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<shiro.version>1.2.3</shiro.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
<spring.security.version>5.0.1.RELEASE</spring.security.version>
</properties>
<!--
maven工程可以分父子依赖关系的,凡是依赖别的项目后,拿到别的项目的依赖包都是传递依赖。
为了防止jar版本在传递依赖过程中的覆盖,我们可以把A项目中主要的jar包坐标导出,那么即使是直接依赖,也无法覆盖。只能起锁定的作用,不能进行jar包的导入
-->
<!-- 锁定jar包版本 dependencyManagement-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 项目依赖jar包 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</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-core</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
</dependencies>
<!-- 添加tomcat7插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
Maven高级
maven工程的拆分和聚合思想:
maven拆分思想:
案例:卖家和买家都需要看订单,都需要从数据库中查询数据,由dao层执行任务。想要使用的是可重用代码。
解决方式:把一个完整的项目,分成不同的独立模块,这些模块有自己各自的独立的坐标。那个地方需要其中的某个模块,直接引用该模块的坐标即可。今后,我们应该先考虑的问题应该是dao service utils domain这些模块是否已经存在,如果已经存在,那么可以直接引用。
maven聚合思想:
把零散的模块聚合到一起,编写一个完整的项目。
创建案例:
父子工程的创建:需要其中的pom.xml文件,可以不适用骨架进行创建
子模块的创建:
1. dao不与界面交互,不适用骨架创建 服pom中多了module,子pom中有parent标签
2. service的创建和dao类似
3. web层,与界面有交互,需要使用骨架进行创建(在其pom文件中,保留packaging以上的,其下面的可以全部删除)
工程和模块的关系以及继承和依赖的概念
工程!=完整的项目,模块也是,当代码完整时才可以说是一个完整的项目。
工程只能使用自己的内部资源,工程天生是独立的,后天可以可以和其他工程或者功能模块建立关联关系。
模块天生不是独立的,其属于父工程,模块一旦创建,父工程的资源模块都可以使用。
父子工程之间,子模块天生继承父工程,可以使用父工程中的资源。子模块之间天生没有任何关系,但是可建立关系,可在pom中加入坐标进行导入 在dependency中直接写就可以。父子之间的依赖关系不再需要建立。
平级直接的引用叫做依赖,需要后天建立。
传递依赖下来的包是否可用:比如junit在父pom中,但是其属性中有个<scope>,dao和Junit是传递依赖,作用域是test,对应好后发现传递丢失。那么test可能会报错。
启动maven:可以直接install父工程
私服
私服是一个特殊的远程仓库,它是架设在局域网内的仓库服务。私服代理广域网上的远程仓库,供局域网内的Maven用户使用。当Maven需要下载构建的使用,它先从私服请求,如果私服上没有的话,则从外部的远程仓库下载,然后缓存在私服上,再为Maven的下载请求提供服务。