替换 GWT UIBinder XML 文件中的版本号变量

发布于 2024-10-07 12:46:02 字数 569 浏览 4 评论 0原文

帮助我找到替换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2024-10-14 12:46:02

你是对的,UIBinder 模板是 GWT 客户端代码的一部分,并被编译为 JavaScript。

我能想到的公开您的应用程序正在运行的版本的唯一方法是:

  1. 在客户端代码中的某个位置(例如,在您的 EntryPoint 类中)将其硬编码为静态最终常量。
  2. 通过托管页面从服务器端代码公开它,并通过执行类似 本文建议
  3. 编写一个简单的 GWT 生成器 来创建一个 UI 小部件,生成其代码以显示当前版本。本文是对生成器的一般介绍。

可能还有其他的,但这些是我首先想到的,按照所涉及的努力从最少到最多的顺序排列。

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:

  1. Hard-code it somewhere in your client-side code, e.g., in your EntryPoint class, as a static final constant.
  2. Expose it from your server-side code through your hosting page and read it in your client-side code by doing something like this article suggests.
  3. Write a simple GWT generator to create a UI widget which has its code generated to display the current version. The article is a general introduction to generators.

There may be others but those are the first ones off the top of my head, in order from least to most effort involved.

っ〆星空下的拥抱 2024-10-14 12:46:02

因为您的 Java 文件将在您有机会更改变量之前被编译,所以您尝试的普通过滤器将不起作用。但是,我能够使用 maven-replacer-plugin 它只是替换文件中的字符串。对我来说,采用像您建议的那样的设置要干净得多,其中 ${my_variable} 格式的变量可以一致地替换为某个当前版本。

然而,使用 maven-replacer-plugin,您就没有那么奢侈了,因为它实际上是在修改原始源文件本身。因此,如果您告诉它在某个时间点用 ${my_variable} 替换 Version 1.2.3,该文件将不再包含文本 "${ my_variable}" 因为它已经被替换了。所以你必须重新考虑你的替代策略。这就是我的设置...

我添加了一个名为“VersionManager”的共享类,它只有以下代码:

public class VersionManager {
  private static String version="empty";

  public static String getVersion(){
    return version;
  }
}

中,我添加了以下行(可选):

<display_version>v${project.version} #${BUILD_ID}</display_version>

然后包含 maven-replacer-plugin 并按如下方式配置它:

     <plugin>
       <groupId>com.google.code.maven-replacer-plugin</groupId>
       <artifactId>replacer</artifactId>
       <version>1.5.0</version>
       <executions>
           <execution>
               <phase>validate</phase>
               <goals>
                   <goal>replace</goal>
               </goals>
           </execution>
       </executions>
       <configuration>
           <file>--yourDirectoryPaths--/shared/VersionManager.java</file>
           <replacements>
               <replacement>
                   <token>private static String version=\".*\";</token>
                   <value>private static String version="${display_version}";</value>
               </replacement>
           </replacements>
       </configuration>
   </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} for Version 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:

public class VersionManager {
  private static String version="empty";

  public static String getVersion(){
    return version;
  }
}

In <project><properties>, I added the following line (optional):

<display_version>v${project.version} #${BUILD_ID}</display_version>

Then include the maven-replacer-plugin and configure it as follows:

     <plugin>
       <groupId>com.google.code.maven-replacer-plugin</groupId>
       <artifactId>replacer</artifactId>
       <version>1.5.0</version>
       <executions>
           <execution>
               <phase>validate</phase>
               <goals>
                   <goal>replace</goal>
               </goals>
           </execution>
       </executions>
       <configuration>
           <file>--yourDirectoryPaths--/shared/VersionManager.java</file>
           <replacements>
               <replacement>
                   <token>private static String version=\".*\";</token>
                   <value>private static String version="${display_version}";</value>
               </replacement>
           </replacements>
       </configuration>
   </plugin>

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.

戏蝶舞 2024-10-14 12:46:02

另一种不使用替换插件的解决方案是替换 html 主机页面中包含的 div 中的值:

<div id="versionInfo" style="display: none;">${project.version}</div>

将 webapp 文件夹添加到 maven 配置中的 Web 资源(启用过滤):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    [...]
    <configuration>
        [...]
        <webResources>
            <resource>
            <directory>src/main/webapp</directory>
            <filtering>true</filtering>
                <includes>
                    <include>**/*.html</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

现在在您的 GWT 中通过代码您可以轻松获取版本值:

RootPanel panel = RootPanel.get("versionInfo");
String version = panel.getElement().getInnerText();
RootPanel.getBodyElement().removeChild(panel.getElement());

如果您使用 GIN 注入,一个好的做法是拥有版本提供程序:

@Provides
@Singleton
@Named("version")
protected String getVersion()
{
    RootPanel panel = RootPanel.get("versionInfo");
    if(panel == null)
    {
        return "unknow";
    }
    else
    {
        String version = panel.getElement().getInnerText();
        RootPanel.getBodyElement().removeChild(panel.getElement()); 
        return version;
    }
}

最后将版本注入您的小部件中:

[...]

@UiField(provided = true)
protected Label     versionLabel;

public MyWidget(@Named("version") String version)
{
    this.versionLabel = new Label(version);
    this.initWidget(uiBinder.createAndBindUi(this));
    [...]
}

Another solution, without using the replacer plugin, is to replace a value from a div contained in your html host page:

<div id="versionInfo" style="display: none;">${project.version}</div>

Add the webapp folder to web resources in maven configuration (enable filtering):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    [...]
    <configuration>
        [...]
        <webResources>
            <resource>
            <directory>src/main/webapp</directory>
            <filtering>true</filtering>
                <includes>
                    <include>**/*.html</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

Now in your GWT code you can easily get the version value:

RootPanel panel = RootPanel.get("versionInfo");
String version = panel.getElement().getInnerText();
RootPanel.getBodyElement().removeChild(panel.getElement());

If you use GIN injection, a good pratice is to have a version provider:

@Provides
@Singleton
@Named("version")
protected String getVersion()
{
    RootPanel panel = RootPanel.get("versionInfo");
    if(panel == null)
    {
        return "unknow";
    }
    else
    {
        String version = panel.getElement().getInnerText();
        RootPanel.getBodyElement().removeChild(panel.getElement()); 
        return version;
    }
}

And finally inject the version in your widget:

[...]

@UiField(provided = true)
protected Label     versionLabel;

public MyWidget(@Named("version") String version)
{
    this.versionLabel = new Label(version);
    this.initWidget(uiBinder.createAndBindUi(this));
    [...]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文