替换 GWT UIBinder XML 文件中的版本号变量
帮助我找到替换 ui.xml 中版本变量的最佳方法
<gwt:HTML>
<span>Current version: ${version}</span>
</gwt:HTML>
我可以使用 Maven 资源插件吗?像这样:
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.ui.xml</include>
</includes>
</resource>
我认为 UIBinder 是 GWT 客户端代码的一部分,它由 gwt 插件编译。该插件找不到我的 ${version} 变量,也不会替换它。
Help me to find best way for replacing version variable in my ui.xml
<gwt:HTML>
<span>Current version: ${version}</span>
</gwt:HTML>
Can i use Maven resourse plugin? Like this:
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.ui.xml</include>
</includes>
</resource>
I think UIBinder is part of GWT client side code, and it compiled by gwt plugin. This plugin don't finds my ${version} variable and don't replace it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的,UIBinder 模板是 GWT 客户端代码的一部分,并被编译为 JavaScript。
我能想到的公开您的应用程序正在运行的版本的唯一方法是:
EntryPoint
类中)将其硬编码为静态最终常量。可能还有其他的,但这些是我首先想到的,按照所涉及的努力从最少到最多的顺序排列。
You are correct, UIBinder templates are part of GWT client-side code and are compiled down to JavaScript.
The only ways I can think of to expose what version your app is running are:
EntryPoint
class, as a static final constant.There may be others but those are the first ones off the top of my head, in order from least to most effort involved.
因为您的 Java 文件将在您有机会更改变量之前被编译,所以您尝试的普通过滤器将不起作用。但是,我能够使用 maven-replacer-plugin 它只是替换文件中的字符串。对我来说,采用像您建议的那样的设置要干净得多,其中
${my_variable}
格式的变量可以一致地替换为某个当前版本。然而,使用 maven-replacer-plugin,您就没有那么奢侈了,因为它实际上是在修改原始源文件本身。因此,如果您告诉它在某个时间点用
${my_variable}
替换Version 1.2.3
,该文件将不再包含文本"${ my_variable}"
因为它已经被替换了。所以你必须重新考虑你的替代策略。这就是我的设置...我添加了一个名为“VersionManager”的共享类,它只有以下代码:
在
中,我添加了以下行(可选):然后包含 maven-replacer-plugin 并按如下方式配置它:
如您所见,我告诉插件将包含
private static String version="*";
的行替换为新行,包含大部分相同的文本,但在引号内包含所需的版本号。您可以通过运行 mvn validate 来测试这一点,而无需编译整个项目,这将运行替换并应出现在源文件中。
然后你的服务器和客户端都知道它们在构建时运行的是什么版本。
Because your Java files will be compiled down before you have a chance to change the variables, the normal filters that you tried won't work. However, I was able to achieve a similar effect using the maven-replacer-plugin which just substitutes strings in files. To me, it's much cleaner to have a setup like the one you suggest in which a variable in the format of
${my_variable}
can consistently be replaced with some current version.Using the maven-replacer-plugin, however, you don't have that luxury, as it's actually modifying the raw source file itself. So if you told it to substitute
${my_variable}
forVersion 1.2.3
at some point in time, the file would no longer contain the text"${my_variable}"
as it would have already been replaced. So you'll have to rethink your substitution strategy. Here's what I setup...I added a Shared class called "VersionManager" which just has the following code:
In
<project><properties>
, I added the following line (optional):Then include the maven-replacer-plugin and configure it as follows:
As you can see, I'm telling the plugin to replace the line containing
private static String version="*";
with a new line, containing most of the same text, but with the desired version number inside the quotes.You can test this without having to compile your whole project by running
mvn validate
which will run the substitution and should appear in your source file.Then your server and client both know what version they were running when they were built.
另一种不使用替换插件的解决方案是替换 html 主机页面中包含的 div 中的值:
将 webapp 文件夹添加到 maven 配置中的 Web 资源(启用过滤):
现在在您的 GWT 中通过代码您可以轻松获取版本值:
如果您使用 GIN 注入,一个好的做法是拥有版本提供程序:
最后将版本注入您的小部件中:
Another solution, without using the replacer plugin, is to replace a value from a div contained in your html host page:
Add the webapp folder to web resources in maven configuration (enable filtering):
Now in your GWT code you can easily get the version value:
If you use GIN injection, a good pratice is to have a version provider:
And finally inject the version in your widget: