Java Web Project File Structure

Earlier i showed and talked about how a badly designed web project looks like with its potential problems. Going forward, i am going to fix all that by starting from scratch.

Lets start with the most important step, defining the project file structure. Keep in mind that, there shouldn't be any duplicate jars and circular dependency, also it should be easy to manage. That's where Maven comes in. Maven makes your life so much easier that its not even funny (how i used to go by before i don't even wanna remember) because it takes care of downloading binaries (jar with source and javadocs) for almost all popular java libraries and tools like spring, hibernate, log4j, apache-commons etc. these are just to name a few.

Now, if you remember, last time we created the project using maven but it was a little tedious to create all those directories and poms by hand. This time lets make them automatically shall we ^_~

Below steps are modified version of Multiple Module Projects . Can also see Multi-module projects with WTP

  1. From the command line, create a new maven project using the archetype plugin.
    c:\projects>mvn archetype:create -DgroupId=com.nyayapati.bookstore -DartifactId=nyayapati-bookstore -Dversion=0.0.1-SNAPSHOT
  2. Delete the "src" folder and open the "pom.xml" file to change the packaging of your parent project from jar to pom
    <packaging>pom</packaging><!-- change from jar to pom -->
  3. Now do the following steps:
    c:\projects>cd nyayapati-bookstore 
    >mvn archetype:create -DgroupId=com.nyayapati.bookstore.beans -DartifactId=bookstore-beans -Dversion=0.0.1-SNAPSHOT 
    >mvn archetype:create -DgroupId=com.nyayapati.bookstore.core -DartifactId=bookstore-core -Dversion=0.0.1-SNAPSHOT 
    >mvn archetype:create -DgroupId=com.nyayapati.bookstore.mysql -DartifactId=bookstore-mysql -Dversion=0.0.1-SNAPSHOT 
    >mvn archetype:create -DgroupId=com.nyayapati.bookstore.web -DartifactId=bookstore-web -Dversion=0.0.1-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp
    >mvn archetype:create -DgroupId=com.nyayapati.bookstore.ws -DartifactId=bookstore-ws -Dversion=0.0.1-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp

    This will create two web projects and three modules, no need to create src/main/java and pom.xml etc. For more pre-configured directory structures go here and here. So if we use option "-DarchetypeArtifactId=appfuse-basic-spring" then it will create a web application with Hibernate, Spring and Spring MVC dependency already set. And if you do "mvn archetype:generate" it gives you a wizard that will walk you through the various choices.
  4. Add bookstore-core dependency (by pasting the below code) in: bookstore-mysql, bookstore-web and bookstore-ws poms.xml respectively
       <dependency>
          <groupId>com.nyayapati.bookstore.core</groupId>
          <artifactId>bookstore-core</artifactId>
          <version>0.0.1-SNAPSHOT</version>
        </dependency>
  5. Add bookstore-beans dependency in: bookstore-core, bookstore-mysql, bookstore-web and bookstore-ws poms.xml respectively
       <dependency>
          <groupId>com.nyayapati.bookstore.beans</groupId>
          <artifactId>bookstore-beans</artifactId>
          <version>0.0.1-SNAPSHOT</version>
        </dependency>
  6. paste the following in bookstore-mysql's pom.xml which would in c:\projects\nyayapati-bookstore\bookstore-mysql\pom.xml

        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.6</version>      
          <scope>compile</scope>
        </dependency>

  7. change junit version to 4.1 in all poms
  8. Make sure there are <parent> tag in bookstore-core, bookstore-mysql, bookstore-web and bookstore-ws poms and there are four <module> defined in top level pom i.e. c:\projects\nyayapati-bookstore\pom.xml. it should have been created automatically
  9. Install the project in your local repository and generate the Eclipse files:
    c:\projects\nyayapati-bookstore>mvn install eclipse:eclipse
  10. Set up a new Eclipse workspace by pointing to c:\projects\nyayapati-bookstore and add the M2_REPO classpath variable.
  11. Click Import... > Existing projects into workspace. point to c:\projects\nyayapati-bookstore and hit finish.

One final step is to implement a source control like subversion. And add all files inside c:\projects\nyayapati-bookstore except the following (can be done by adding them in svnIgnore)

.classpath
.project
.settings
target

That's it!! your starting project structure is complete.