WAR 文件中缺少编译的类

2024-01-11

我正在开发一个简单的 JSF 2 应用程序,但在使用托管 bean 时遇到了问题。我收到错误消息,说找不到 bean,当我查看战争时,它没有任何已编译的 bean。在我的 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.traintrack</groupId>
<artifactId>test</artifactId>
<version>SNAPSHOT</version>
<packaging>war</packaging>
<name>test</name>
<build>
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>7.7.Final</version>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <warSourceDirectory>${basedir}/WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

如果我从 warSourceDirectory 中删除 WebContent 文件夹,我会在 war 文件中得到项目的其余部分,但这似乎不对。该文件是由 eclipse 生成的,所以我认为它没问题。在 WEB-INF 中有一个类文件夹,该文件夹是空的。

我的项目文件夹结构是:

test
- src/
  - sample/
    - sampleBean.java
- WebContent/
  - sample.xhtml
  - WEB-INF/
    - faces-config.xml
    - web.xml
  - META-IF/

但我编译的war文件的结构是这样的:

test
  - sample.xhtml
  - WEB-INF/
    - faces-config.xml
    - web.xml
    - classes/
  - META-IF/

我的 war 文件中应该打包什么内容以及编译后的 bean 应该在哪里?

Thanks


您的问题是,标准 Eclipse Web Tools 不仅使用与 Apache Maven 用于 Web 应用程序完全不同的目录布局,而且还使用不同的方式来指定库依赖项。解决此问题的最简单方法是将项目更改为使用 Maven 约定并重新导入项目:

1. Move all of your main (not unit test) java source files into directory `src/main/java`;
2. Move all of your unit test java source files into directory `src/test/java`;
3. Move all the files in WebContent into directory `src/main/webapp` (excluding class files and jar files);
4. Remove the following from your pom.xml:
    a. `<outputDirectory>...</outputDirectory>`;
    b. `<warSourceDirectory>${basedir}/WebContent</warSourceDirectory>`;
5. Delete the project from eclipse (but not the source files!);
6. Execute `mvn eclipse:clean` on your project from a command line;
7. Ensure that the .project file, .classpath file and content of .settings directory have been removed (manually if necessary);
8. Re-import the project into Eclipse as a 'Maven' project.

由于这是一个 Web 应用程序,此时您可能会出现编译错误,因为您的 pom.xml 文件中似乎没有依赖项。您至少需要:

<dependencies>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

最后,您应该指定的唯一情况maven-eclipse-plugin在您的 pom.xml 文件中,您的 Eclipse 版本是否早于 4 年左右。该插件与现在内置于 Eclipse 中的 Maven 集成不兼容。唯一可以使用的安全目标是eclipse:clean用于删除旧的 eclipse 项目配置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WAR 文件中缺少编译的类 的相关文章

随机推荐