前置工作
- 本机安装maven
- Idea 设置使用本机maven 工具
Settings--->Maven
开始创建maven项目
1. 创建maven项目,勾选通过模板创建,选择 maven-archetype-webapp 模板
GroupId: 公司名倒序
ArtifactId: 项目名
设置本地maven仓库配置
项目文件显示名,和项目名一致
设置maven jar包自动导入
手动导入
打开setting→maven→importing:
选择Import Maven projects automatically
等待结果
2. 检查项目的maven本地配置和仓库
设置参数,加快jar包下载速度 可选
maven – Runner
archetypeCatalog -- internal
4. 确认或修改maven支持的JDK版本为1.8
打开pom.xml文件
5. 构建maven目录,创建对应文件夹
并设置src\main\java为代码源目录
6. 设置src\main\resources为资源文件源目录后期原config下的配置文件,复制到resources下,并修改对应路径
更换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_3_0.xsd"
version="3.0">
<display-name>Test_Maven2</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>
</web-app>
新增目录结构如下
7. 添加tomcat组件
Run – Edit Configuration
点击Deployment,部署当前maven项目
设定url访问的根目录 http://localhost:6060/
启动tomcat测试一下
其它设置:
idea-maven不识别.xml文件,添加如下代码到pom.xml文件内
pom.xml文件配置jar包
<?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>com.gxy</groupId>
<artifactId>My_Maven1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging><name>My_Maven1 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url><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>
</dependencies><build>
<finalName>My_Maven1</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><!--配置Maven 对resource文件 过滤 -->
<resources>
<resource>
<directory>src/main/resource</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources></build>
</project>
官方maven坐标下载地址: