gpt4 book ai didi

maven - WAR 文件中缺少编译的类

转载 作者:行者123 更新时间:2023-12-03 02:37:12 25 4
gpt4 key购买 nike

我正在开发一个简单的 JSF 2 应用程序,但在使用托管 bean 时遇到了问题。我收到错误消息,说找不到 bean,当我查看 war 时,它没有任何已编译的 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中有一个classes文件夹,它是空的。

我的项目文件夹结构是:

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 应该在哪里?

谢谢

最佳答案

您的问题是,标准 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>

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

关于maven - WAR 文件中缺少编译的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29475054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com