Thursday, November 5, 2020

Re: How to pass a build number to a gwt app?

Am 12.03.2018 um 18:43 schrieb Bruno Salmon:

> I would like my gwt app to be able to identify its version itself,
> by providing a method that returns the build number.
>
> [...]
> but I don't know how I can pass that build.number to the GWT compiler and how to
> code that method in my java source so it returns that build number.

You can use a late binding generator to create a class containing all the
data you need. Google it but here's the one I've created 13 years ago
for the exact reason you've asked about. These were the steps I've taken
(it's been 13 years and hasn't been touched since then so there might be
better ways now):

Create a data-holding class AboutData.java, that contain public members for the
informations you want to provide (let's assume it's in package mypackage.data)

public class AboutData {
/**
* The date, the application was built
*/
public Date buildDate = null;
/**
* The version of the base application being used for building
*/
public String isBuiltVersion = IMainPanel.CONSTANTS.General_Unknown();
/**
* The SVN-revision-number of the version-class of the application
*/
public int isSvnRevision = -1;
}

Create a Generator (I've placed it in package mypackage.rebind):

import a.package.completely.outside.the.gwt.app.Version;
import com.google.gwt.core.ext.Generator;
[...]

public class AboutDataGenerator extends Generator {

/**
* Generate a default constructible subclass of the requested type. The
* generator throws <code>UnableToCompleteException</code> if for any reason
* it cannot provide a substitute class
*
* @return the name of a subclass to substitute for the requested class, or
* return <code>null</code> to cause the requested type itself to be used
*
*/
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
logger.log(TreeLogger.INFO, "Creating AboutData-class with build-informations", null);
TypeOracle oracle = context.getTypeOracle();
JClassType requestedClass = oracle.findType(typeName);
String packageName = requestedClass.getPackage().getName();
String simpleName = requestedClass.getSimpleSourceName();
String proxyName = simpleName + "Proxy";
String proxyClassname = packageName + "." + proxyName;

PrintWriter pw = context.tryCreate(logger, packageName, proxyName);
if (pw == null){
return proxyClassname;
}

ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, proxyName);

factory.setSuperclass(simpleName);
factory.addImport("java.util.Date");

Calendar c = Calendar.getInstance();

SourceWriter source = factory.createSourceWriter(context, pw);
source.println("public " + proxyName + "(){");
source.indent();
source.println("buildDate = new Date(" + Long.toString(System.currentTimeMillis()) + "L);");
source.println("isBuiltVersion = \"" + escape(Version.getImplVersion()) + "\";");
source.println("isSvnRevision = " + Integer.toString(Version.REVISION) + ";");
source.outdent();
source.println("}");
source.commit(logger);

return proxyClassname;
}
}

To get this generator being executed, add the following line to your you gwt.xml-file:

<generate-with class="mypackage.rebind.AboutDataGenerator">
<when-type-assignable class="mypackage.data.AboutData"/>
</generate-with>

This will generate a class mypackage.data.AboutDataProxy that will be returned instead
of the original class when required. In your actual class where you want to access
the data, you do a GWT.create:

private void refreshValues(){
AboutData data = (AboutData) GWT.create(AboutData.class);
if (data.buildDate != null){
DateTimeFormat format = DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG);
builtValue.setText(format.format(data.buildDate));
}
else{
builtValue.setText(CONSTANTS.General_Unknown());
}
isBuiltVersion.setText(data.isBuiltVersion);
}

That approach is a bit more complicated than using define-configuration but it's
more powerful and e.g. allows you to get all kinds if information using the full
spectrum of what the JVM provides you with (calling a web service, checking
files on Jenkin's local file system, DB-accesses, etc.). Also, because you're
accessing members of a java class, you'll immediately end up with compile errors
if you have typos somewhere.


Cheers, Lothar

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/0c3d9749-db45-c939-64ff-662eccec20e3%40kimmeringer.de.

No comments:

Post a Comment