Structure for multiple JSF projects with shared code

Create a new "Java Project" in Eclipse. Add it as another project to the Deployment Assembly property of the main dynamic web project. This way it will automatically end up as a JAR in /WEB-INF/lib of the build of the web project. Since newer Eclipse versions, you can also create the project as "Web Fragment Project". This way the Deployment Assembly step will be done automatically.

Put all those shared resource files in /META-INF/resources folder of the Java project. Just treat it like WebContent/resources of the main web project. Tagfiles can just be kept in their own /META-INF/tags folder.

E.g.

CommonWebProject
 |-- META-INF
 |    |-- resources
 |    |    `-- common
 |    |         |-- css
 |    |         |    `-- some.css
 |    |         |-- js
 |    |         |    `-- some.js
 |    |         |-- images
 |    |         |    `-- some.png
 |    |         |-- components
 |    |         |    `-- somecomposite.xhtml
 |    |         `-- sometemplate.xhtml
 |    |-- tags
 |    |    `-- sometag.xhtml
 |    |-- beans.xml
 |    |-- faces-config.xml
 |    |-- some.taglib.xml
 |    |-- web-fragment.xml
 |    `-- MANIFEST.MF
 :

with

<h:outputStylesheet library="common" name="css/some.css" />
<h:outputScript library="common" name="js/some.js" />
<h:graphicImage library="common" name="images/some.png" />
<common:somecomposite />
<common:sometag />
<ui:include src="/common/sometemplate.xhtml" />
...

In case you're using Maven, the /META-INF folder has to be placed in src/main/resources and thus NOT src/main/java!

If you want to trigger the JSF annotation scanner as well so that you can put @FacesValidator, @FacesConverter, @FacesComponent, @FacesRenderer and consorts in that project as well, then create a /META-INF/faces-config.xml file as well. Below is a JSF 2.3 compatible one:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">
    <!-- Put shared faces-config.xml config here. -->
</faces-config>

The /META-INF/web-fragment.xml is mandatory for the JAR to be recognized by the servletcontainer as a "Web Fragment Project" and should already be generated by your IDE, but for sake of completeness here is how it should look like for a Servlet 4.0 compatible one:

<?xml version="1.0" encoding="utf-8"?>
<web-fragment
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-fragment_4_0.xsd"
    version="4.0">
    <!-- Put shared web.xml config here. -->
</web-fragment>

That's all.

See also:

  • Splitting up shared code and web.xml from WAR project to common JAR project
  • JSF facelets template packaging
  • Obtaining Facelets templates/files from an external filesystem or database