1

我已经使用 Maven 插件在 Eclipse IDE 中创建了一个 Web 应用程序,如图所示 在此处输入图像描述

我在那个项目中创建了一个 Servlet

pom.xml生成的是

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>MTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>MTest</finalName>
  </build>
</project>

当我右键单击 pom.xml 并执行 Run as Maven Install

我在下面看不到我的 servlet 的类文件

C:\Users\sai\OfficeWSApril14\MTest\target\MTest\WEB-INF\classes

因此,它的投掷ClassNotFoundException

请让我知道我做错了什么

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MTest Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ MTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MTest ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ MTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\sai\OfficeWSApril14\MTest\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ MTest ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ MTest ---
[INFO] Surefire report directory: C:\Users\sai\OfficeWSApril14\MTest\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MTest ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MTest] in [C:\Users\sai\OfficeWSApril14\MTest\target\MTest]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\sai\OfficeWSApril14\MTest\src\main\webapp]
[INFO] Webapp assembled in [107 msecs]
[INFO] Building war: C:\Users\sai\OfficeWSApril14\MTest\target\MTest.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ MTest ---
[INFO] Installing C:\Users\sai\OfficeWSApril14\MTest\target\MTest.war to C:\Users\sai\.m2\repository\com\MTest\0.0.1-SNAPSHOT\MTest-0.0.1-SNAPSHOT.war
[INFO] Installing C:\Users\sai\OfficeWSApril14\MTest\pom.xml to C:\Users\sai\.m2\repository\com\MTest\0.0.1-SNAPSHOT\MTest-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.536s
[INFO] Finished at: Mon Apr 29 10:10:06 IST 2013
[INFO] Final Memory: 5M/15M
[INFO] ---------------------

当我右键单击 Eclipse IDE Project create new Servlet 时,如何
确保源文件放在 src/main/java 下?

我附上了截图,请看

在此处输入图像描述

4

1 回答 1

1

由于Maven显示我们如下: -

[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ MTest ---
[INFO] No sources to compile

这意味着没有任何已知的 java 类。请检查您的代码并确保源文件位于src/main/java

编辑

有两种可能的方法来解决您的问题how can i move the servelet under src/main/java ?

1按照以下步骤将现有转换Maven Java Project为 a :-Dynamic Web Project

  1. Right click在你的项目中。

  2. Properties从上下文菜单中选择。

  3. Properties Windows 选择Project Facets.

  4. right hand panel选择以下选项:-

    1. 动态网页模块:3.0
    2. 爪哇:1.6
    3. Java脚本:1.0
  5. 点击OK button

Maven Web Project2使用以下步骤创建新的:-

  1. 转到菜单File ---> New ---> Other
  2. 在 处Select Wizard Windows,选择Maven ---> Project
  3. 单击下一步并输入所需的信息,以便我们在New Maven Project.
  4. Filter文本框中,输入webapp-javaee6.
  5. 选择org.codehaus.mojo.archetypes:webapp-javaee6:1.5.
  6. 单击下一步并输入所需信息,以完成项目创建。

请注意,我更喜欢第二种方式,因为我们会有额外的配置,pom.xml这对于进一步的开发非常有用。您还可以参考我之前关于如何在 Eclipse 中配置 Java EE maven 项目的完整故事的回答?

我希望这可能会有所帮助。

于 2013-04-29T05:52:19.377 回答