21

I don't really understand the structure of directories with Maven and JSF webapp.

When I generate project I have this structure :

src
|_ main
   |_ java
   |_ resources
   |_ webapp
      |_ WEB-INF 
         |_ web.xml
      |_ index.xhtml

I want to include some resources :

  • javascript file
  • css file
  • images
  • i18n files

I can include i18n files inside src/main/resources but not anywhere and I can include JS file, CSS file and images inside src/main/webapp/resources but not anywhere...

I didn't find very clear rules on the web about directories structure with JSF and Maven.

What are the rules please ?

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • 1
    The fact that you're using JSF isn't very relevant, the end result will be a standard WAR - in which the content of webapp will be the root (and thus the content of webapp excluding WEB-INF is what will be the web tree). So, maybe start with the [Maven WAR plugin doc](http://maven.apache.org/plugins/maven-war-plugin/) and [the basic structure of a WAR file](http://www.openscope.net/2010/01/25/war-deployment-file-structure/) – fvu Nov 24 '12 at 12:04

2 Answers2

32

JSF resources which are to be referenced by <h:outputStylesheet>, <h:outputScript> and <h:graphicImage> (thus, CSS/JS/images), should end up in /resources folder of the public webcontent, there where the /WEB-INF and /META-INF folders also are.

Thus, you've to put them in /src/main/webapp/resources.

src
 `-- main
      |-- java
      |-- resources
      `-- webapp
           |-- resources
           |    |-- css
           |    |    `-- style.css
           |    |-- images
           |    |    `-- logo.png
           |    `-- js
           |         `-- script.js
           |-- WEB-INF 
           |    `-- web.xml
           `-- index.xhtml

Those i18n files (I assume you technically meant resource bundle files) have ultimately to end up in a package in /WEB-INF/classes. The /src/main/resources is intented for non-class files which are supposed to end up in /WEB-INF/classes, you should put them in there. Assuming a bundle base name of com.example.i18n.text, provide them as such:

src
 `-- main
      |-- java
      |-- resources
      |    `-- com
      |         `-- example
      |              `-- i18n
      |                   |-- text.properties
      |                   |-- text_en.properties
      |                   |-- text_es.properties
      |                   `-- text_nl.properties
      :                   

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    You're welcome. I think the biggest source of confusion is in the same naming convention of the "resources" folder and completely different purposes for each of them. – BalusC Nov 24 '12 at 12:51
  • Yes it is. Moreover it's my first JSF webapp and Maven use – Olivier J. Nov 24 '12 at 12:54
2

The best location for css/images etc is in src/main/webapp/images or src/main/webapp/css/ etc. Currently there is no location src/main/webapp/resources. The src/main/resources folder is intended for resources (property files etc.) which should be filtered or should be located into WEB-INF/classes folder. So usualy you don't like to filter images and css files. Take a look into the maven-war-plugin documentation which gives some hints and configuration examples. The best approach seemed to be to put everything into src/main/webapp/FOLDER which shouldn't be filtered in anyway otherwise you should put it into src/main/resources and you can control the filtering and the replacements

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • thank you, but when I put images, css and js files into webapp folder, I can only access images not js and css files. Surely, it is because I use h:outputStylesheet and h:outputScript. I'll look for this tag to understand well why. – Olivier J. Nov 24 '12 at 12:29