经过了前两篇文章的了解:
- https://blog.hackyle.net/index.php/tools/maven-basic/
- https://blog.hackyle.net/index.php/tools/maven-pom/
接下来是实际项目中的实践(按图索骥),这也许是最有意思的!
Eclipse
配置Maven
Eclipse也内置了一个Maven,但是不稳定、不能够自定义设置,所以不要使用Eclipse内置的Maven。
点击Window-Preferences:
注意:每次创建项目都要设置Maven的参数!
创建JavaSE工程
成功创建
管理工程:
修改默认JDK
<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>
创建JavaWeb工程
创建完毕,但在webapp目录下没有WEB-INF等目录,且pom.xml文件还报错,所以这不是一个完整的web工程。
在webapp目录下配置WEB-INF目录:首先选中项目,右键属性:
点击Further Configuration available
完整目录结构的Web项目:
IDEA
配置Maven
IDEA中内置了Maven ,一般不使用内置的,因为用内置修改Maven的设置仓库位置不方便。进入设置页面:
配置运行参数:
-DarchetypeCatalog=internal
注意:为了使的这个设置在以后创建Maven项目时都生效,需要进行全局设置
进入配置对话框后,按照上文进行配置即可
创建JavaSE项目
File-New Project:
因为要创建的是JavaSE工程,所以选择maven-archetype-quickstart
创建成功:
创建resources目录:
pom.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标签值是固定的,不可更改-->
<modelVersion>4.0.0</modelVersion>
<!--自己项目的坐标(gav,三个标签的首字母)-->
<groupId>net.hackyle</groupId>
<artifactId>HelloMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--项目在Web上的展示名字和URL地址-->
<name>HelloMaven</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!--设置Maven的属性-->
<properties>
<!--设置编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--编译Java代码的JDK版本-->
<maven.compiler.source>1.7</maven.compiler.source>
<!--代码运行的JDK版本,与上一个标签中的版本保持一致-->
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--设置依赖,即管理项目中所用到的Jar包-->
<dependencies>
<!--Junit单元测试的依赖(Jar)包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--Maven插件的管理,即时不写也会默认使用以下指定的插件,所以可以直接删除build标签-->
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
项目管理
创建JavaWeb项目
补全目录:
如果文件夹不能自动识别,选中文件夹,右键,执行以下设置:
pom.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>
<!--自己项目的坐标(gav,三个标签的首字母)-->
<groupId>net.hackyle</groupId>
<artifactId>MavenWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<!--web项目的打包方式为war-->
<packaging>war</packaging>
<!--项目在Web上的展示名字和URL地址-->
<name>MavenWeb Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!--设置Maven的属性-->
<properties>
<!--设置编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--编译Java代码的JDK版本-->
<maven.compiler.source>1.7</maven.compiler.source>
<!--代码运行的JDK版本,与上一个标签中的版本保持一致-->
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--设置依赖,即管理项目中所用到的Jar包-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--Maven插件的管理,即时不写也会默认使用以下指定的插件,所以可以直接删除build标签-->
<build>
<finalName>MavenWeb</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
在pom.xml中加入Servlet依赖
在以前的Web项目,我们需要配置Tomcat或其他中间件,才能正常使用Servlet,而使用了Maven以后,直接加入Servlet依赖即可:
<?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>
<!--自己项目的坐标(gav,三个标签的首字母)-->
<groupId>net.hackyle</groupId>
<artifactId>MavenWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<!--web项目的打包方式为war-->
<packaging>war</packaging>
<!--设置Maven的属性-->
<properties>
<!--设置编码-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--编译Java代码的JDK版本-->
<maven.compiler.source>1.7</maven.compiler.source>
<!--代码运行的JDK版本,与上一个标签中的版本保持一致-->
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<!--设置依赖,即管理项目中所用到的Jar包-->
<dependencies>
<!--Junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Servlet依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<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>
写一个JSP和Servlet
Index.jsp
<%@page contentType="text/html; charset=utf-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h2>This is a Maven Web Project</h2>
<a href="<%=request.getContextPath()%>/hello">点击请求一个Servlet</a>
</body>
</html>
HelloServlet
package net.hackyle.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
System.out.println("HelloServlet开始执行");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
doGet(request,response);
}
}
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>
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>net.hackyle.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
配置ProjectStructure
进入facets,设置web属性:
在弹出的对话框中选择当前项目:
随后,选择页面最下面的“Create Artifact”,注意不要配置其他的:
进入Artifact后,执行以下操作:
继续选择当前模块名后,结果:
然后回到Modules,配置部署参数:
如果上述配置在运行时不成功,则尝试以下配置:
方案一:配置部署参数为Maven打包后的路径
执行Maven的打包命令,会在当前目录生成该项目的target文件夹:
进入ProjectStructure:
方案二:配置部署参数为Artifact的输出路径
配置Tomcat
Server的参数:
配置部署:
运行成功:
点击该超链接后: