16

I have some JavaScript files, a main HTML file, a main CSS file, and some CSS files that get imported into the main one.

I understand that I can put static files in two places: a) the 'war' folder; or b) the 'public' folder.

Where is the best place to put my static files? Are the two locations treated differently? Is there a 'best practice' on this issue?

David
  • 5,184
  • 3
  • 41
  • 67

3 Answers3

20

The difference between the 2 locations is that files in the public folders are copied by the gwt compiler to the 'your module' folder in the 'war' folder. This is means if you deploy the 'war' (for example via the google plugin to the google appengine) the files from the 'public' folder are not at the toplevel.

For example, if you have an index.html in the 'public' folder with gwt module named 'mymodule' and you deploy it to www.example.com it looks as follows, you need to access it via:

www.example.com/mymodule/index.html

If you have the index.html in the 'war' folder, you get:

www.example.com/index.html

Summarizing. Your landing page should be in the 'war' folder. Resource files used by the landing page can also be stored here (css, images). Any other resource file that is referred to in any gwt module file (or code, like images) should be stored in the 'public' folder related to the gwt module.

Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • 2
    Does this answer still hold for GWT 2.6? My project doesn't seem to have a `public` folder. On the other hand, if I add anything to the `war` folder, it gets deleted as soon as I recompile the project. So, where should static files be put now? – O. R. Mapper Mar 25 '14 at 10:06
7

The new way of working in GWT is to use the war folder.

But, if you project is a reusable widget library which is used in a GWT application then you should put the resources in the public folder. The compiler will make sure that the files are automatically included in the generated package.

David Nouls
  • 1,895
  • 12
  • 21
  • 2
    How do you put the css in the war folder and then reference it in the xxx.gwt.xml file? My problem is that my relative url can change in the future and I don't want to change anything which keeping it in the public gwt folder guarantees – AlfaTeK May 11 '11 at 22:32
  • see also http://www.gwtproject.org/doc/latest/DevGuideOrganizingProjects.html#DevGuideDirectoriesPackageConventions (Guidelines 6.) – Andreas Covidiot Mar 25 '14 at 09:17
  • But when placing anything into the `war` folder, it gets deleted upon recompilation of the project. Where should static resources be placed instead? – O. R. Mapper Mar 25 '14 at 10:07
  • Make sure that you have a in your App.gwt.xml and create a folder public in the package where the gwt.xml file is stored. The GWT compiler will copy all the files you put in public in generated output. (It will be in the module folder not in the war root folder). – David Nouls Mar 26 '14 at 10:25
4

As I see it, it depends on your requirements, but let's start at a speaking example first ...

I find the documentation (should be GWT 2.6.0) about this to be incorrect or at least incomplete/confusing. As I see it (I am not a guru so please correct me if my investigations are wrong!) I am looking at the following example proj structure

myproj/
  src/my/gwtproj/
     client/
       img/
         foo1.png
       AppClientBundle.java
       foo2.png
     public/
       img/
         foo3.png
       foo4.png
  war/
    img/foo5.png
    foo6.png
  .classpath
  .project

Imagine we may (or may not) need to reference such resources in some AppClientBundle interface (or other application reference context):

interfaces AppClientBundle extends ClientBundle {

  @Source("img/foo1.png")
  ImageResource fooImg();
}

Then it seems to depend on your Requirements, e.g.:

  • R.a) these resources (like images) are refered to in the application code, e.g. in our AppClientBundle interface via @Source annotations
  • R.b) these resources are to be grouped by folders, e.g. foo2.png vs. img/foo1.png
  • R.c) these resources should be available outside some specific application URL context path, e.g. if used as widget library, e.g. http://host1/gwtapp1/foo4.png vs. http://host1/gwtapp2/foo4.png
  • R.d) these resources need to be application-independently (e.g. externally) URL-referenced, e.g. http://host1/gwtapp1/foo4.png vs. http://host1/foo6.png

Here's what one can do (Possibilities) and it's implications regarding R.* above:

  • P.1) (generally recommended as I see it) put nicely folder-structured resources under my.gwtproj.client (here e.g. foo1.png)
    • this way @Source("img/foo1.png")... works fine
    • in the docs above they speek about some public folder (in my case my.gwtproj.public), but creating it as a package in Eclipse does not me allow this (since public is a reserved Java key word, but creating it via the Navigator view works)
      • however, this way the @Source above does not work (likely because it's an issue with the relative AppClientBundle file system location)
      • nevertheless if the resource should be publicly available under the application context one may have to do it via this public folder
  • P.2) put "ungrouped" resources directly under myproj/war, e.g. projdir/war/foo6.png
    • this way it can be used/found within annotations, e.g. @Source
    • and it can be referenced outside the application itself via e.g. http://host1/foo6.png
  • P.3) put folder-structured resources under myproj/war, e.g. projdir/war/img/foo5.png
    • in contrast to P.2) @Source("img/foo5.png") would not work anymore
Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96
  • A very extensive analysis, but it really should be a new question. And in that case you should better structure what your asking, otherwise it will probably be flagged as 'unclear what is being asked'. – Hilbrand Bouwkamp Mar 25 '14 at 16:06
  • 2
    Looking at the original questions, the heading, the google ranking and having had the same issues etc. I find my answer more than fitting and don't see, why we should create another duplicate question. If I may be wrong at some points though I'll thankfully correct it here if pointed out. – Andreas Covidiot Mar 26 '14 at 09:58
  • This was helpful, thanks, I hadn't heard of [ClientBundle](http://www.gwtproject.org/doc/latest/DevGuideClientBundle.html). I created a simple [example project](https://github.com/donkirkby/webandnative/blob/assets/html/src/com/github/donkirkby/webandnative/client/WebAndNativeEntryPoint.java#L22) to show how to set up the asset files and include them in a bundle. – Don Kirkby Dec 16 '14 at 07:32