`
sundful
  • 浏览: 1253100 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Creating Dynamic Web Project using Maven in Eclipse

    博客分类:
  • Java
阅读更多

While using Maven as build tool in our project, I found it very difficult to create a Dynamic Web Project which supports Maven dependencies and can execute in Eclipse! I have seen lot of people using Maven just as build tool and for local setup uses jar files in some /lib directory. I wanted to remove this dependencies on local /lib for jar files and resolve everything with Maven. My project should be a Dynamic web project with Maven dependencies enabled.

Here is a simple tutorial which you can go through to create Dynamic Web Project having Maven enabled in Eclipse. This project can be used as base project and can be easily converted to most kind of project like Struts based, Spring MVC based etc.

Required Tools

For this tutorial, I assume you have following setup in your machine.
1. JDK 1.5 or above (download)
2. Eclipse 3.2 or above (download)
3. Maven 2.0 or above (download)
4. M2Eclipse Plugin (download)

Step 1: Create Maven Project in Eclipse

Create a new project in Eclipse. Goto File > New > Project.. and select Maven Project from the list. Click Next.
eclipse-maven-new-project

Enter “MavenWeb” as Project name and click Next. On Configuration screen, select war in Packaging and also check the checkbox for src/main/webapp.

new-maven-project

Once done, click Finish. This will create a Maven project in Eclipse.

Step 2: Generate Eclipse Project with WTP

Let us convert the Maven project to Dynamic Web Project for Eclipse. For this we will use following maven command.

 
1 mvn eclipse:eclipse -Dwtpversion=1.5

Goto the folder where the new project is created and execute above command.

C:\Workspace\Test\MavenWeb>mvn eclipse:eclipse -Dwtpversion=1.5

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - MavenWeb:MavenWeb:war:0.0.1-SNAPSHOT
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse]
[INFO] Adding support for WTP version 1.5.
[INFO] Using Eclipse Workspace: C:\Workspace\Test
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v5.5
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "MavenWeb" to C:\Workspace\Test\MavenWeb.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Wed Jul 28 13:55:09 CEST 2010
[INFO] Final Memory: 7M/30M
[INFO] ------------------------------------------------------------------------

That’s it. We just created Dynamic Web Project from Maven project. Now refresh the project in Eclipse.

Step 3: Change Project Facet

The above step by default set the project facet to JDK 1.4. We need to modify this and set project facet to JDK 5. Right click on MavenWeb project and select Properties (shortcut: Alt+Enter). From the Properties dialog box, select Project Facets. Now click on “Modify Project…” button and change the project facet to Java 5.0
project-facet-maven-eclipse

Step 4: Setting Build Path

We need to specify the Maven jar dependencies in Java Build Path of our MavenWeb project. Open Properties.. dialog box (Alt+Enter) and select Java Build Path. Click “Add Library..” button and select “Maven Managed Dependencies” and click Finish.
maven-eclipse-build-path

Step 5: Hello World Servlet

Our dynamic web project with maven support is done now. Let us add a small Hello World servlet to this project and see how it works.

Open pom.xml from root folder and copy following content into it.

File: pom.xml

 
01 <?xml version="1.0" encoding="UTF-8"?>
02 <project>
03     <modelVersion>4.0.0</modelVersion>
04     <groupId>MavenWeb</groupId>
05     <artifactId>MavenWeb</artifactId>
06     <packaging>war</packaging>
07     <version>0.0.1-SNAPSHOT</version>
08     <description></description>
09     <build>
10         <plugins>
11             <plugin>
12                 <artifactId>maven-war-plugin</artifactId>
13                 <version>2.0</version>
14             </plugin>
15         </plugins>
16     </build>
17     <dependencies>
18         <dependency>
19             <groupId>javax.servlet</groupId>
20             <artifactId>servlet-api</artifactId>
21             <version>2.5</version>
22         </dependency>
23     </dependencies>
24 </project>

Also create a servlet file HelloWorldServlet.java. We will add this servlet in net.viralpatel.maven package.

File: /src/main/java/net/viralpatel/maven/HelloWorldServlet.java

 
01 package net.viralpatel.maven;
02   
03 import java.io.IOException;
04 import java.io.PrintWriter;
05   
06 import javax.servlet.ServletException;
07 import javax.servlet.http.HttpServlet;
08 import javax.servlet.http.HttpServletRequest;
09 import javax.servlet.http.HttpServletResponse;
10   
11 public class HelloWorldServlet extends HttpServlet {
12   
13     private static final long serialVersionUID = 1031422249396784970L;
14   
15     public void doGet(HttpServletRequest req, HttpServletResponse resp)
16             throws ServletException, IOException {
17   
18         resp.setContentType("text/html");
19   
20         PrintWriter out = resp.getWriter();
21         out.print("Hello World from Servlet");
22         out.flush();
23         out.close();
24     }
25 }

Once the servlet is created, let us configure this in web.xml. Note that in our maven project no web.xml is present. We will create one at /src/main/webapp/WEB-INF/ location.

File: /src/main/webapp/WEB-INF/web.xml

 

01 <?xml version="1.0" encoding="UTF-8"?>
02 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03     xmlns="http://java.sun.com/xml/ns/javaee"
04     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
05     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
06     id="WebApp_ID" version="2.5">
07     <display-name>HelloWorldServlet</display-name>
08     <welcome-file-list>
09         <welcome-file>hello-world</welcome-file>
10     </welcome-file-list>
11   
12     <servlet>
13         <servlet-name>HelloWorldServlet</servlet-name>
14         <servlet-class>
15             net.viralpatel.maven.HelloWorldServlet
16         </servlet-class>
17         <load-on-startup>1</load-on-startup>
18     </servlet>
19     <servlet-mapping>
20         <servlet-name>HelloWorldServlet</servlet-name>
21         <url-pattern>/hello-world</url-pattern>
22     </servlet-mapping>
23 </web-app>

That’s All Folks

Our dynamic web project with Maven support in Eclipse is completed. Run the web project in eclipse (Alt+Shirt+X, R)
hello-world-maven-eclipse-web-project

分享到:
评论

相关推荐

    如何在eclipse jee中创建Maven project并且转换为Dynamic web project

    在Eclipse JEE环境下,创建一个Maven项目并将其转换为Dynamic Web Project是开发Java Web应用程序的常见步骤。Maven是一种强大的项目管理工具,而Dynamic Web Project则是Eclipse中的一个特殊项目类型,专用于Web...

    MAVEN包和插件

    其次,"Creating Dynamic Web Project using Maven in Eclipse.pdf"则可能涵盖了如何在Eclipse中使用Maven创建动态Web项目。在Java Web开发中,Maven能够帮助我们构建Web应用程序,管理其生命周期,并自动下载所需的...

    Eclipse Tomcat Dynamic Web Project

    【Eclipse Tomcat Dynamic Web Project】是一个用于开发和部署Java Web应用程序的工作流程,它整合了Eclipse IDE的强大功能和Apache Tomcat服务器的便捷性。在Eclipse中创建一个Dynamic Web Project,开发者可以轻松...

    maven-eclipse-plugin

    【maven-eclipse-plugin】是Maven生态中的一个重要插件,它主要负责将Maven项目导入Eclipse这样的IDE中,以便于开发人员进行便捷的开发和管理。在使用Maven时,理解并熟练掌握这个插件的使用对于提高开发效率至关...

    Maven 2 Eclipse Plugin

    - 使用Eclipse的“Maven-&gt;Update Project Configuration”功能,确保Eclipse项目配置与POM.xml保持一致。 总之,Maven 2 Eclipse Plugin是Java开发者在Eclipse中高效使用Maven的关键工具,它消除了两个强大工具之间...

    eclipse创建maven dynamic web 3.0项目教程

    本文档记录了本人用eclipse搭建maven dynamic web3.0的详细过程,包含了搭建maven项目,把maven项目转换为web项目,Dynamic web Module 2.3转换为3.0等内容

    Maven 与 Eclipse 的集成

    **Maven 与 Eclipse 的集成** 在软件开发领域,Maven 和 Eclipse 是两个非常重要的工具。Maven 是一个项目管理和综合工具,它主要用于构建、管理和部署 Java 项目,通过使用一组标准化的构建过程和依赖管理机制。...

    SSM+MAVEN+ECLIPSE

    SSM+MAVEN+ECLIPSE是一个常见的Java Web开发框架组合,主要由Spring、Spring MVC(简称SM)、MyBatis(简称M)以及Maven构建工具集成。这个项目旨在提供一个快速查询数据库的功能,适用于初学者和开发者进行实践和...

    Maven和Eclipse集成

    1. **创建Maven项目**:在Eclipse中选择“File”&gt;“New”&gt;“Project”,然后选择Maven相关的项目类型。 2. **配置Maven项目**:根据向导的提示配置项目的基本信息,如项目名称、组ID、版本号等。 3. **构建项目**:...

    maven-eclipse_plugin.zip

    2. **创建Maven项目**:安装完成后,可以在Eclipse中通过“File” &gt; “New” &gt; “Other” &gt; “Maven Project”创建Maven项目,选择对应的Maven坐标(GroupId、ArtifactId、Version)。 3. **导入现有Maven项目**:...

    Developing with Maven & Eclipse

    《Developing with Maven & Eclipse》是一本专注于使用Maven和Eclipse进行软件开发的指南。Maven和Eclipse是Java开发领域中两个非常重要的工具,它们的结合使用可以帮助开发者更高效地构建、管理和调试项目。 Maven...

    Eclipse中创建Web Maven Project并部署到Tomcat中

    1. 打开Eclipse,选择“File” -&gt; “New” -&gt; “Dynamic Web Project”。 2. 在新项目向导中,为项目命名,确保“Target Runtime”已选择Tomcat服务器。 3. 在“Project Facets”选项卡中,勾选“Dynamic Web Module...

    maven2 eclipse

    4. **构建和运行**:现在可以在 Eclipse 内部使用 Maven 命令,如 "Maven &gt; Update Project" 或 "Maven Build",进行构建和测试。 5. **使用 Maven 编译和构建**:Eclipse 会根据 Maven 的生命周期阶段自动构建项目...

    Maven+Eclipse plugin

    4. **项目对象模型(Project Object Model, POM)**:POM是Maven的核心,存储项目信息和配置。 **Eclipse 插件介绍** Eclipse是一款流行的开源集成开发环境,广泛用于Java开发。Eclipse Maven插件(M2E)是Eclipse...

    Maven在eclipse中的项目启动命令

    ### Maven在Eclipse中的项目启动命令详解 #### 一、Maven简介 Maven是一款流行的自动化构建工具,它主要用于Java项目的构建与管理。Maven通过一个中心信息文件`pom.xml`来管理项目的构建过程、依赖关系以及其他...

    eclipse+egit安装使用+maven安装项目搭建

    三、Dynamic Web project 转成 Maven 项目 161、新建 Dynamic Web Project 若已有 Dynamic Web Project,可以将其转换为 Maven 项目。右键点击项目,选择 Configure &gt; Convert to Maven Project。 四、Git 安装和...

    如何基于maven在eclipse下创建web工程

    ### 如何基于Maven在Eclipse下创建Web工程 #### Maven与Eclipse简介 Maven是Apache软件基金会的一个开源项目,它是一个项目管理和综合工具,主要用于Java项目的构建、依赖管理和项目信息管理。通过Maven,开发者...

    maven3 eclipse plugin

    6. **生命周期映射**:Maven的生命周期映射到Eclipse的操作,如“Build”菜单中的“Build Project”对应于Maven的“install”目标。 7. **同步POM**:如果在命令行中修改了POM.xml,插件会检测到变化并自动同步到...

Global site tag (gtag.js) - Google Analytics