4

In a large web application, I'm using requirejs amd modules so that the scripts themselves are modular and maintainable. I have the following directory structure

web
 |-src
    |-main
       |-java
       |-resources
       |-webapp
          |-static
             |-scripts
             |-styles
             |-images
          |-static-built    //output from r.js. not checked into git
          |-WEB-INF

During build js and css are optimized using r.js into static-built folder. Gradle is the build tool.

Now the problem: The jsps refer to the scripts in static/scripts folder and this is how i want when working locally. However when building war, I want the static files to be served from static-built folder. The important thing is the source jsp should not have to change to serve the optimized files from static-built folder.

Two options that I have are: a) the gradle build while making war should include static-built instead of static. b)include static-built in addition to static and using tuckey urlrewrite pick the resouce from static-built rather than static.

What best practices are the community following in similar scenarios?

vijay
  • 444
  • 1
  • 3
  • 7

2 Answers2

1

We've setup the server to have a runtime profile (dev, qa, prod, etc) read from a system property which determines some settings based on it. When running in production profile we serve the optimized files from the WAR. In development we serve the non-minified and non-concatenated files directly from the filesystem outside the application context.

Files are structured according to the official multipage example.

Configuring serving files depends on your chosen backend solution. Here's an example for spring.

Alternatively, r.js can generate source maps and those will help with development as well.

Community
  • 1
  • 1
ekuusela
  • 5,034
  • 1
  • 25
  • 43
0

Not sure if this question is outdated already, but I had a kind of similar problem.

I had similar project structure, but with the only difference - I've split the project into 2 modules:

  • one of them (let's call it service) was java-module for back-end
  • the second one contained only js and other stuff related to front-end (let's call it ui).

Then in Gradle build 'assemble' task of the service depends on 'assemble' task of ui AND another custom task called 'pre-assemble'. This 'pre-assemble' task was copying the optimized js files to place where I wanted them to be.

So, basically, I've just added another task that was responsible for placing all the optimized js files in the proper place.