Maven的Apache公司开源项目,是项目构建工具。用来依赖管理
maven的好处
项目大小


同样的代码,实现功能都一样,maven项目如何做到的?可以初步推断maven项目中一定没有jar包。没有jar包的maven项目如何运行?
Maven项目找jar包过程

甚至可以继承另一个工程
实现
maven的两大核心:
依赖管理:对jar包管理过程
项目构建:项目在编码完成后,对项目进行编译、测试、打包、部署等一系列的操作都通过命令来实现
通过maven命令将web项目发布到tomcat:

Maven的安装
下载安装


maven是纯java开发的
配置环境变量

将maven_home环境配置到path环境变量中

检验,查询maven版本信息

配置本地仓库
仓库类型

配置本地仓库
-
找到jar包仓库压缩包
解压到本地磁盘
-
配置本地仓库:让maven程序知道仓库在哪
maven项目标准目录结构

导入项目



maven的常用命令
clean:清理
将项目根目录下target目录清理掉。里面都是编译文件.class

工作的jar包

compile:编译
将项目中.java文件编译为.class文件


test:单元测试
单元测试类名有要求:XxxxTest.java
将项目根目录下src/test/java目录下的单元测试类都会执行。


package:打包
web project ---- war包
java project -----jar包
将项目打包,打包项目根目录下taget目录


生成war包

install:安装
解决本地多个项目公用一个jar包。
打包到本地仓库


打包进了本地仓库

如果是java项目打的jar包就有了更大的用处
在我们后面进行操作时,前面的操作每次都会执行,这就是maven的生命周期
maven的生命周期
在maven中存在“三套”生命周期,每一套生命周期相互独立,互不影响。在一套生命周期内,执行后面的命令前面操作会自动执行
CleanLifeCycle:清理生命周期
Clean
defaultLifeCycle:默认生命周期
compile,test,package,install,deploy
siteLifeCycle:站点生命周期
site
maven整合web项目案例
配置eclipse中maven环境
- 配置m2e插件,Mars2版本自带maven插件

- 需要配置maven程序


- 配置userSetting:让eclipse知道maven仓库位置

- 构建索引
先显示仓库视图

构建索引


Maven整合servlet




-
补全xml文件
-
改变jdk版本
但是maven-更新后又回到了原来的1.5
修改pom.xml
<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>cn.itcast</groupId>
<artifactId>01_helloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 添加项目jdk编译插件 -->
<build>
<plugins>
<!-- 设置编译版本为1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
此时报错,让我们更新一下项目

更新


此时报错消失,怎么更新都没问题了
前面选择简单构建项目,避免自己选模块,但是仍旧有很多不足。
- 创建servlet
报错

缺失servlet-api-xx.jar包
查找依赖
注意:选择依赖选择[jar]

此时不报错了
pom.xml
<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>cn.itcast</groupId>
<artifactId>01_helloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 添加项目jdk编译插件 -->
<build>
<plugins>
<!-- 设置编译版本为1.8 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>`
依赖范围

添加依赖范围:
默认是compile
Provided: 运行部署到tomcat不在需要
如果将servlet-api.jar设置为compile,打包后包含serlvet-api.jar,war包部署到tomcat跟tomcat中存在servlet-api.jar包冲突。导致运行失败。

解决

如果使用到tomcat自带jar包,将项目中依赖作用范围设置为:provided,其他可以默认
运行
HelloMaven.java



此时运行不借助eclipse了

调试

Maven整合struts2
通过工具生成xml

添加插件


pom.xml
<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>cn.itcast</groupId>
<artifactId>02_maven_struts2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<!-- 设置详细信息 -->
<configuration>
<cource>1.8</cource>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
修改完需要更新项目
添加struts2的依赖

此时导入一个全来

创建struts2的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo" namespace="/" extends="struts-default">
<action name="customerAction_*" class="cn.itcast.web.action.CustomerAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
创建Action类
package cn.itcast.web.action;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
//url : http://locahost:8080/02_maven_struts2/customerAction_save.action
public String save() throws Exception {
System.out.println("CustomerAction的save方法被调用了");
return SUCCESS;
}
}
在web.xml配置核心过滤器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>02_maven_struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts2框架核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
终于成功
终于成功
终于成功
哈哈哈

因为我们使用tomcat:run默认使用tomcat6,而tomcat6不支持jdk8,我们需要使用tomcat7:run
所以要在pom.xml添加如下代码,必须2.2版本
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
pom.xml
<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>cn.itcast</groupId>
<artifactId>02_maven_struts2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<!-- 设置详细信息 -->
<configuration>
<cource>1.8</cource>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
终于成功,搞了1个小时~~~
概念模型
两个核心:
依赖管理:对jar包管理
项目构建:通过命令进行项目构建




