Maven新建Web项目(开发平台为Eclipse)
来源:互联网 发布:win10系统优化软件 编辑:程序博客网 时间:2024/06/05 18:38
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></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></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></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></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 startINFO: Starting tomcat server六月 24, 2017 4:11:29 下午 org.apache.catalina.core.StandardEngine startINFO: Starting Servlet Engine: Apache Tomcat/6.0.37六月 24, 2017 4:11:29 下午 org.apache.coyote.http11.Http11Protocol initINFO: Initializing Coyote HTTP/1.1 on http-8080六月 24, 2017 4:11:29 下午 org.apache.coyote.http11.Http11Protocol startINFO: 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>
7、项目下载地址
https://github.com/zengyh/MavenWebProjectDemo.git
- Maven新建Web项目(开发平台为Eclipse)
- Eclipse新建Maven web项目
- eclipse新建maven web项目
- Eclipse新建maven web项目
- eclipse 新建maven web项目
- Eclipse 新建maven web项目
- 新建web项目出现的问题(eclipse+maven)
- Eclipse中使用Maven新建Web项目
- eclipse新建maven web项目流程
- eclipse新建web maven springMvc项目
- eclipse中maven插件新建web项目
- eclipse 新建maven动态web项目
- Maven入门1-在Eclipse中新建Maven Web项目
- eclipse新建maven项目
- maven 新建web项目
- maven新建web项目
- Maven新建Web项目
- eclipse改变maven项目为web项目
- ubuntu下ftp服务器的配置
- css排版和元素分类
- opencv学习笔记(6)----鼠标操作
- 2017年四川省赛 -- G题 2017 【容斥定理】
- 【CUDA开发】 Check failed: error == cudaSuccess (8 vs. 0) invalid device function
- Maven新建Web项目(开发平台为Eclipse)
- Java+Selenium3方法篇22-利用Javascript处理竖向滚动条
- 从数据集合中深层次条件遍历元素
- python使用numpy向量运算注意点
- (转)Thrift在Windows及Linux平台下的安装和使用示例
- 动态规划训练15 [Monkey and Banana HDU
- windows 提权
- python正则表达式全面理解笔记
- TCP报文段及三次握手四次挥手过程