Eclipse + Maven + 动态Web项目 -> Maven覆盖部署组件

18

摘要

在Eclipse中,当我执行“Maven->更新项目配置”时,“Maven Dependencies”会从我的项目的“部署程序集”中删除。

详细信息

我开始使用预配置的Eclipse项目:文件->新建->动态Web项目->JavaServer Face v2.0项目。为了去除"魔法",我将其转换为Maven项目:配置->转换为Maven项目。

pom.xml包含以下内容:

<build>
    <finalName>jsf-facelets-tutorial</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webXml>WebContent/WEB-INF/web.xml</webXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>2.0.5</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-impl</artifactId>
        <version>2.0.5</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

为了确保在部署之前将 Maven 依赖项复制到“WEB-INF/lib/”中,我已将它们添加到项目的“Deployment Assembly”中:Project Properties->Deployment Assembly。有关更多详细信息,请参见此问题:Eclipse + Maven + JavaServer Faces -> ClassNotFoundException: StartupServletContextListener

我知道现在有两个相关的问题。

问题

(1) 当我执行“Project->Maven->Update Project Configuration”时,“Maven Dependencies”会从我的“Deployment Assembly”中删除。是否有一种方法可以阻止这种情况发生,可能是通过某个 Maven 插件?

(2) 当我从 Eclipse 中构建 .war 文件时,一切都很好,在 Tomcat 中运行 .war 文件也很正常。但是当我使用 Maven 从命令行构建 .war 文件 mvn clean compile war:war 时,“WebContent/”中的 .html 文件不会被添加到 .war 文件中。再次,我需要哪些 Maven 设置/插件来解决这个问题?

FYI,这是一个非常基本的应用程序……只有一个登录页面和一个欢迎页面。

如果需要提供任何其他详细信息(web.xml、faces-config-xml、login.xhtml),请告诉我,我会添加它们。

谢谢

--EDIT--

Eclipse 版本:3.6.2

Eclipse m2e 版本:1.0.1

Apache Maven 版本:2.2.1

(1) 解决方案

更新为 Eclipse Java EE 3.7.2 (Indigo)。现在还使用 m2e-wtp Maven Integration for Eclipse WTP plugin

(2) 解决方案

原型创建新的 Maven 项目,然后使用“Eclipse->导入->现有的 Maven 项目”导入。使用新版本的 Eclipse 和 m2e-wtp,在 Eclipse 中的 Java EE 视图中可以很好地处理标准的 Maven 目录结构

现在 pom.xml 也更简单了:

<build>
    <finalName>jsf-facelets-tutorial</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>el-api</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>2.0.5</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-impl</artifactId>
        <version>2.0.5</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

你使用的是哪个版本的Eclipse和M2Eclipse? - nwinkler
Eclipse版本:3.6.2 Eclipse m2e版本:1.0.1 Apache Maven版本:2.2.1 - Alex Averbuch
我已更新我的答案,以回答您关于WebContent目录的问题。 - nwinkler
1个回答

4
我建议您升级到Eclipse 3.7.x(Indigo),该版本对m2e有更好的支持,并且还允许您使用Maven Integration for WTP(单独安装),这应该会使事情变得更加容易。我想通过升级到Indigo,大部分问题应该得以解决。
安装链接:http://marketplace.eclipse.org/node/96737 此外,请参见:Maven/Tomcat Projects In Eclipse Indigo/3.7 编辑 将Web资源放置在WebContent下,然后将其作为WAR插件中的一个目录可能是一种解决方法。正确的方式是将它们放在src/main/resourcessrc/main/webapp下,它们应该会自动包含在WAR文件中。 WebContent不是Maven的标准目录之一。

已安装完成。在这个新的配置下,您推荐使用哪种创建项目的方法?是命令行maven从原型创建,还是使用Eclipse向导? - Alex Averbuch
当使用m2e和m2e-wtp插件在WTP项目上工作时,Faces Config Editor是否仍然打开?我的已经不再打开了,我一直无法找出原因(http://stackoverflow.com/questions/9992977/eclipse-faces-config-editor-not-working)。 - Alex Averbuch
到目前为止,我还没有使用过这个编辑器,但是快速测试表明我可以很好地使用它打开 faces-config.xml 文件。 - nwinkler
你刚才使用了“打开方式...”->“Faces Config Editor”吗? - Alex Averbuch
这是一个老话题,但我在Eclipse Mars中也遇到了完全相同的问题。那么该怎么办? :p - MychaL
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接