2015-04-06 74 views
4

我正在研究一个简单的JSF 2应用程序,并且我在托管的bean中遇到了问题。我收到错误,说该bean无法找到,当我看到这场战争时,它没有任何编译的bean。在我的pom.xml我有以下几点:WAR文件中缺少编译类

<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文件夹,但这似乎并不正确。该文件是由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/ 

什么都应该打包在我的战争文件和编译豆应该在哪里呢?

感谢

+0

首先你为什么改变默认值。为什么不使用'src/main/webapp'?此外,它将是有用的,看到完整的POM文件... – khmarbaise

+0

对不起,问题更新全POM。 Eclipse以这种方式生成文件。 – ewanc

+0

我将添加一个编辑到这个问题,因为这适用于将eclipse项目更改为纯粹的maven项目 – anquegi

回答

4

你的问题是,标准的Eclipse Web工具不在onl y使用与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项目配置。

+1

好彻底的答案。我只会添加的servlet依赖项应该是范围' ' – artbristol

+0

doh!你当然是对的 –

+0

非常感谢,这正是我的问题所在,我最终不得不从头开始完全重建我的项目,这就解释了为什么! – ewanc

0

你需要行家的开始,这是一个战争anounce:

<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> 

那么,在年底把和平插件Maven构建战争:

<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> 

希望这有助于

+0

对不起,我应该包括完整的pom文件,我已经更新了这个问题。生成战争文件没有任何问题,它只是内部的内容。 – ewanc

+0

确定您的问题是您没有任何依赖关系。你需要把所有的战争作为依赖 – GingerHead

+0

这个项目没有任何其他项目的依赖。它只是/ WebContent /中的一个页面和/src/sample/Bean.java中的一个bean。编译的war包含了WebContent文件夹中的所有内容,但编译的bean不在那里。 – ewanc