开发者

Replace version number variable in GWT UIBinder XML File

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>
   开发者_C百科         <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.


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.


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.


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));
    [...]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜