Add a new cli command to output version information (2nd attempt) #3892 (#3899)

* Properly git-ignore all nbactions.xml files.

* Add a command line action to print the program version (#3892).

I am using maven resource filtering capabilities so that an existing version.properties resource file
gets filtered upon build and populated with the project version tag. This resource is then read
at runtime as required.

* Using a different version tag when unreadable.
This commit is contained in:
Miguel García 2016-10-03 11:14:54 +02:00 committed by wing328
parent 1840956320
commit e633073082
6 changed files with 58 additions and 9 deletions

3
.gitignore vendored
View File

@ -4,6 +4,7 @@ out/
*.iws
classpath.txt
version.properties
!modules/swagger-codegen-cli/src/main/resources/version.properties
.project
.classpath
lib/*
@ -33,7 +34,7 @@ packages/
/target
/generated-files
/nbactions.xml
nbactions.xml
# scalatra
samples/server-generator/scalatra/output

View File

@ -18,6 +18,7 @@
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>logback.xml</exclude>
</excludes>

View File

@ -6,6 +6,7 @@ import io.swagger.codegen.cmd.ConfigHelp;
import io.swagger.codegen.cmd.Generate;
import io.swagger.codegen.cmd.Langs;
import io.swagger.codegen.cmd.Meta;
import io.swagger.codegen.cmd.Version;
/**
* User: lanwen
@ -21,16 +22,20 @@ public class SwaggerCodegen {
public static void main(String[] args) {
String version = Version.readVersionFromResources();
@SuppressWarnings("unchecked")
Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("swagger-codegen-cli")
.withDescription("Swagger code generator CLI. More info on swagger.io")
.withDescription(String.format(
"Swagger code generator CLI (version %s). More info on swagger.io",
version))
.withDefaultCommand(Langs.class)
.withCommands(
Generate.class,
Meta.class,
Langs.class,
Help.class,
ConfigHelp.class
ConfigHelp.class,
Version.class
);
builder.build().parse(args).run();

View File

@ -68,12 +68,8 @@ public class Meta implements Runnable {
"src/main/resources/META-INF/services", "io.swagger.codegen.CodegenConfig")
);
String swaggerVersion = this.getClass().getPackage().getImplementationVersion();
// if the code is running outside of the jar (i.e. from the IDE), it will not have the version available.
// let's default it with something.
if (swaggerVersion==null) {
swaggerVersion = "2.1.3";
}
String swaggerVersion = Version.readVersionFromResources();
Map<String, Object> data = new ImmutableMap.Builder<String, Object>()
.put("generatorPackage", targetPackage)
.put("generatorClass", mainClass)

View File

@ -0,0 +1,45 @@
package io.swagger.codegen.cmd;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import io.airlift.airline.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Command(name = "version", description = "Show version information")
public class Version implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(Meta.class);
private static final String VERSION_PLACEHOLDER = "${project.version}";
private static final String UNREADABLE_VERSION = "unreadable";
private static final String UNSET_VERSION = "unset";
private static final String UNKNOWN_VERSION = "unknown";
public static String readVersionFromResources() {
Properties versionProperties = new Properties();
try (InputStream is = Version.class.getResourceAsStream("/version.properties")) {
versionProperties.load(is);
} catch (IOException ex) {
LOGGER.error("Error loading version properties", ex);
return UNREADABLE_VERSION;
}
String version = versionProperties.getProperty("version", UNKNOWN_VERSION).trim();
if (VERSION_PLACEHOLDER.equals(version)) {
return UNSET_VERSION;
} else {
return version;
}
}
@Override
public void run() {
String version = readVersionFromResources();
System.out.println(version);
}
}

View File

@ -0,0 +1 @@
version = ${project.version}