简体   繁体   English

对于 Maven 项目,如何使用 Eclipse 将字符串外部化为“资源”?

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

I am using Eclipse for developing a Maven project.我正在使用 Eclipse 开发 Maven 项目。

I tried to use the externalize Strings wizard in Eclipse.我尝试在 Eclipse 中使用外部化字符串向导。 By default, the resulting messages.properties is placed within the "src/main/java" folder.默认情况下,生成的messages.properties放在“src/main/java”文件夹中。 Yet, when packaging my project with Maven, the properties file is thrown out from the created jar file.然而,当使用 Maven 打包我的项目时,属性文件会从创建的 jar 文件中丢弃。 Therefore I tried to move the message.properties to the "src/main/resources" folder but it seems like Eclipse does not accept that!因此,我尝试将 message.properties 移动到“src/main/resources”文件夹,但似乎 Eclipse 不接受!

I'd rather not modify my pom.xml since it is quite standardized in the enterprise.我宁愿不修改我的pom.xml ,因为它在企业中非常标准化。 So is there a way to force Eclipse to accept having the properties file out of the "src/main/java"?那么有没有办法强制 Eclipse 接受“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.为此,我在 eclipse.org 创建了错误 369296 ,在此过程中,我找到了解决方法。 It turns out that the m2e plugin (which manages maven projects in eclipse) adds an exclusion filter of "**" to any resource directory.事实证明,m2e 插件(在 eclipse 中管理 maven 项目)将“**”排除过滤器添加到任何资源目录。 This exclusion filter prevents the NLS wizard from using that resource directory as its source folder.此排除过滤器可防止 NLS 向导将该资源目录用作其源文件夹。

So, to work around this issue, edit your build path in eclipse and remove the ** exclusion from the src/main/resources folder.因此,要解决此问题,请在 eclipse 中编辑您的构建路径,并从 src/main/resources 文件夹中删除 ** 排除项。 Then, run the NLS wizard and do all the NLS-y things you want to do.然后,运行 NLS 向导并做所有你想做的 NLS 事情。 Lastly, have m2e regenerate the project configuration - this will re-add the ** exclusion.最后,让 m2e 重新生成项目配置 - 这将重新添加 ** 排除项。

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:如果您使用的是 spring,请确保在您的应用程序 servlet 上下文中具有:

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

In your JSP:在您的 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 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通过从命令行在项目根文件夹中运行以下命令,使用 maven 的项目文件

mvn eclipse:eclipse

Then clean and refresh the project inside eclipse.然后清理并刷新 eclipse 里面的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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