maven简介
什么是maven
Apache下的一个开源的项目,是纯java编写,并且只是用来管理java项目
maven的好处
maven项目节省空间
传统项目:
maven管理的项目:
分析:
一键构建
用一个命令(tomcat:run)让项目运行起来
可以跨平台
maven使用java编写的,java可以跨平台,maven也可以
应用于大型项目可以提高开发效率
maven的安装
maven工程目录结构
演示命令
编译项目(mvn compile)
编译测试(mvn test)
打包(mvn package)
打包到本地仓库中(mvn install)
删除编译之后的文件(mvn clean):
生命周期
clean生命周期
default生命周期
compile-->test-->package-->install-->deploy
site生命周期
构建入门程序
新建maven项目
- Ctrl+N,输入maven:
-
跳过骨架
- 创建项目:
几个注意的点:
-
报错
- 出现的原因:web项目没有web.xml文件
- 解决方式:
结果:
修改JDK版本
- 全局修改
打开apache-maven-3.3.9\conf\settings.xml文件,添加一下代码:
<profile>
<id>jdk-1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
<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>
- 局部修改
在pom.xml文件中修改
<build>
<!-- 配置了很多插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
- 刷新maven项目
- 结果
依赖管理
依赖范围
compile | provided | test | runtime | system | |
编译 | Y | Y | |||
测试 | Y | Y | Y | ||
打包 | Y | Y | Y | ||
运行 | Y | Y | Y |
运行调试
创建一个maven项目
加入依赖
<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.itheima</groupId>
<artifactId>crm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
</dependencies>
</project>
设置struts2核心拦截器
<?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>crm</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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
加入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>
<!-- 配置常量 -->
<!-- 字符集 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 通用package -->
<package name="customer" namespace="/" extends="struts-default">
<action name="zhenhun" class="com.itheima.web.action.CustomerAction" method="findCustomerById">
<result name="success">/jsp/info.jsp</result>
</action>
</package>
</struts>
编写代码
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
private Long custId;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String findCustomerById() {
System.out.println(custId);
return SUCCESS;
}
}
编写页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 style="color: green">第一次访问maven</h1>
</body>
</html>
测试运行
http://localhost:8080/crm/zhenhun?custId=2
结果
总结
常见问题
出现以下问题,要重新安装JDK