WAR文件中缺少编译的类

时间:2015-04-06 16:15:45

标签: maven java-ee

我正在开发一个简单的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应该在哪里?

由于

2 个答案:

答案 0 :(得分:4)

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

由于这是一个webapp,此时您可能会遇到编译错误,因为您的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项目配置。

答案 1 :(得分:1)

你需要在maven开始时宣布这是一场战争:

<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.my</groupId>
    <artifactId>project</artifactId>
    <name>MyProject</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>

然后在最后把maping的heping插件用于构建战争:

<plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>

希望这有帮助