Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiled classes missing in WAR file

I'm working on a simple JSF 2 application and I'm having trouble with the managed beans. I was getting errors saying that the bean cannot be found and when I looked at the war it didn't have any compiled beans. In my pom.xml I had the following:

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

If I remove the WebContent folder from the warSourceDirectory I get the rest of my project in the war file but this doesn't seem right. The file was generated by eclipse so I would assume it's ok. In WEB-INF there is a classes folder which is empty.

My project folder structure is:

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

but my compiled war file's structure is like this:

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

What should be packaged in my war file and where should the compiled bean be?

Thanks

like image 221
ewanc Avatar asked Apr 06 '15 16:04

ewanc


2 Answers

Your problem is that standard Eclipse Web Tools not only uses a completely different directory layout from that used by Apache Maven for web applications, but also uses a different way to specify library dependencies. The easiest way to fix this is to change your project to use the Maven conventions and re-import your project:

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.

As this is a webapp, you will likely have compilation errors at this point as your pom.xml file appears to have no dependencies in it. At a minimum, you will need:

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

Finally, the only circumstance in which you should specify the maven-eclipse-pluginin your pom.xml file is if your version of Eclipse is older than 4 years or so. This plugin is not compatible with the Maven integration that is now built into Eclipse. The only safe goal to use is eclipse:clean for deleting old eclipse project configuration.

like image 147
Steve C Avatar answered Nov 08 '22 13:11

Steve C


you need to anounce at the start of maven that it's a 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/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>

Then at the end put the heping plugin for maven to build the war:

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

Hope this helps

like image 26
GingerHead Avatar answered Nov 08 '22 12:11

GingerHead