Running an Ant-builded project as web-application - eclipse

I'm trying to run a "project.zip" I've downloaded from the official GWT site about RPC and Hibernate at http://www.gwtproject.org/articles/using_gwt_with_hibernate.html .
The guide suggests:
"you can use Ant to build the project, as well as start up hosted mode
to see the UI and our Hibernate instance setup in the embedded Jetty
server."
So I have imported it on Eclipse with New > Project menu-> Java Project from Existing Ant Buildfile wizard.
My final goal is to run it on Eclipse as web-application, but it gives me every types of errors and above all there's no run as-> web application but only as ->ant build ! And all the War directory is missing in Eclipse.
How can change this Ant build project.zip to a normal project GWT? I'm amazed how an official Google guide can give so many problems!
If you need it this is the build.xml :
<?xml version="1.0" encoding="utf-8" ?>
<project name="Guestbook" default="build" basedir=".">
<!-- Define gwt.home, gwt.dev.jar, appengine.sdk.home -->
<property file="build.properties"/>
<path id="project.class.path">
<pathelement location="war/WEB-INF/classes"/>
<pathelement location="${gwt.home}/gwt-user.jar"/>
<!-- Add any additional non-server libs (such as JUnit) -->
<fileset dir="war/WEB-INF/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="libs" description="Copy libs to WEB-INF/lib">
<mkdir dir="war/WEB-INF/lib" />
<copy todir="war/WEB-INF/lib" file="${gwt.home}/gwt-servlet.jar" />
<!-- Add any additional server libs that need to be copied -->
<copy todir="war/WEB-INF/lib" flatten="true">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</copy>
</target>
<target name="javac" depends="libs" description="Compile java source">
<mkdir dir="war/WEB-INF/classes"/>
<javac srcdir="src" includes="**" encoding="utf-8"
destdir="war/WEB-INF/classes"
source="1.5" target="1.5" nowarn="true"
debug="true" debuglevel="lines,vars,source">
<classpath refid="project.class.path"/>
</javac>
</target>
<!-- can add additional arguments like -logLevel INFO or -style PRETTY -->
<target name="gwtc" depends="javac" description="GWT compile to JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
<pathelement location="${gwt.home}/${gwt.dev.jar}"/>
</classpath>
<!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->
<jvmarg value="-Xmx256M"/>
<arg value="com.google.musicstore.MusicStore"/>
</java>
</target>
<target name="hosted" depends="javac" description="Run hosted mode">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.HostedMode">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
<pathelement location="${gwt.home}/${gwt.dev.jar}"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-startupUrl"/>
<arg value="MusicStore.html"/>
<arg value="com.google.musicstore.MusicStore"/>
<!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
</java>
</target>
<target name="build" depends="gwtc" description="Build this project" />
<target name="clean" description="Cleans this project">
<delete dir="war/WEB-INF/classes" failonerror="false" />
<delete dir="war/musicstore" failonerror="false" />
</target>
</project>

run
ant
to build the project.
run
ant hosted
to run the project.
I'm confused by the concept of a normal GWT project ... I'm not sure to understand what it means. But if it means an eclipse-ready-GWT-project, you will probably be disappointed because as you noted yourself:
you can use Ant to build the project
It is an ant based project, not en eclipse project. You can call ant from eclipse, but there is no specic eclipse files (.project and .classpath) so that it can run with gwt eclipse plugin without configuring it yourself.

Related

Ant build.xml working Eclipse. Not working direct

My buildfile seems to be running correctly (and generating junit test reports) correctly when executing in Eclipse, however when I execute directly with: ant -buildfile C:\....\build.xml the files aren't produced - and the cmd output suggests it's not running the tests.
I have two eclipse projects. JUnitTest1 which is the code. JUnitTestUnitTests which contains the test code.
I've followed the instructions here to create my buildfile in eclipse (selecting only the test project) and can see the files drop into the junit directory.
When I run ant command directly, no files are generated and it doesn't look like it's running my tests.
Why aren't the changes made via the eclipse GUI reflected in the build.xml? I thought it was auto updated?
Here's the eclipse config
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="JUnitTest1UnitTests">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../../tools/eclipse-mars/"/>
<property name="junit.output.dir" value="junit"/>
<property name="JUnitTest1.location" value="../JUnitTest1"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="JUnitTest1.classpath">
<pathelement location="${JUnitTest1.location}/bin"/>
<pathelement location="${JUnitTest1.location}/../../../../../tools/libs/hamcrest-core-1.3.jar"/>
<pathelement location="${JUnitTest1.location}/../../../../../tools/libs/junit-4.12.jar"/>
</path>
<path id="JUnitTest1UnitTests.classpath">
<pathelement location="bin"/>
<pathelement location="../../../../../tools/libs/hamcrest-core-1.3.jar"/>
<pathelement location="../../../../../tools/libs/junit-4.12.jar"/>
<path refid="JUnitTest1.classpath"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall">
<ant antfile="build.xml" dir="${JUnitTest1.location}" inheritAll="false" target="clean"/>
</target>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects">
<ant antfile="build.xml" dir="${JUnitTest1.location}" inheritAll="false" target="build-project">
<propertyset>
<propertyref name="build.compiler"/>
</propertyset>
</ant>
</target>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="JUnitTest1UnitTests.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="JUnitTest1UnitTests">
<mkdir dir="${junit.output.dir}"/>
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml"/>
<test name="com.me.tests.MyTestClass" todir="${junit.output.dir}"/>
<classpath refid="JUnitTest1UnitTests.classpath"/>
</junit>
</target>
<target name="junitreport">
<junitreport todir="${junit.output.dir}">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${junit.output.dir}"/>
</junitreport>
</target>
I believe I've fixed this.
Eclipse creates your build file and seperately, you configure your run configurations. These run configurations (where you specify the run order of the ant tasks) are not persisted or represented in the actual build file.
The default target of the build file is the target="build" ... So you need to manually edit the build file and use the depends="..." clause to chain together the dependancies of your tasks.
This page (read example build file and see final line) has a great explanation of the depends clause.

Ant + GWT compiling two related projects

I have a problem with using Ant to compile a GWT project which is split into two parts. First project which is GWT project- client side of a web app and the second project included in build path which is a data model for hibernate. When I'm runing a build.xml I'm geting errors that I forget to inherit some modules. Where should I include my second project? in javac? project.classpath? libs?
I'm using standard build.xml example from GWT showcase:
<?xml version="1.0" encoding="utf-8" ?>
<project name="Showcase" default="build" basedir=".">
<!-- Arguments to gwtc and devmode targets -->
<property name="gwt.args" value="" />
<!-- Configure path to GWT SDK -->
<property name="gwt.sdk" location="../.." />
<path id="project.class.path">
<pathelement location="war/WEB-INF/classes"/>
<pathelement location="${gwt.sdk}/gwt-user.jar"/>
<fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>
<!-- Add any additional non-server libs (such as JUnit) -->
<fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>
</path>
<target name="libs" description="Copy libs to WEB-INF/lib">
<mkdir dir="war/WEB-INF/lib" />
<copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />
<copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet-deps.jar" />
<!-- Add any additional server libs that need to be copied -->
</target>
<target name="javac" depends="libs" description="Compile java source to bytecode">
<mkdir dir="war/WEB-INF/classes"/>
<javac srcdir="src" includes="**" encoding="utf-8"
destdir="war/WEB-INF/classes"
source="1.5" target="1.5" nowarn="true"
debug="true" debuglevel="lines,vars,source">
<classpath refid="project.class.path"/>
</javac>
<copy todir="war/WEB-INF/classes">
<fileset dir="src" excludes="**/*.java"/>
</copy>
</target>
<target name="gwtc" depends="javac" description="GWT compile to JavaScript (production mode)">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
</java>
</target>
<target name="devmode" depends="javac" description="Run development mode">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
<pathelement location="../../validation-api-1.0.0.GA.jar" />
<pathelement location="../../validation-api-1.0.0.GA-sources.jar" />
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-startupUrl"/>
<arg value="Showcase.html"/>
<arg line="-war"/>
<arg value="war"/>
<!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
<arg line="${gwt.args}"/>
<arg value="com.google.gwt.sample.showcase.Showcase"/>
</java>
</target>
<target name="superdevmode" depends="javac" description="Run Super Dev Mode">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.codeserver.CodeServer">
<classpath>
<pathelement location="src"/>
<pathelement location="../../gwt-codeserver.jar" />
<path refid="project.class.path"/>
<pathelement location="../../validation-api-1.0.0.GA.jar" />
<pathelement location="../../validation-api-1.0.0.GA-sources.jar" />
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="com.google.gwt.sample.showcase.Showcase"/>
</java>
</target>
<target name="eclipse.generate" depends="libs" description="Generate eclipse project">
<java failonerror="true" fork="true" classname="com.google.gwt.user.tools.WebAppCreator">
<classpath>
<path refid="project.class.path"/>
</classpath>
<arg value="-XonlyEclipse"/>
<arg value="-ignore"/>
<arg value="com.google.gwt.sample.showcase.Showcase"/>
</java>
</target>
<target name="hosted" depends="devmode" description="Run development mode (NOTE: the 'hosted' target is deprecated)" />
<target name="build" depends="gwtc" description="Build this project" />
<target name="war" depends="build" description="Create a war file">
<zip destfile="Showcase.war" basedir="war"/>
</target>
<target name="clean" description="Cleans this project">
<delete dir="war/WEB-INF/classes" failonerror="false" />
<delete dir="war/showcase" failonerror="false" />
</target>
</project>

Eclipse with Java EE (EJB), ANT and Jenkins / Hudson

My Goal is to get a Enterprise Java Application to be built on Jenkins. The Application consists of 4 Projects (Client-Interfaces, Webapplcation (incl. Faces), EJB Application (incl. JPA), EAR-Container-Project).
When Eclipse deploys this projects to a glassfish server, it assembles the Webapplication (war-file), the Client-Interfaces (jar-file) and the EJB-Interfaces (jar-file) into one ear-File.
Now, if I want to use continous integration I need to achieve the same on the CI-Server jenkins.
My first idea was to solve this with ant, so I used the Export-Function of Eclipse and generated build-Files for the projects.
The Problem is that the generated Build-Files refer to the Java EE Libraries (such as Glassfish-Runtime, JPA-Library, etc) which are outside of the project directory. There are about 30 libraries.
This implies that I cannot use the file on jenkins, because this libraries are missing. Of course I can copy these, but I don't think this is how it should be done.
So, what is the best way to get the Java EE Enterprise Application to be built on the CI Server? Do I have to write the ANT-Script all by myself and copy the libraries into the project? Or am I Missing something obvious?
Since I did not found anything that suited for me, I wrote an ant script that covered my needs on my own.
Here is my solution if this helps anyone in the future:
`
<project basedir="." default="build" name="Project">
<available property="glassfishdir" value="/opt/glassfish3/glassfish/modules"
file="/opt/glassfish3/glassfish/modules" type="dir" />
<!-- ########### Property Declarations ################################################################################################################### -->
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<property name="builddir" value="build" />
<property name="outputartifacts" value="out" />
<property name="web.name" value="ProjectWeb" />
<property name="web.projectpath" value="ProjectWeb"/>
<property name="web.src" value="${web.projectpath}/src" />
<property name="web.builddir" value="${builddir}/web" />
<property name="web.builddir.classes" value="${web.builddir}/WEB-INF/classes"/>
<property name="ejb.name" value="ProjectEJB" />
<property name="ejb.projectpath" value="ProjectEJB"/>
<property name="ejb.src" value="${ejb.projectpath}/src"/>
<property name="ejb.builddir" value="${builddir}/ejb" />
<property name="ejb.builddir.classes" value="${ejb.builddir}/classes" />
<property name="ejbclient.name" value="ProjectEJBClient" />
<property name="ejbclient.projectpath" value="ProjectEJBClient"/>
<property name="ejbclient.src" value="${ejbclient.projectpath}/src"/>
<property name="ejbclient.builddir" value="${builddir}/ejbclient" />
<property name="ejbclient.builddir.classes" value="${ejbclient.builddir}/classes"/>
<property name="ear.name" value="ProjectApplication" />
<property name="ear.dir" value="ProjectEAR" />
<!-- ########### Main Targets ################################################################################################################### -->
<target name="build" depends="create-ear">
</target>
<target name="clean-build">
<antcall target="clean" />
<antcall target="build" />
</target>
<target name="clean">
<delete dir="${builddir}"/>
<delete dir="${outputartifacts}"/>
</target>
<target name="init">
<mkdir dir="${outputartifacts}" />
</target>
<!-- ########### EJB App ################################################################################################################### -->
<target name="init-ejb" depends="init">
<mkdir dir="${ejb.builddir}" />
<copy includeemptydirs="false" todir="${ejb.builddir.classes}">
<fileset dir="${ejb.src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="build-ejb" depends="init-ejb">
<javac debug="true" debuglevel="${debuglevel}" destdir="${ejb.builddir.classes}" includeantruntime="false" source="${source}" target="${target}">
<src path="${ejb.src}"/>
<classpath>
<fileset dir="${glassfishdir}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${outputartifacts}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- ########### WEB ################################################################################################################### -->
<target name="init-web" depends="init">
<mkdir dir="${web.builddir.classes}"/>
<copy includeemptydirs="false" todir="${web.builddir}">
<fileset dir="${web.projectpath}/WebContent">
</fileset>
</copy>
<copy includeemptydirs="false" todir="${web.builddir.classes}">
<fileset dir="${web.src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init-web,create-ejb-client" name="build-web">
<javac debug="true" debuglevel="${debuglevel}" destdir="${web.builddir.classes}" includeantruntime="false" source="${source}" target="${target}">
<src path="${web.src}"/>
<classpath>
<fileset dir="${glassfishdir}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="out/">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- ############## EJB CLIENT ################################################################################################################ -->
<target name="init-ejb-client" depends="init">
<mkdir dir="${ejbclient.builddir}"/>
<copy includeemptydirs="false" todir="${ejbclient.builddir.classes}">
<fileset dir="${ejbclient.src}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init-ejb-client" name="build-ejb-client">
<javac debug="true" debuglevel="${debuglevel}" destdir="${ejbclient.builddir.classes}" includeantruntime="false" source="${source}" target="${target}">
<src path="${ejbclient.src}"/>
<classpath>
<fileset dir="${glassfishdir}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- ############ CREATE ARCHIVES################################################################################################################## -->
<target name="create-web" depends="build-web">
<war destfile="${outputartifacts}/${web.name}.war" basedir="${web.builddir}" webxml="${web.projectpath}/WebContent/WEB-INF/web.xml"/>
</target>
<target name="create-ejb-client" depends="build-ejb-client">
<jar destfile="${outputartifacts}/${ejbclient.name}.jar" basedir="${ejbclient.builddir.classes}" includes="**/*"/>
</target>
<target name="create-ejb" depends="build-ejb">
<jar destfile="${outputartifacts}/${ejb.name}.jar" basedir="${ejb.builddir.classes}" includes="**/*">
<manifest>
<attribute name="Class-Path" value="${ejbclient.name}.jar"/>
</manifest>
</jar>
</target>
<target name="create-ear" depends="create-ejb-client,create-web,create-ejb">
<ear destfile="${outputartifacts}/${ear.name}.ear" appxml="${ear.dir}/EarContent/META-INF/application.xml">
<fileset dir="${outputartifacts}" includes="*.jar,*.war"/>
</ear>
</target>
</project>
`
Use Maven.
Maven allow to define all dependencies in a single xml file (pom), dependencies which will be automatically downloaded from internet at compilation phase.
Maven come with a set of plugin to facilitate continuous integration like being able to start a container, run the test and close it automatically.
Maven integrate natively with jenkins.
Maven defines a complex lifecycle designed for this kind of problematic and allowing to compile, run unit test, package, run integration test and deploy with a single command triggered from jenkins;
Maven is definitively THE solution here.
You can also auto-create the build.xml in Eclipse using "Export... > General\Ant Buildfile" from the project context menu. This way the correct classpath is generated to your JAR's already available in the project.
Chances are that if there are dependencies between the projects, you only need to configure one build file to be run on Jenkins, as it will automatically call the build files from the other projects.

Getting gwt hibernate example code to work with eclipse

I am starting out with GWT and hibernate. I'm going through the tutorial at https://developers.google.com/web-toolkit/articles/using_gwt_with_hibernate.
I downloaded the sample code provided with the tutorial (http://google-web-toolkit.googlecode.com/files/gwt_hibernate_base.zip). This is a simple music store where you can add an account and records. I am able to run this and successfully add accounts and records to the database by doing the following commands:
(in the data directory) java -cp ../lib/hsqldb.jar org.hsqldb.Server
ant build hosted
Here is the build.xml file used to build this:
<?xml version="1.0" encoding="utf-8" ?>
<project name="Guestbook" default="build" basedir=".">
<!-- Define gwt.home, gwt.dev.jar, appengine.sdk.home -->
<property file="build.properties"/>
<path id="project.class.path">
<pathelement location="war/WEB-INF/classes"/>
<pathelement location="${gwt.home}/gwt-user.jar"/>
<!-- Add any additional non-server libs (such as JUnit) -->
<fileset dir="war/WEB-INF/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="libs" description="Copy libs to WEB-INF/lib">
<mkdir dir="war/WEB-INF/lib" />
<copy todir="war/WEB-INF/lib" file="${gwt.home}/gwt-servlet.jar" />
<!-- Add any additional server libs that need to be copied -->
<copy todir="war/WEB-INF/lib" flatten="true">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</copy>
</target>
<target name="javac" depends="libs" description="Compile java source">
<mkdir dir="war/WEB-INF/classes"/>
<javac srcdir="src" includes="**" encoding="utf-8"
destdir="war/WEB-INF/classes"
source="1.5" target="1.5" nowarn="true"
debug="true" debuglevel="lines,vars,source">
<classpath refid="project.class.path"/>
</javac>
</target>
<!-- can add additional arguments like -logLevel INFO or -style PRETTY -->
<target name="gwtc" depends="javac" description="GWT compile to JavaScript">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
<pathelement location="${gwt.home}/${gwt.dev.jar}"/>
</classpath>
<!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->
<jvmarg value="-Xmx256M"/>
<arg value="com.google.musicstore.MusicStore"/>
</java>
</target>
<target name="hosted" depends="javac" description="Run hosted mode">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">
<classpath>
<pathelement location="src"/>
<path refid="project.class.path"/>
<pathelement location="${gwt.home}/${gwt.dev.jar}"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-startupUrl"/>
<arg value="MusicStore.html"/>
<arg value="com.google.musicstore.MusicStore"/>
<!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
</java>
</target>
<target name="build" depends="gwtc" description="Build this project" />
<target name="clean" description="Cleans this project">
<delete dir="war/WEB-INF/classes" failonerror="false" />
<delete dir="war/musicstore" failonerror="false" />
</target>
Now I would like to get this simple example working in eclipse. I created a project called MusicStore using the gwt eclipse plugin. I then copied over the tutorial files without changing them. I can't post an image but here is a link to my eclipse project structure:
http://oi50.tinypic.com/t6rn2w.jpg
Again, I start by running
(in the data directory) java -cp ../lib/hsqldb.jar org.hsqldb.Server
Then I try to run my project in eclipse but hibernate doesn't work. When I try to add an account in the UI it alerts "Failed to save account" and I get the error java.lang.NoClassDefFoundError: org/hibernate/Session.
Please let me know how I should go about getting the sample code working with eclipse. I think that many users new to hibernate will also want to do this.
Thanks!
Basically eclipse cannot find your hibernate libraries. That is because your build.xml is for building a war file. Eclipse doesn't read it so it doesn't have any of your build information in there.
A quick solution to this is to just go to the "Build Path" properties in Eclipse and make sure your /lib directory is included in the "Libraries" tab. Then eclipse will know where you are keeping your libraries.
A better solution would be to use something like Maven. Then the Maven Plugin for Eclipse will automatically look at the Maven build file and configure the Eclipse project so that it can detect where the libraries are held etc...

Java EE eclipse project directory structure?

I am attempting to setup a sample dynamic web project in Eclipse using Java EE, Spring and Maven (using Nexus repository manager). I was wondering if anybody knows the "best practice" directory structure that I should setup for an enterprise web app in Eclipse? Should I just stick with the default structure that is setup for me? I ask because looking around the internet I see wide variation (for instance, where the WEB-INF and META-INF folders are..if there is a 'war' directory etc.). Thanks!
If you use Maven, I'd warmly recommend to just follow Maven's convention. This is the "best practice" in Maven's world (and I don't see any good reasons to not do so, not following this advice will lead to more work).
One easy way to create a webapp project is to use the maven-archetype-webapp:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.mycompany.app \
-DartifactId=my-webapp \
-Dversion=1.0-SNAPSHOT
(You can paste this command "as is" in a Linux shell; on Windows, type everything on single line without the "\".)
And this is the structure you'll get:
my-webapp
|-- pom.xml
`-- src
`-- main
|-- resources
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp
This layout is compliant with Eclipse WTP (whatever plugin you're using for the Eclipse integration). Just import this project into Eclipse and there you go.
If you have more specific question, feel free to ask (for example, most of time you don't have to worry about the META-INF directory, but put it under src/main/resources if really you need to have it).
If you're using Maven, it's best to follow their convention.
If you're using Spring, you don't need an EAR. A WAR will do just fine.
A WAR file has a definite standard that you must follow. As long as you can generate a proper WAR file, you can use any directory structure for your source code that makes sense to you.
I use something like this:
/project
+---/src (.java)
+---/test (TestNG .java here)
+---/test-lib (testNG JAR, Spring test JAR, etc.)
+---/resources (log4j.xml, etc.)
+---/web (root of web content here)
+---+---/WEB-INF
+---+---+---/classes (compile .java to this directory)
+---+---+---/lib (JAR files)
I use IntelliJ, so it creates an exploded WAR file as output for me.
I have an Ant build.xml that generally follows the IntelliJ directory structure. You're welcome to crib it if you find it useful.
<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">
<property name="version" value="1.6"/>
<property name="haltonfailure" value="no"/>
<property name="out" value="out"/>
<property name="production.src" value="src"/>
<property name="production.lib" value="lib"/>
<property name="production.resources" value="config"/>
<property name="production.classes" value="${out}/production/${ant.project.name}"/>
<property name="test.src" value="test"/>
<property name="test.lib" value="lib"/>
<property name="test.resources" value="config"/>
<property name="test.classes" value="${out}/test/${ant.project.name}"/>
<property name="exploded" value="out/exploded/${ant.project.name}"/>
<property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
<property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>
<property name="reports.out" value="${out}/reports"/>
<property name="junit.out" value="${reports.out}/junit"/>
<property name="testng.out" value="${reports.out}/testng"/>
<path id="production.class.path">
<pathelement location="${production.classes}"/>
<pathelement location="${production.resources}"/>
<fileset dir="${production.lib}">
<include name="**/*.jar"/>
<exclude name="**/junit*.jar"/>
<exclude name="**/*test*.jar"/>
</fileset>
</path>
<path id="test.class.path">
<path refid="production.class.path"/>
<pathelement location="${test.classes}"/>
<pathelement location="${test.resources}"/>
<fileset dir="${test.lib}">
<include name="**/junit*.jar"/>
<include name="**/*test*.jar"/>
</fileset>
</path>
<path id="testng.class.path">
<fileset dir="${test.lib}">
<include name="**/testng*.jar"/>
</fileset>
</path>
<available file="${out}" property="outputExists"/>
<target name="clean" description="remove all generated artifacts" if="outputExists">
<delete dir="${out}" includeEmptyDirs="true"/>
<delete dir="${reports.out}" includeEmptyDirs="true"/>
</target>
<target name="create" description="create the output directories" unless="outputExists">
<mkdir dir="${production.classes}"/>
<mkdir dir="${test.classes}"/>
<mkdir dir="${reports.out}"/>
<mkdir dir="${junit.out}"/>
<mkdir dir="${testng.out}"/>
<mkdir dir="${exploded.classes}"/>
<mkdir dir="${exploded.lib}"/>
</target>
<target name="compile" description="compile all .java source files" depends="create">
<!-- Debug output
<property name="production.class.path" refid="production.class.path"/>
<echo message="${production.class.path}"/>
-->
<javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
<classpath refid="production.class.path"/>
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</javac>
<javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
<classpath refid="test.class.path"/>
<include name="**/*Test.java"/>
</javac>
</target>
<target name="junit-test" description="run all junit tests" depends="compile">
<!-- Debug output
<property name="test.class.path" refid="test.class.path"/>
<echo message="${test.class.path}"/>
-->
<junit printsummary="yes" haltonfailure="${haltonfailure}">
<classpath refid="test.class.path"/>
<formatter type="xml"/>
<batchtest fork="yes" todir="${junit.out}">
<fileset dir="${test.src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${junit.out}">
<fileset dir="${junit.out}">
<include name="TEST-*.xml"/>
</fileset>
<report todir="${junit.out}" format="frames"/>
</junitreport>
</target>
<taskdef resource="testngtasks" classpathref="testng.class.path"/>
<target name="testng-test" description="run all testng tests" depends="compile">
<!-- Debug output
<property name="test.class.path" refid="test.class.path"/>
<echo message="${test.class.path}"/>
-->
<testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
<classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
</testng>
</target>
<target name="exploded" description="create exploded deployment" depends="testng-test">
<copy todir="${exploded.classes}">
<fileset dir="${production.classes}"/>
</copy>
<copy todir="${exploded.lib}">
<fileset dir="${production.lib}"/>
</copy>
</target>
<target name="package" description="create package file" depends="exploded">
<jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
</target>
</project>