简体   繁体   中英

How to externalize Strings to “resources” with Eclipse for a Maven project?

I am using Eclipse for developing a Maven project.

I tried to use the externalize Strings wizard in Eclipse. By default, the resulting messages.properties is placed within the "src/main/java" folder. Yet, when packaging my project with Maven, the properties file is thrown out from the created jar file. Therefore I tried to move the message.properties to the "src/main/resources" folder but it seems like Eclipse does not accept that!

I'd rather not modify my pom.xml since it is quite standardized in the enterprise. So is there a way to force Eclipse to accept having the properties file out of the "src/main/java"?

Thanks for your help.

I created bug 369296 at eclipse.org for this, and in the process of doing that, I found a work-around. It turns out that the m2e plugin (which manages maven projects in eclipse) adds an exclusion filter of "**" to any resource directory. This exclusion filter prevents the NLS wizard from using that resource directory as its source folder.

So, to work around this issue, edit your build path in eclipse and remove the ** exclusion from the src/main/resources folder. Then, run the NLS wizard and do all the NLS-y things you want to do. Lastly, have m2e regenerate the project configuration - this will re-add the ** exclusion.

Hope this helps. It looks like they're going to fix my bug.

If you are using spring, make sure that in your application servlet context you have:

<resources mapping="/resources/**" location="/resources/" />

In your JSP:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:message key="whatever.key.from.message.file" />

After you use the externalize Strings wizard in Eclipse, it puts the property files in src/main/java, so move the property files to src/main/resources (either in eclipse with refactor/move or outside eclipse) and then recreate the eclipse project file using maven by running the following in the project root folder from the command line

mvn eclipse:eclipse

Then clean and refresh the project inside eclipse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM