Replace version number in GWT UIBinder XML File

Help me find the best way to replace the version variable in my ui.xml

<gwt:HTML>
    <span>Current version: ${version}</span>
</gwt:HTML>

Can I use the plugin for Maven? Like this:

        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.ui.xml</include>
            </includes>
        </resource>

I think UIBinder is part of the GWT client side code, and it is compiled by a gwt plugin. This plugin does not find my variable $ {version} and does not replace it.

+3
source share
3 answers

You are right, UIBinder templates are part of the GWT client code and compiled to JavaScript.

The only ways I can imagine which version your application launches are as follows:

  • - - , , EntryPoint, .
  • , - ..
  • GWT, , . .

, , .

+3

Java , , , , . , , maven-replacer-plugin, . , , , ${my_variable} .

maven-replacer, , , . , ${my_variable} Version 1.2.3 - , "${my_variable}", . . ...

Shared, "VersionManager", :

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

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

<project><properties> ():

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

maven-replacer :

     <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, .

, , .

+3

Another solution not using the replacer plugin is to replace the value from the div contained in your html main page:

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

Add the webapp folder to web resources in the 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, good pratice has 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, add the version to your widget:

[...]

@UiField(provided = true)
protected Label     versionLabel;

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

Source: https://habr.com/ru/post/1780041/


All Articles