Eclipse WTP 不发布 Maven 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9541785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:57:02  来源:igfitidea点击:

Eclipse WTP not publishing Maven dependencies

eclipsetomcatmavenm2eclipseeclipse-wtp

提问by dcompiled

I'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 doGetmethod. When running the servlet, it complains it cannot find any bindings because the logback jars are not being copied into the Eclipse tomcat instance. I expected to find the jars published somewhere in here:

我正在尝试使用 Eclipse Indigo 和 Tomcat 服务器设置一个基本的 hello world 项目。我用一个简单的 servlet 创建了一个动态项目。测试了 servlet,效果很好。然后我启用了 Maven 支持并将 logback 添加到我的 pom.xml 文件中。我在 servlet 的doGet方法中放置了一个日志记录语句。运行 servlet 时,它抱怨找不到任何绑定,因为 logback jar 没有被复制到 Eclipse tomcat 实例中。我希望能在这里的某个地方找到发布的罐子:

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

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

How do I get Eclipse to work with WTP/Maven properly? I also tried installing the m2e-wtp connector with no difference.

如何让 Eclipse 与 WTP/Maven 正常工作?我也尝试安装 m2e-wtp 连接器,没有任何区别。

回答by e-zinc

Check Deployment Assembly(context menu on project), there must be mapping Maven Dependencies -> WEB-INF/lib.

检查部署程序集(项目的上下文菜单),必须有映射Maven Dependencies -> WEB-INF/lib

回答by dcompiled

Coming from an ASP.NET background, I find it shocking how much work it takes to get a webapp running with Eclipse WTP and Maven especially if you are learning on your own. Hopefully this quick start guide will help someone else get up to speed quickly.

来自 ASP.NET 背景,我发现使用 Eclipse WTP 和 Maven 运行 web 应用程序所需的工作量令人震惊,特别是如果您是自学的。希望本快速入门指南能帮助其他人快速上手。

There are two ways to get a hello world project working in Eclipse WTP with Maven. You can create a Dynamic web project and then add the Maven nature or you can do the opposite.

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

Pre-requisites for Eclipse with update sites

带有更新站点的 Eclipse 的先决条件

Startup configuration

启动配置

Option 1: Create Dynamic Web Project then add Maven Nature

选项 1:创建动态 Web 项目然后添加 Maven Nature

  • Create new Maven project, select archetype org.apache.maven.archetypes:maven-archetype-webapp

  • Change to Java EE perspective.

  • Create a new source folder, src\main\java. Notice how Eclipse is not smart enough to do this for you and also the ordering of the folders is incorrect. src\main\javafolder is listed after src\main\resources. This can be manually fixed later in the project properties.

  • Create a new servlet. Notice how Eclipse defaults this file in the wrong folder src\main\resourcesbecause the order is wrong. Instead, manually select src\main\java. Change the URL mapping on the second page of the wizard to /* to make testing easier.

  • Now our servlet is ready but the dependencies on the servlet api are unbound. A) we can add the servlet api as a dependency to our project or B) we can bind to the Eclipse server config for Apache 7.0.

  • For option A, add this dependency to the pom:

  • 新建Maven项目,选择archetype 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>
  • For option B:

    • Project properties -> Java Build Path -> Libraries -> Add Library -> Server Runtime -> Apache Tomcat 7.0
    • Right click and run on server:
  • A blank page should come up in the internal browser like http://localhost:8080/${artifact}

  • 对于选项 B:

    • 项目属性 -> Java Build Path -> Libraries -> Add Library -> Server Runtime -> Apache Tomcat 7.0
    • 右键单击并在服务器上运行:
  • 内部浏览器中应该会出现一个空白页面,例如http://localhost:8080/${artifact}

Test of dependency publishing:

依赖发布测试

  • Add joda-time to the pom.

  • Add this line in the servlet created earlier for the doGet method and import the necessary dependencies:

  • 将 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());
}

Reload the test page and the output should now be:

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

The time is now: 2012-03-03T14:14:29.890-05:00

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

Now if you want to play with Servlet 3.0 and annotations this is not enabled by default, for what reason I don't know. First force Maven to use Java 1.6 by adding this to your pom, otherwise each time you update your pom the configuration will revert to Java 1.5.

现在,如果您想使用 Servlet 3.0 和注释,默认情况下不启用此功能,我不知道是什么原因。首先通过将其添加到您的 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>

Open Project Properties -> Project Facets. Change the Version under "Dynamic Web Module" to 3.0, change java version 1.6

打开项目属性 -> 项目构面。将“动态Web模块”下的版本改为3.0,将java版本改为1.6

Create a new Servlet with class name AnnotatedServlet in src\main\java and notice how the @WebServlet annotation is auto created.

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

Option 2: Create Dynamic Web Project then add Maven Nature

选项 2:创建动态 Web 项目然后添加 Maven Nature

  • Select Tomcat Runtime and Dynamic Module Version 3.0
  • Create source folder src\main\java
  • Set default output target\classes
  • Set context directory src\main\webapp
  • Check generate web.xml
  • Create servlet with mapping /* for quick testing
  • Add an output statement to the doGet method
  • 选择 Tomcat 运行时和动态模块版本 3.0
  • 创建源文件夹 src\main\java
  • 设置默认输出目标\类
  • 设置上下文目录 src\main\webapp
  • 检查生成 web.xml
  • 使用映射 /* 创建 servlet 以进行快速测试
  • 向 doGet 方法添加输出语句

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

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

  • Double click the "Deployment descriptor" and add this attribute to the root web-app element metadata-complete="false"

  • Right click project and select Run As -> Run On Server

  • Right click project -> Configure -> Convert To Maven Project
  • Select packaging as war
  • Edit pom and set compiler to use java 1.6 and add joda-time dependency:
  • 双击“部署描述符”并将此属性添加到根 web-app 元素 metadata-complete="false"

  • 右键单击项目并选择运行方式 -> 在服务器上运行

  • 右键项目 -> 配置 -> 转换为 Maven 项目
  • 选择包装作为War
  • 编辑 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>

回答by Igal Dvir

Right click on the web project in Project Explorer then choose Maven -> Update Project

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

回答by vanval

I faced a similar problem and although I had configured Deployment Assembly correctly it still didn't work. Then I found that under Window -> Preferences -> My Eclipse -> Java Enterprise Project -> Web Project, under the Deployment tab, the management of Dependent projects was turned off. I changed it to deploy jars of dependent projects to the lib folder and then everything worked like a charm. I could even turn off the Deployment Assembly option.

我遇到了类似的问题,尽管我已经正确配置了部署程序集,但它仍然无法正常工作。然后我发现在Window -> Preferences -> My Eclipse -> Java Enterprise Project -> Web Project下,在Deployment选项卡下,关闭了依赖项目的管理。我将其更改为将依赖项目的 jar 包部署到 lib 文件夹,然后一切正常。我什至可以关闭部署程序集选项。