1、在idea中设置maven
idea中内置了maven ,一般不使用内置的, 因为用内置修改maven的设置不方便。
使用自己安装的maven, 需要覆盖idea中的默认的设置。让idea指定maven安装位置等信息配置的入口
①:配置当前工程的设置, file--settings ---Build, Excution,Deployment--Build Tools
--Maven
Maven Home directory: maven的安装目录
User Settings File : 就是maven安装目录conf/setting.xml配置文件
Local Repository : 本机仓库的目录位置--Build Tools--Maven--Runner
VM Options : -DarchetypeCatalog=internal
JRE: 你项目的jdkarchetypeCatalog=internal , maven项目创建时,会联网下载模版文件,比较大, 使用-DarchetypeCatalog=internal,不用下载, 创建maven项目速度快。
②:配置以后新建工程的设置, file--other settings--Settings for New Project
2、依赖范围, 使用scope表示的
scope的值有 compile, test, provided ,默认是compile
scope:表示依赖使用的范围,也就是在maven构建项目的那些阶段中起作用。
maven构建项目 编译, 测试 ,打包, 安装 ,部署 过程(阶段)
junit的依赖范围是 test
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
3、 maven常用操作
1.maven的属性设置
<properties> 设置maven的常用属性
2.maven的全局变量
自定义的属性,1.在<properties> 通过自定义标签声明变量(标签名就是变量名)
2.在pom.xml文件中的其它位置,使用 ${标签名} 使用变量的值自定义全局变量一般是定义 依赖的版本号, 当你的项目中要使用多个相同的版本号,先使用全局变量定义, 在使用${变量名}
3.资源插件<build> <resources> <resource> <directory>src/main/java</directory><!--所在的目录--> <includes><!--包括目录下的.properties,.xml 文件都会扫描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!—filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 --> <filtering>false</filtering> </resource> </resources> </build>
4、一个简单的maven-javaweb
(1)设置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>org.example</groupId>
<artifactId>ch03-maven-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<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>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 加入servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp的依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
(2)创建一个servlet
src/main/webapp/WEB-INF/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>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.yinli.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
src/main/java/com/yinli/HelloServlet
package com.yinli;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.println("Hello Maven Web");
pw.flush();
pw.close();
}
}
(3)页面调用
src/main/webapp/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="hello">访问servlet</a>
</body>
</html>