7. JSF: Including additional files in Maven (local jars, JNDI datasources, properties, resource bundles ..)
This is briefly exposed in an earlier post, but I think it is important to return to this aspect.
I am using Eclipse Oxygen and working with Maven projects.
Dependencies that are not in Maven Central or other repositories, can be defined in several ways, see Roufid, but as I am simple Java programmer, I use "my way" as in Frank Sinatra's song, being inspirated in the 2ond solution (adding directly the dependency as system scope)
These are the basic steps:
1. Create the folder lib in the webapp/WEB-INF/ folder. Take into account that the webapp folder is in the src/main folder of the project.
2. Insert the desired jars (for instance myjar.jar) in the lib folder
src/main/webapp/WEB-INF/lib/myjar.jar
3. Place the dependency in the pom.xml. Focus on scope and systemPath tags:
Remember to supply the scope to "system" and refer systemPath to "${basedir}" pointing to our lib folder. The groupId, artifactID and version can be any value.
I am in a quandary, I know this is a dirty solution but for me it is simple enough, as I do not need to build any repository, and the project can be easily transported to another computer.
Usually the JNDI resources are defined in the contex.xml file. If we place it in the META-INF folder of our deployed application, it can be accessed by this application and will not affect other applications in the server.
Let's consider this context.xml with a JNDI Postgres datasource:
It can be placed in:
src/main/webapp/META-INF/context.xml
We will talk about JNDI datasources in the next post where some JPA concepts will be exposed.
A resource bundle is very similar as a property file. So in Maven it is recommended to place in the src/main/resources folder. You can nest this files in other folder as shown in this schema.
src/main/resources (folder)
|--properties (folder)
|--application.properties (file)
|--other.properties (file)
|--bundles (folder)
|--label.properties
|--label_es.properties
|--label_fr.properties
To access this file for language=fr we need this code
ResourceBundle language = ResourceBundle.getBundle("bundles.label_" + "fr");
I am using Eclipse Oxygen and working with Maven projects.
1. Web configuration files
1. Files beans.xml and faces-config.xml should be placed in:
src/main/webapp/WEB-INF/beans.xml
src/main/webapp/WEB-INF/faces-config.xml
2. Some aspects of file web.xml can be replaced or implemented in a java class annotated with @WebListener that implements ServletContextListener interface. BalusC explains in detail this issue. But if you need to use this file (not all capabilities of web.xml can be easily replaced programmatically as explains Piotr), you can locate it in the same folder as beans.xml and faces-config.xml files
src/main/webapp/WEB-INF/web.xml
src/main/webapp/WEB-INF/web.xml
2. JPA persistence.xml
1. File persistence.xml should be placed in META-INF in the resources folder.
src/main/resources/META-INF/persistence.xml
3. Local jars (dependencies)
Dependencies that are not in Maven Central or other repositories, can be defined in several ways, see Roufid, but as I am simple Java programmer, I use "my way" as in Frank Sinatra's song, being inspirated in the 2ond solution (adding directly the dependency as system scope)These are the basic steps:
1. Create the folder lib in the webapp/WEB-INF/ folder. Take into account that the webapp folder is in the src/main folder of the project.
2. Insert the desired jars (for instance myjar.jar) in the lib folder
src/main/webapp/WEB-INF/lib/myjar.jar
3. Place the dependency in the pom.xml. Focus on scope and systemPath tags:
1 2 3 4 5 6 7 | <dependency> <groupId>org.me</groupId> <artifactId>mine</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/myjar.jar</systemPath> </dependency> |
Remember to supply the scope to "system" and refer systemPath to "${basedir}" pointing to our lib folder. The groupId, artifactID and version can be any value.
I am in a quandary, I know this is a dirty solution but for me it is simple enough, as I do not need to build any repository, and the project can be easily transported to another computer.
4. JNDI Datasources (for Tomcat)
Usually the JNDI resources are defined in the contex.xml file. If we place it in the META-INF folder of our deployed application, it can be accessed by this application and will not affect other applications in the server.Let's consider this context.xml with a JNDI Postgres datasource:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <Context path="/mydb" docBase="mydb" debug="5" reloadable="true" crossContext="true"> <Resource name="log_post" auth="Container" type="javax.sql.DataSource" username="user" password="paswword" maxTotal="50" maxIdle="20" maxWait="10000" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/mydb"/> /> </Context> |
It can be placed in:
src/main/webapp/META-INF/context.xml
We will talk about JNDI datasources in the next post where some JPA concepts will be exposed.
5. Properties, resource bundles etc.
A resource bundle is very similar as a property file. So in Maven it is recommended to place in the src/main/resources folder. You can nest this files in other folder as shown in this schema.src/main/resources (folder)
|--properties (folder)
|--application.properties (file)
|--other.properties (file)
|--bundles (folder)
|--label.properties
|--label_es.properties
|--label_fr.properties
To access this file for language=fr we need this code
ResourceBundle language = ResourceBundle.getBundle("bundles.label_" + "fr");
Comments
Post a Comment