bld Extension to Generate a Project Version Data Class
To install, please refer to the extensions documentation.
To automatically create a generated version class using the default template in your project on compile, add the following to your build file:
@Override
public void compile() throws Exception {
genver();
super.compile();
}
@BuildCommand(summary = "Generates version class")
public void genver() throws Exception {
new GeneratedVersionOperation()
.fromProject(this)
.execute();
}
./bld compile
This is the default template:
package {{v packageName/}};
import java.util.Date;
public final class {{v className/}} {
public static final String PROJECT = "{{v project/}}";
public static final Date BUILD_DATE = new Date({{v epoch/}}L);
public static final int MAJOR = {{v major/}};
public static final int MINOR = {{v minor/}};
public static final int REVISION = {{v revision/}};
public static final String QUALIFIER = "{{v qualifier/}}";
public static final String VERSION = "{{v version/}}";
private {{v className/}}() {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}
You can specified your own template using some or all of the template value tags, as follows:
@BuildCommand(summary = "Generates MyAppVersion class")
public void genver() throws Exception {
new GeneratedVersionOperation()
.fromProject(this)
.projectName("My App")
.packageName("com.example.myapp")
.className("MyAppVersion")
.classTemplate("my_app_version.txt")
.execute();
}
// my_app_version.txt
package {{v packageName/}};
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public final class {{v className/}} {
public static final String PROJECT = "{{v project/}}";
public static final LocalDateTime BUILD_DATE = Instant.ofEpochMilli({{v epoch/}}L)
.atZone(ZoneId.systemDefault()).toLocalDateTime();
public static final String VERSION = "{{v version/}}";
private {{v className/}}() {
// no-op
}
}
Please check the GeneratedVersionOperation documentation for all available configuration options.