James
James

Reputation: 273

File stucture of Java/Spring web application

I've just started learning Spring (and Java) and I'm putting together an application that has several parts to it. What I'd like is some opinions on how to structure the application in terms of within an IDE, and when deploying. I use intellij 11 with Tomcat 7 for reference.

My application consists of these pieces:

I need to share Java classes between all of these apps (my service layer), and also there's the issue of sharing UI resources (css, js, images) between the main website and the admin site.

How should I best go about organising these in my IDE and on Tomcat?

My thoughts so far are that each of the apps mentioned would be separate WAR files (they don't need to share sessions). I would create my service layer as a JAR which could be used by any of the apps. How would you represent this in intellij or Eclipse?

I guess that my UI resources on cdn.myapp.com can just be included as they normally would and again these UI resources would just be packaged as another WAR. Does this sound ok?

How do you organise your projects with multiple sub-apps like this?

As you can see I'm not necessarily after a hard and fast rule, just opinions really (though if anyone has a solid clear cut answer then great).

Thanks,

James.

Upvotes: 0

Views: 728

Answers (2)

David
David

Reputation: 61

There are essentially two ways to go about organizing your projects. With either solution, I would agree with your approach to separate the components into 4 different WAR files, and with ebaxt's recommendation that you use a build tool such as ant or gradle in addition to your IDE. A proxy such as Apache or nginx will also help with your subdomain names.

Your proposed solution would be my preferred approach: one set of projects maintains shared resources such as images and is also responsible for building the service layer. The jar files that are created by those projects can then be included in your other projects. Each application component (public site, REST service, admin, resources) would have their own project that would build a war file for deployment to tomcat. In this solution, you can set up your IDE so that the final 4 projects include/depend on the common projects. In Eclipse, this is done by configuring the build path.

The other option is to just build one monolithic project that builds four war files. If your product isn't too large, this can often be more efficient, but it will cause problems as your codebase grows.

Upvotes: 1

ebaxt
ebaxt

Reputation: 8417

I would look into using a build tool like maven, Gradle or Buildr. They all use a standard directory structure and supports sub-modules.

Using sub-modules you can for example create two "war modules", and put the shared resources in a shared jar.

/root <- Root project, contains configuration that is shared accross all sub-modules
/root/sharedResources (jar) <- Contains the things you need to share between applications
/root/admin (war) <- admin site, has a dependency on sharedResources.jar
/root/mainSite (war) <- main site, has a dependecy on sharedResources.jar

When it comes to tomcat you can deploy the different war files in separate contexts, or use a reverse-proxy to create the url scheme of choice.

IntelliJ has first class support for maven (not sure about buildr and gradle), so you just point the IDE to the root-pom.xml, and it automatically creates the correct configuration for you.

Have a look at github, for instance cucumber-jvm for examples of how to configure parent/sub modules in a given build tool.

Upvotes: 2

Related Questions