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

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

问题描述




  • 如何使用servlet创建Java Web项目? ,jsp和其他类与Maven?

  • 它创建一个简单的目录结构,src-> main-> java。在哪里和如何放置web-inf文件夹?

  • 我需要将jdbc驱动程序手动添加到web-inf / lib中的文件夹中,还是只是指出依赖关系?

  • 有没有办法用junit测试servlet?


解决方案<哇,这是一个很多问题。我承认,使用Maven和Eclipse设置一个webapp项目可能会很棘手,所以我会尝试全部回答。



使用Maven创建一个Web应用程序




如何使用servlet jsp和其他类与maven一起创建一个java web项目?它创建一个简单的目录结构,src-> main-> java。


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



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

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

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



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



WEB-INF位置




inf文件夹?


默认情况下,Maven需要应该在WAR文件的根目录中的资源 - 例如图像,html页面和WEB -INF目录 - 驻留在 / src / main / webapp / 中。因此,WEB-INF文件夹应位于 / src / main / webapp / WEB-INF / 。如果您使用maven-archetype- webapp此目录自动创建,并附有一个示例web.xml文件。



Eclipse集成



Eclipse在问题标题中,确实有可能在Eclipse中使用m2eclipse插件开发Mavenized Web应用程序。 Eclipse通过WTP(Web Tools Platform)对Web应用程序有很好的支持。



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



依赖关系



< blockquote>

我需要手动将jdbc驱动程序添加到web-inf / lib中的文件夹中,还是只是指出依赖关系?


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

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

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



测试




有没有办法用junit测试servlet?




这个问题已经在Stackoverflow中被问及(并回答):





参考




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

  • 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. Where and how do I put the web-inf folder?
  • 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?
  • Is there a way to test the servlets with junit?

解决方案

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.

Creating a Web application project with Maven

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.

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.

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

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.

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 location

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

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 integration

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).

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.

Dependencies

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?

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>

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.

Testing

Is there a way to test the servlets with junit?

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

References

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

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