4

I'm doing an app that has jee6 backend (json-based api only) and a lot of client side code. It looks like Single Page Application (this cool buzzword these days).

I'm not sure how to structure my codebase. There are ways I consider

  • have separate projects for backend and frontend - this means having backend part in pure java, with maven and all that stuff and have separate frontend with all the js-specific build tools etc.
  • have everything in one project, so that I can get one archive (war) to deploy on server with all the frontend stuff inside. I don't really like that approach...

What about the deployment if I go with the first one? I'd have to build another "war" archive from this frontend part and drop it on the server?

I couldn't find any practices people use.

grafthez
  • 3,921
  • 4
  • 28
  • 42

1 Answers1

4

I would leave your Javascript and CSS package with the WAR and use something like Wro4J (I have my own asset pipeline code I can share at some point).

Heres why:

  1. To ease development you'll want the Javascript/CSS to refresh when you edit them while you run your servlet container. Maven, Eclipse (and/or JRebel) resource refresher will not work with weird package management.
  2. There isn't really any thing like a WAR/JAR for Javascript/CSS only minification (Require.js and JAM are more for dependency loading).

The reason you have separate Java code in separate projects/jars is to avoid coupling, improve reuse, and increase cohesion. What your trying to do for separation is mainly for cohesion. In large part you don't need to separate the Javascript from your Web App because its already probably very separate from the Java and is a completely different language/runtime.

However I can understand if you say wanted to CDN your JS/CSS and why separating the code might be easier for experimenting.

Here are two options:

  1. Git's submodules and have the Javascripts and CSS as separate projects that are submodules to your WAR project.
  2. Have two WAR projects. One with your backend Java code and another with your frontend code. You'll need a container that supports multiple WARS and you will have worry about managing the dreaded servlet container context path.

I have actually done number #2 (laugh at that one) at a previous company I worked for. It worked pretty well. However now days I prefer #1 for its quickness and ease (laugh at that one).

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Thanks, the one with git modules seems cool. One more question: what do you mean by managing servlet context path in case of multiple deployments? I'd put them in e.g. JBoss 7: backend.war(/backend) and frontent.war(/client). – grafthez Dec 16 '12 at 14:38
  • You can only have one WAR as the Root WAR ie its base context path is "`/`". Thus you will have to prefix the urls to your JS/CSS resources with something like `/frontend/` if you do not make the frontend war the ROOT war. If its still confusing I recommend you google servlet context path. – Adam Gent Dec 16 '12 at 16:00
  • It is also possible to keep web resources packaged in jars, see webjar - initiative (http://www.jamesward.com/2012/04/25/introducing-webjars-web-libraries-as-managed-dependencies). This allows client side resource management similar to java artifacts. – Alex Objelean Dec 17 '12 at 19:38
  • @AlexObjelean I did not know of that project. I'm guessing its probably using Classpath resolution to find the resources. So far I have been playing with Jam but just gave up on it. So far we have been using our own Servlet Filter that does the gzip, etag, and cache fingerprint busting. It does somethings Wro4j and others didn't do that worked well with Eclipse/JRebel reloading issues I had. One of these days I'll throw it on github. – Adam Gent Dec 18 '12 at 01:25
  • @AdamGent the webjar project aims only to package web resources, without handling the resource retrieval from classpath. It is up to you to decide how to load them in your application. Regarding your implementation of servletFilter, I'm curious about its implementation and of missing features. Would gladly add them in the future release of wro4j. – Alex Objelean Dec 18 '12 at 13:19
  • @AlexObjelean What the servlet filter does (and I'll add more details to the mailing list) that Wro4j doesn't do is in debug mode the files are not concatented (grouped). Instead the servlet filter sets an attribute with all the `` or ` – Adam Gent Dec 18 '12 at 13:35
  • @AlexObjelean See Rails asset [**fingerprint**](http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care). I also upload the assets to cloudfiles (Rackspace doesn not have origin pull)... again why the fingerprint is important. – Adam Gent Dec 18 '12 at 13:37
  • @AlexObjelean it does look [Wro4J has fingerprinting](http://www.operatornew.com/2012/10/adding-web-resource-fingerprinting-to.html) through taglib (I swear it wasn't in the version I used a while ago). I still like how my servlet filter just goes ahead and generates the HTML. No custom taglib (I use Handlebars.java). – Adam Gent Dec 18 '12 at 13:48