diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 23b4d1954f..e8134b5536 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ jobs: - name: Build release ${{env.RELEASE_VERSION}} if: env.DO_BUILD - run: ./gradlew clean build dist distThirdParty -Pversion=%{{ + run: ./gradlew clean build dist distThirdParty -Pversion=${{env.RELEASE_VERSION}} - name: Publish build artifacts uses: actions/upload-artifact@v2 diff --git a/build.gradle b/build.gradle index 0cf6ae53f2..e2702d0d9e 100644 --- a/build.gradle +++ b/build.gradle @@ -10,9 +10,15 @@ plugins { id "org.asciidoctor.jvm.convert" version "3.3.2" } -version = "0.1" group = "com.fortify.cli" +ext.buildTime = LocalDateTime.now() +ext.getVersion = { + def result = project.findProperty('version'); + return !result || result=='unspecified' ? buildTime.format('0.yyyyMMdd.HHmmss') : result; +} +version = ext.getVersion(); + apply plugin: "io.micronaut.application" subprojects { apply plugin: "io.micronaut.library" @@ -129,6 +135,19 @@ apply from: "${gradleHelpersLocation}/readme2html.gradle" mainClassName = "com.fortify.cli.app.FCLIRootCommands" +task generateBuildProperties { + doLast { + def buildPropertiesDir = "${sourceSets.main.output.resourcesDir}/com/fortify/cli/app" + mkdir "${buildPropertiesDir}" + ant.propertyfile(file: "${buildPropertiesDir}/fcli-build.properties") { + entry(key: "projectName", value: project.name) + entry(key: "projectVersion", value: project.version) + entry(key: "buildDate", value: buildTime.format('yyyy-MM-dd HH:mm:ss')) + } + } +} +processResources.dependsOn(generateBuildProperties) + tasks.register('generateAutoCompleteDir') { doLast { mkdir "${autoCompleteDir}" diff --git a/src/main/java/com/fortify/cli/app/FCLIRootCommands.java b/src/main/java/com/fortify/cli/app/FCLIRootCommands.java index b43e96a14d..e6aee0827e 100644 --- a/src/main/java/com/fortify/cli/app/FCLIRootCommands.java +++ b/src/main/java/com/fortify/cli/app/FCLIRootCommands.java @@ -57,6 +57,7 @@ sortOptions = false, showAtFileInUsageHelp = false, resourceBundle = "com.fortify.cli.i18n.FortifyCLIMessages", + versionProvider = FortifyCLIVersionProvider.class, subcommands = { ConfigCommands.class, SSCCommands.class, diff --git a/src/main/java/com/fortify/cli/app/FortifyCLIVersionProvider.java b/src/main/java/com/fortify/cli/app/FortifyCLIVersionProvider.java new file mode 100644 index 0000000000..61051f23bc --- /dev/null +++ b/src/main/java/com/fortify/cli/app/FortifyCLIVersionProvider.java @@ -0,0 +1,30 @@ +package com.fortify.cli.app; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import picocli.CommandLine.IVersionProvider; + +public class FortifyCLIVersionProvider implements IVersionProvider { + private static final Properties buildProperties = loadProperties(); + + @Override + public String[] getVersion() throws Exception { + return new String[] {String.format("%s version %s, built on %s" + , buildProperties.getProperty("projectName", "fcli") + , buildProperties.getProperty("projectVersion", "unknown") + , buildProperties.getProperty("buildDate", "unknown"))}; + } + + private static final Properties loadProperties() { + final Properties p = new Properties(); + try (final InputStream stream = FortifyCLIVersionProvider.class.getResourceAsStream("fcli-build.properties")) { + if ( stream!=null ) { p.load(stream); } + } catch ( IOException ioe ) { + System.err.println("Error reading fcli-build.properties from classpath"); + } + return p; + } + +}