Here is the Maven structure of my project
app > common-module > webapp-module > batch-module pom.xml
The common-module exposes a Version class. This class is used by both webapp and batch modules.
The Version class has one unique static method called get. It returns the global version of the project.
The global version is stored in a properties file. When get is called from batch module (a standalone java application), the properties file is successfully loaded.
In the webapp, things are different. I have created a managed bean VersionBean that would permit any JSF page to call the get method. Whenever I use one of the following
FacesContext.getCurrentInstance().getExternalContext()
FacesContext.getCurrentInstance().getExternalContext().getContext()
Thread.currentThread().getClassLoader()
I can never find the properties.file.
How can I load the properties file (getResourceAsStream) located in a jar file from a managed bean ?
EDIT
Here is the solution I came up with based on advice from @BalusC and @eljunior
VersionBean.java
@ManagedBean(eager=true)
@ApplicationScoped
public class VersionBean {
private String version;
@PostConstruct
public void init(){
version = Version.get();
}
}
Version.java
public class Version {
public static String get() {
InputStream is = Version.class.getResourceAsStream("/version.properties");
// Read InputStream and return version string ...
}
}
I would delegate the loading of the properties file to the same classloader of the class
Version: in the method get, useVersion.class.getResourceAsStream("yourFile.properties");.That should work anywhere the Version class can be loaded, so it would work in the webapp too (of course, provided the properties file actually IS inside the common-module jar file :).
Then your VersionBean could be an application scoped bean that just loaded the property on initializing the application, something like this:
Note that if you are using a JSF version before 2.0, you’ll have to configure the managed-bean in the
faces-config.xmlinstead of using the annotations@ManagedBeanand@ApplicationScoped.