【问题标题】:Maven web module not running from inside EclipseMaven Web 模块未从 Eclipse 内部运行
【发布时间】:2012-11-15 07:07:55
【问题描述】:

我已经配置了 Maven 多模块项目,当我在做的时候

mvn clean install

它正在为父项目中的两个模块创建 War 和 Jar 文件,当我在 Tomcat6 中部署 War 文件时它工作正常。

但是当我尝试从 eclipse 内部运行 web 模块时。

右键项目->在服务器上运行->选择Tomcat作为服务器

然后项目不工作。

Web 模块依赖于 java 项目,该项目也是 Multimodule 项目的一部分,因此我将此(Java)项目的依赖项添加到我的 Web 项目中,但在 Web 项目中 Java 构建路径不包含这种依赖关系。谁能知道我如何用eclipse解决这个问题? 正如我在eclipse中看到的

workspace_maven.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web\WEB-INF\lib

我没有在这里找到Java项目。但是当我在做的时候

mvn clean install

并在包含我的 Java 项目 jar 文件的 /WEB-INF/lib 目录中部署该战争。 我的 Web 模块 Pom.xml 文件...

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.test</groupId>
        <artifactId>demo_parent</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>org.csdc</groupId>
    <artifactId>demo-web</artifactId>
    <version>7.0.0</version>
    <packaging>war</packaging>
    <name> web 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>


        <dependency>
            <groupId>org.test</groupId>
            <artifactId>core-java</artifactId>
            <version>5.0.0</version>
        </dependency>



    </dependencies>  


    <build>
        <finalName>amanda-web</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat6-maven-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
    </build>
</project>

还有我的父 pom.xml 文件...

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.test</groupId>
    <artifactId>demo_parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>
    <modules>

        <module>demo-web</module>
        <module>core-java</module>
    </modules>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration> 
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>

            </plugins>

        </pluginManagement>
    </build>
    <properties>
        <maven.compiler.source>1.5</maven.compiler.source>
        <maven.compiler.target>1.5</maven.compiler.target>
    </properties>
</project>

*注意:-*我已经从父 pom.xml 中删除了存储库和依赖项,以使 pom 更短。

【问题讨论】:

  • 你安装m2e-wtp插件了吗?
  • 是的,我是从 Eclipse 市场安装的
  • 从 Eclipse 运行时遇到什么错误?
  • 当我从 ecipse 内部运行 tomcat 服务器中的应用程序时,项目启动时需要的类和部分 java 项目不可用,所以它告诉类未找到异常
  • 您也可以发布父 POM 吗?

标签: java eclipse maven


【解决方案1】:

你应该需要apache maven plugin

目标应该是tomcat:run

它将在您的目标文件夹中下载新的 tomcat 实例并将您的战争部署在该 tomcat 上。

您可以设置该tomcat的所有设置,也可以在插件的configuration标签中引用设置XML。

您可以在调试模式下运行它,并且可以即时查看所有更改。 (仅限 Java、JSP 文件。无 XML 和属性)

为了您的帮助,我添加了一个代码,可以帮助您在安装 Web 模块时运行 tomcat 服务器并部署 WAR。

应用程序将安装在 tomcat 5 和根上下文中。 您可以通过网址访问它:http://localhost:8080/

<project>
...
<build>
  <finalName>myWAR</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.6</source>
               <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
                <path>/</path>
        </configuration>
        <executions>
            <execution>
                <id>RUN_TOMCAT</id>
                <phase>install</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <inherited>false</inherited>
                <configuration>
                </configuration>
            </execution>
        </executions>
        </plugin>
    </plugins>
</build>
</project>

【讨论】:

  • Parent Pom.xml 或 Web 模块 Pom.xml 中是否需要它?我的 web 模块正在 Tomcat 中运行,但由于 Java 项目依赖性,它无法正常工作
  • 你需要在web模块中添加它。 WAR 插件还包括所有依赖和父 jar,因此无需为此配置任何内容。
  • 我添加了 web 模块 pom.xml 文件请看一下,让我知道它是否正确
  • 我在 web pom.xml 文件中进行了更改,但我仍然得到相同的问题项目无法从 eclipse 内部工作,正如我已经提到的,eclipse 显示我在服务器上运行项目并且它正在部署但 web 项目需要一些 Java 项目的 claases 才能启动,以便从 eclipse 运行 tomcat 显示 Some class not found exception on server startup
  • @TalhaAhmedKhan 我不认为 Maven Tomcat 插件是强制性的。我在没有这个插件的情况下从 Eclipse+M2E 部署 Web 应用程序,它运行良好。
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 2011-04-27
  • 2011-05-12
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
相关资源
最近更新 更多