使用 Maven 和 Eclipse 的 Java 动态 Web 项目 [英] Java Dynamic Web project with Maven and Eclipse

查看:15
本文介绍了使用 Maven 和 Eclipse 的 Java 动态 Web 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个关于使用 Maven 和 Eclipse 创建 Java Web 应用程序的问题:

I have several questions about creating a Java Web application with Maven and Eclipse:

  • 如何使用 Maven 创建带有 servlet、jsp 和其他类的 Java Web 项目?
  • 它创建了一个简单的目录结构,src->main->java.我该如何放置 web-inf 文件夹?
  • 我是否需要手动将 jdbc-drivers 添加到 web-inf/lib 内的文件夹中,还是仅仅指出依赖关系就可以了?
  • 有没有办法用 junit 测试 servlet?

推荐答案

哇,一下子有很多问题.我承认使用 Maven 和 Eclipse 设置 webapp 项目可能会很棘手,所以我会尽量一一解答.

Wow that's a lot of questions at once. I admit that setting up a webapp project with Maven and Eclipse can be tricky, so I'll try to answer them all.

我如何使用 servlets jsp 和其他类使用 maven 创建一个 java web 项目?它创建了一个简单的目录结构,src->main->java.

How do I create a java web project with servlets jsp and other classes with maven? It creates a simple directory structure, src->main->java.

当您创建 Java Web 项目时,最终产品应该是 WAR 或 EAR 文件.WAR 和 EAR 文件是具有特定结构的 JAR 文件,可以部署在应用服务器或 servlet 容器中.

When you are creating a Java web project, the final product should be a WAR or EAR file. WAR and EAR files are JAR files with a specific structure that can be deployed in an application server or servlet container.

如前所述,为 Web 应用程序设置 Maven 项目的最简单方法是使用原型:

As mentioned, the easiest way to set up a Maven project for web applications is to use archetypes:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

如果我们使用此原型创建一个项目,则会生成一个简单的目录结构和 pom.xml.该项目遵循您提到的标准 Maven 目录布局,具有 /src/main/java//src/test/java 等.Maven 从中生成 WAR 文件结构与战争:战争目标.

If we create a project with this archetype then a simple directory structure and pom.xml are generated. This project follows the standard Maven directory layout you mention, with /src/main/java/, /src/test/java, etc. Maven generates the WAR file from this structure with the war:war goal.

请注意,此原型适用于非常简单(过时)的 Web 应用程序,但它是最好的可用起点.您可能希望放弃 web.xml 文件并创建一个支持 Servlet 3.0 的新文件.

Note that this archetype is for a very simple (outdated) web application, but it's the best available starting point. You probably want to discard the web.xml file and create a new one that supports Servlet 3.0.

在哪里以及如何放置 web-inf 文件夹?

Where and how do I put the web-inf folder?

默认情况下,Maven 期望应该在 WAR 文件的根目录中的资源——例如图像、html 页面和 WEB-INF 目录——驻留在 /src/main/webapp/代码>.所以WEB-INF文件夹应该位于/src/main/webapp/WEB-INF/.如果你使用maven-archetype-webapp这个目录是自动创建的,以及示例 web.xml 文件.

By default, Maven expects resources that should go in the root of the WAR file -- such as images, html pages and the WEB-INF directory -- to reside in /src/main/webapp/. So the WEB-INF folder should be located at /src/main/webapp/WEB-INF/. If you use the maven-archetype-webapp this directory is automatically created, along with a sample web.xml file.

你在问题​​标题中提到了Eclipse,确实可以在Eclipse中使用m2eclipse插件开发Mavenized web应用程序.Eclipse 通过 WTP(Web Tools Platform)对 Web 应用程序有很好的支持.

You mentioned Eclipse in the question title, and it is indeed possible to develop Mavenized web applications in Eclipse with the m2eclipse plugin. Eclipse has good support for Web applications through WTP (Web Tools Platform).

尽管互联网上的许多指南(错误地)推荐它,您不应该使用 mvn eclipse:eclipse 命令来创建 Eclipse 项目.这个插件只能为非常旧的 Eclipse 版本生成 WTP 项目(WTP 2.0 是最大的).相反,使用 m2eclipse 插件,如 此处.

Although many guides on the internet (wrongly) recommend it, you should not use the mvn eclipse:eclipse command to create the Eclipse project. This plugin can only generate WTP projects for very old Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described here.

我是否需要手动将 jdbc-drivers 添加到 web-inf/lib 内的文件夹中,还是仅仅指出依赖关系就可以了?

Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?

无需手动执行此操作,因为 Maven 的主要优势之一是依赖项管理.如果在 pom.xml 中添加一个范围为 compileruntime 的依赖项,JAR 文件将自动包含在 WEB-INF/lib/ WAR 文件的目录.例如,要添加 Postgresql JDBC 驱动程序依赖项,请将其添加到您的 pom.xml 中:

There is no need to do this manually, since one of the key strengths of Maven is dependency management. If you add a dependency in the pom.xml with a scope of compile or runtime, the JAR file will be automatically included in the WEB-INF/lib/ directory of the WAR file. For example to add the Postgresql JDBC driver dependency, add this to your pom.xml:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>

由于未指定范围,Maven 将假定它在默认范围编译.结果是Maven将在WAR文件中包含WEB-INF/lib/postgresql-9.1-901.jdbc4.jar.

Since the scope is unspecified Maven will assume it is in the the default scope compile. The result is that Maven will include WEB-INF/lib/postgresql-9.1-901.jdbc4.jar in the WAR file.

有没有办法用junit测试servlets?

Is there a way to test the servlets with junit?

此问题已在 Stackoverflow 上提出(并回答):

This question has been asked (and answered) on Stackoverflow:

这篇关于使用 Maven 和 Eclipse 的 Java 动态 Web 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆