首页 文章

Eclipse WTP不发布Maven依赖项

提问于
浏览
20

我'm trying to set up a basic hello world project using Eclipse Indigo and a Tomcat server. I created a dynamic project with a simple servlet. Tested the servlet and that worked fine. Then I enabled Maven support and added logback to my pom. I put a logging statement in the servlet' s doGet 方法 . 运行servlet时,它会抱怨它找不到任何绑定,因为没有将logback jar复制到Eclipse tomcat实例中 . 我希望在这里的某处发现 jar :

${workspace}\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\

如何让Eclipse正常使用WTP / Maven?我也试过安装m2e-wtp连接器没有区别 .

4 回答

  • 51

    检查 Deployment Assembly (项目上下文菜单),必须有映射 Maven Dependencies -> WEB-INF/lib .

  • 0

    来自ASP.NET背景,我发现使用Eclipse WTP和Maven运行webapp所需的工作量令人震惊,尤其是如果您自己学习的话 . 希望这个快速入门指南可以帮助其他人快速上手 .

    使用Maven在Eclipse WTP中使用hello world项目有两种方法 . 您可以创建一个动态Web项目,然后添加Maven性质,或者您可以执行相反的操作 .

    Pre-requisites for Eclipse with update sites

    Startup configuration

    Option 1: Create Dynamic Web Project then add Maven Nature

    • 创建新的Maven项目,选择原型org.apache.maven.archetypes:maven-archetype-webapp

    • 更改为Java EE透视图 .

    • 创建一个新的源文件夹src \ main \ java . 请注意Eclipse不够智能,无法为您执行此操作,并且文件夹的顺序也不正确 . src \ main \ java文件夹列在src \ main \ resources之后 . 这可以稍后在项目属性中手动修复 .

    • 创建一个新的servlet . 请注意Eclipse如何在错误的文件夹src \ main \ resources中默认此文件,因为顺序错误 . 而是手动选择src \ main \ java . 将向导第二页上的URL映射更改为/ *以使测试更容易 .

    • 现在我们的servlet已经准备就绪,但是对servlet api的依赖是未绑定的 . A)我们可以将servlet api添加为项目的依赖项,或者B)我们可以绑定到Apache 7.0的Eclipse服务器配置 .

    • 对于选项A,将此依赖项添加到pom:

    .

    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-api</artifactId>
      <version>7.0.${set this}</version>
      <scope>provided</scope>
    </dependency>
    
    • 对于选项B:

    • 项目属性 - > Java构建路径 - >库 - >添加库 - >服务器运行时 - > Apache Tomcat 7.0

    • 右键单击并在服务器上运行:

    • 内部浏览器中应出现空白页面,如http://localhost:8080/ $

    Test of dependency publishing

    • 将joda-time添加到pom .

    • 在先前为doGet方法创建的servlet中添加此行,并导入必要的依赖项:

    .

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("The time is now: " + new DateTime().toString());
    }
    

    重新加载测试页面,输出现在应该是:

    现在时间:2012-03-03T14:14:29.890-05:00

    现在,如果您想使用Servlet 3.0和注释,默认情况下不会启用此功能,原因我不知道 . 首先通过将Java 1.6添加到您的pom来强制Maven使用Java 1.6,否则每次更新pom时,配置都将恢复为Java 1.5 .

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    

    打开项目属性 - >项目构面 . 将“Dynamic Web Module”下的Version更改为3.0,更改java版本1.6

    在src \ main \ java中创建一个名为AnnotatedServlet的新Servlet,并注意如何自动创建@WebServlet注释 .

    Option 2: Create Dynamic Web Project then add Maven Nature

    • 选择Tomcat运行时和动态模块版本3.0

    • 创建源文件夹src \ main \ java

    • 设置默认输出目标\类

    • 设置上下文目录src \ main \ webapp

    • 检查生成web.xml

    • 使用映射/ *创建servlet以进行快速测试

    • 将输出语句添加到doGet方法

    response.getWriter().println("Another test");

    • 双击"Deployment descriptor"并将此属性添加到根web-app元素 metadata-complete="false"

    • 右键单击项目,然后选择Run As - > Run On Server

    • 右键单击项目 - >配置 - >转换为Maven项目

    • 选择包装作为战争

    • 编辑pom并设置编译器以使用java 1.6并添加joda-time依赖项:

    .

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  • 13

    在Project Explorer中右键单击Web项目,然后选择Maven - > Update Project

  • 1

    我遇到了类似的问题,虽然我已经正确配置了部署程序集,但它仍然无效 . 然后我发现在Window - > Preferences - > My Eclipse - > Java Enterprise Project - > Web Project下,在Deployment选项卡下,Dependent项目的管理被关闭了 . 我将其更改为将依赖项目的jar部署到lib文件夹,然后是所有内容像魅力一样工作 . 我甚至可以关闭Deployment Assembly选项 .

相关问题