11 Mart 2014 Salı

ResourceBundle Usage In Maven

Recently, I've been working on converting standard web application projects to Maven web application projects in Netbeans. During this process, I've faced a problem. In standard java or java web projects, you can reach a properties file in every location inside the project. I mean if you put your properties file (eg. messages.properties) in a package like com.test.myproject.resources, you can reach that properties file with ResourceBundle like:

ResourceBundle rb = ResourceBundle.getBundle("com.test.myproject.resources.messages");

When you convert your projects to maven projects, you cannot access resources like this. Maven has a unique directory hierarchy to follow. When you create a Maven project, you will see a directory hierarchy like:

PROJECT_NAME > src > main > java - resources - webapp - ...

You can find the whole hierarchy in https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

In this hierarchy, Maven forces you to put the related files to related folders. You should put web files to webapp folder, java files to java folder and resources to resource folder. If resource folder is not created automatically,  you can generate one. After that, messages.properties file should be put into resources folder. Then, you can reach your properties file by using ResourceBundle like:

ResourceBundle rb = ResourceBundle.getBundle("messages");

Maven sees resources folder as a root folder for resources so you do not need to specify and package name. You can successfully use your properties file in Maven.

( resourcebundle, maven, directory hierarchy, properties )

Hiç yorum yok:

Yorum Gönder