1、在Eclipse中新建项目
New——》Other.. ——》Maven——》Maven Project , 新建 org.apache.maven.archetypes = maven-archetype-webapp 的项目,如下图
项目的目录结构如下:
2、将项目构建成Web项目
Properties——》Project Facets——》Convert to facted form...
效果如下:
3、修改项目的目录结构
1) 将 src/main/webapp 目录下的文件 移动到 WebContent 目录下
2) 删除src目录下的package,只留下名字为src的Source Folder
3)添加test目录,该目录为Source Folder
4)添加log4j.properties文件到src目录下
最终项目的目录结构如下:
目录结构说明:
src: 存放java代码和项目的配置文件
test: 存放junit测试类
WebContent: 项目的Web App目录,存放项目的页面资源,如jsp、css、html等
4、修改pom.xml文件
注意:web项目的pom.xml中以下配置是必须的:
1)默认源代码目录、默认测试源代码目录、默认资源目录、默认测试资源目录
2)通过maven-dependency-plugin插件,把依赖的jar包复制到target/${project.artifactId}/WEB-INF/lib目录下
3)通过maven-war-plugin插件,指定WebApp目录
4)配置tomcat6-maven-plugin,当然你也可以使用其它的web容器,如JBOSS等
最终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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>terry.codex</groupId>
<artifactId>MavenWebProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MavenWebProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
<javax.servlet.version>2.5</javax.servlet.version>
<!-- 源代码Java编译版本 -->
<maven.compiler.source.version>1.7</maven.compiler.source.version>
<!-- 目标平台Java编译版本 -->
<maven.compiler.target.version>1.7</maven.compiler.target.version>
<org.apache.log4j.version>1.2.17</org.apache.log4j.version>
<javax.servlet.jsp.version>2.2</javax.servlet.jsp.version>
<!-- lombok插件版本号 -->
<lombok.version>1.18.2</lombok.version>
<!-- ProtoStuff插件 -->
<protostuff.version>1.4.0</protostuff.version>
<!-- Tomcat的jar包版本号 -->
<tomcat.embed.version>8.5.31</tomcat.embed.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope> <!-- scope=provided 在编译和测试的过程有效,最后项目打包时不会加入 -->
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${javax.servlet.jsp.version}</version>
<scope>provided</scope> <!-- scope=provided 在编译和测试的过程有效,最后项目打包时不会加入 -->
</dependency>
<!-- log4j日志打印包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${org.apache.log4j.version}</version>
</dependency>
<!-- lombok插件,编译时自动注入代码到字节码,使用方式注解方式:@Data、@NoArgsConstructor等等 -->
<!-- 用途:编译时,自动生成Bean的Getter、Setter方法,生成构造函数等等,提高开发效率,减少代码量 -->
<!-- 不使用该插件,也可以去掉这个依赖包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- ProtoStuff序列号和反序列化插件,不使用该插件,也可以去掉该依赖包 -->
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>${protostuff.version}</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>${protostuff.version}</version>
</dependency>
<!-- end -->
<!-- 加入Tomcat的依赖包,可以去掉,此处加上,只是为了项目能够调试Tomcat的代码 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.embed.version}</version>
<scope>provided</scope> <!-- scope=provided 在编译和测试的过程有效,最后项目打包时不会加入 -->
</dependency>
<!-- end -->
</dependencies>
<build>
<!-- 项目打包(packaging)后的文件名,去掉了后缀 -->
<finalName>${project.artifactId}</finalName>
<!--默认源代码目录 -->
<sourceDirectory>src</sourceDirectory>
<outputDirectory>${project.build.directory}/classes</outputDirectory> <!-- ${project.build.directory}就是我们通常看到的target目录 -->
<!--默认测试源代码目录 -->
<testSourceDirectory>test </testSourceDirectory>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<!--默认资源目录 -->
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<!--默认测试资源目录 -->
<testResources>
<testResource>
<directory>test</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 把依赖的jar包复制到target/${project.artifactId}/WEB-INF/lib目录下,在Web项目中这个是必须配置的 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}就是我们通常看到的target目录 -->
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>true</stripVersion>
<!-- 不复制scope=provided的依赖包 -->
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath> <!-- 告知 maven-jar-plugin 添加一个 Class-Path 元素到 MANIFEST.MF 文件 -->
<classpathPrefix>lib/</classpathPrefix> <!-- classpathPrefix 指出,相对存档文件,所有的依赖项应该位于 “lib” 文件夹 -->
<!--<mainClass>theMainClass</mainClass> --> <!-- 当用户使用 lib 命令执行 JAR 文件时,使用该元素定义将要执行的类名 -->
</manifest>
</archive>
</configuration>
</plugin>
<!-- 指定WebApp目录,在Web项目中这个是必须配置的 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
<warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
<webResources>
<resource>
<directory>WebContent\WEB-INF</directory>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<!-- Java Compile版本号设置 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 源代码Java编译版本 -->
<source>${maven.compiler.source.version}</source>
<!-- 目标平台Java编译版本 -->
<target>${maven.compiler.target.version}</target>
</configuration>
</plugin>
<!-- 配置tomcat6-maven-plugin,在Web项目中这个是必须配置的,当然你也可以使用其它的web容器,如JBOSS等 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- 打包成功后,运行tomcat服务 -->
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<!-- <configuration>的以下三个子字节点必须配置: -->
<!-- <additionalConfigFilesDir> 默认值是 ${basedir}\src\java\main\webapp\tomcatconf,不重新指定的话,启动tomcat时会报错,提示找不到 src\java\main\webapp目录-->
<!-- <contextFile> 默认值是 ${basedir}\src\java\main\webapp\tomcatconf\META-INF\context.xml,不重新指定的话,启动tomcat时会报错,提示找不到 src\java\main\webapp目录-->
<!-- <warSourceDirectory> 默认值是 ${basedir}\src\java\main\webapp,不重新指定的话,启动tomcat时会报错,提示找不到 src\java\main\webapp目录-->
<configuration>
<additionalConfigFilesDir>${basedir}\WebContent\tomcatconf</additionalConfigFilesDir>
<contextFile>${basedir}\WebContent\tomcatconf\META-INF\context.xml</contextFile>
<!-- <serverXml>${basedir}/tomcatconf/server.xml</serverXml> -->
<!-- 项目的WebApp路径 -->
<warSourceDirectory>${basedir}\WebContent</warSourceDirectory>
<!-- 项目的Context Root -->
<!-- <path>/${project.artifactId}</path> -->
<!-- 项目打包后的war包路径 -->
<!-- <warFile>${project.build.directory}/${project.build.finalName}.war</warFile> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- repositories 配置远程仓库 -->
<!-- 1、先从settings.xml配置的仓库<repositories>中下载构建(即jar包),如果没有该构建,则从pom.xml的<repositories>中下载-->
<!-- 2、构建的下载,按照配置的仓库的先后顺序来查找和下载-->
<!-- 3、【特别注意】 如果 settings.xml配置了mirrors镜像仓库,则该配置无效!【不】会从pom.xml的<repositories>中下载,只会从mirrors镜像仓库中查找下载!-->
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>Central2</id>
<name>Central Repository</name>
<url>http://central.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>Central2</id>
<name>Central Repository</name>
<url>http://central.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
5、运行项目
Run As——》Run Configurations..——》Maven Build——》右键New
运行效果:
......
[DEBUG] -- end configuration --
[INFO] Running war on http://localhost:8080/MavenWebProject
[INFO] Using existing Tomcat server configuration at D:\EclipseWorkspace\MavenWebProject\target\tomcat
[DEBUG] context reloadable: false
[DEBUG] adding classPathElementFile file:/D:/EclipseWorkspace/MavenWebProject/target/classes/
[DEBUG] add dependency to webapploader log4j:log4j:1.2.17:compile
[DEBUG] context reloadable: false
[DEBUG] start tomcat instance on http port:8080 and protocol: HTTP/1.1
六月 24, 2017 4:11:29 下午 org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
六月 24, 2017 4:11:29 下午 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.37
六月 24, 2017 4:11:29 下午 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
六月 24, 2017 4:11:29 下午 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
6、重命名项目
不想改项目名称的话,这个步骤可以忽略!!
最终,发现项目名字凸显不出来是demo,那我就重新命名项目,步骤如下:
1)Refactor——》Rename...
2)修改pom.xml文件的<artifactId>、<name>,将值修改成项目重命名后的项目名称,修改之后内容如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>terry.codex</groupId>
<artifactId>MavenWebProjectDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MavenWebProjectDemo</name>
<url>http://maven.apache.org</url>