From 23cdf3753fc73471389d6c6ea8ba34840061726f Mon Sep 17 00:00:00 2001 From: Punyashloka Biswal Date: Fri, 18 Nov 2016 16:47:15 -0500 Subject: [PATCH] Add extensions block to manifest (#143) --- readme.md | 3 +++ .../javadist/DistributionExtension.groovy | 13 ++++++++++ .../javadist/JavaDistributionPlugin.groovy | 6 ++++- .../javadist/tasks/CreateManifestTask.groovy | 24 +++++++++++-------- src/main/resources/manifest.yml | 5 ---- .../javadist/DistributionExtensionTest.groovy | 6 +++++ .../JavaDistributionPluginTests.groovy | 18 ++++++++------ 7 files changed, 52 insertions(+), 23 deletions(-) delete mode 100644 src/main/resources/manifest.yml diff --git a/readme.md b/readme.md index adf1b4df..06320fda 100644 --- a/readme.md +++ b/readme.md @@ -53,6 +53,7 @@ program for a default run configuration: mainClass 'com.palantir.foo.bar.MyServiceMainClass' args 'server', 'var/conf/my-service.yml' env 'KEY1': 'value1', 'KEY2': 'value1' + manifestExtensions 'KEY3': 'value2' } The `distribution` block offers the following options: @@ -76,6 +77,8 @@ The `distribution` block offers the following options: nothing in `${projectDir}/var/data` is copied. * (optional) `javaHome` a fixed override for the `JAVA_HOME` environment variable that will be applied when `init.sh` is run. + * (optional) `manifestExtensions` a map of extended manifest attributes, as specified in + [SLS 1.0](https://github.com/palantir/sls-spec/blob/master/manifest.md). #### JVM Options The list of JVM options passed to the Java processes launched through a package's start-up scripts is obtained by diff --git a/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy b/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy index 02123ad5..62396477 100644 --- a/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy +++ b/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy @@ -43,6 +43,7 @@ class DistributionExtension { private boolean enableManifestClasspath = false private String javaHome = null private List excludeFromVar = ['log', 'run'] + private Map manifestExtensions = [:] DistributionExtension(Project project) { this.project = project @@ -108,6 +109,18 @@ class DistributionExtension { this.excludeFromVar = excludeFromVar.toList() } + public Map getManifestExtensions() { + return this.manifestExtensions; + } + + public void setManifestExtensions(Map manifestExtensions) { + this.manifestExtensions = manifestExtensions; + } + + public void manifestExtensions(Map manifestExtensions) { + this.manifestExtensions.putAll(manifestExtensions) + } + public String getServiceName() { return serviceName } diff --git a/src/main/groovy/com/palantir/gradle/javadist/JavaDistributionPlugin.groovy b/src/main/groovy/com/palantir/gradle/javadist/JavaDistributionPlugin.groovy index fd8bd929..bce245b1 100644 --- a/src/main/groovy/com/palantir/gradle/javadist/JavaDistributionPlugin.groovy +++ b/src/main/groovy/com/palantir/gradle/javadist/JavaDistributionPlugin.groovy @@ -68,7 +68,11 @@ class JavaDistributionPlugin implements Plugin { Task manifest = project.tasks.create('createManifest', CreateManifestTask) project.afterEvaluate { - manifest.configure(distributionExtension.serviceName, distributionExtension.serviceGroup) + manifest.configure( + distributionExtension.serviceName, + distributionExtension.serviceGroup, + distributionExtension.manifestExtensions, + ) } Task distTar = DistTarTask.createDistTarTask(project, 'distTar') diff --git a/src/main/groovy/com/palantir/gradle/javadist/tasks/CreateManifestTask.groovy b/src/main/groovy/com/palantir/gradle/javadist/tasks/CreateManifestTask.groovy index 88b5f520..f470295c 100644 --- a/src/main/groovy/com/palantir/gradle/javadist/tasks/CreateManifestTask.groovy +++ b/src/main/groovy/com/palantir/gradle/javadist/tasks/CreateManifestTask.groovy @@ -17,14 +17,12 @@ package com.palantir.gradle.javadist.tasks import com.palantir.gradle.javadist.JavaDistributionPlugin -import com.palantir.gradle.javadist.util.EmitFiles +import groovy.json.JsonOutput import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction -import java.nio.file.Paths - class CreateManifestTask extends DefaultTask { @Input @@ -33,6 +31,9 @@ class CreateManifestTask extends DefaultTask { @Input String serviceGroup + @Input + Map manifestExtensions + CreateManifestTask() { group = JavaDistributionPlugin.GROUP_NAME description = "Generates a simple yaml file describing the package content." @@ -50,16 +51,19 @@ class CreateManifestTask extends DefaultTask { @TaskAction void createManifest() { - EmitFiles.replaceVars( - CreateManifestTask.class.getResourceAsStream('/manifest.yml'), - getManifestFile().toPath(), - ['@serviceGroup@' : serviceGroup, - '@serviceName@' : serviceName, - '@serviceVersion@': getProjectVersion()]) + getManifestFile().setText(JsonOutput.prettyPrint(JsonOutput.toJson([ + 'manifest-version': '1.0', + 'product-type': 'service.v1', + 'product-group': serviceGroup, + 'product-name': serviceName, + 'product-version': projectVersion, + 'extensions': manifestExtensions, + ]))) } - public void configure(String serviceName, String serviceGroup) { + public void configure(String serviceName, String serviceGroup, Map manifestExtensions) { this.serviceName = serviceName this.serviceGroup = serviceGroup + this.manifestExtensions = manifestExtensions } } diff --git a/src/main/resources/manifest.yml b/src/main/resources/manifest.yml deleted file mode 100644 index 45bb99a4..00000000 --- a/src/main/resources/manifest.yml +++ /dev/null @@ -1,5 +0,0 @@ -manifest-version: 1.0 -product-group: @serviceGroup@ -product-name: @serviceName@ -product-version: @serviceVersion@ -product-type: service.v1 diff --git a/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy b/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy index 40ff193b..9bc0248e 100644 --- a/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy +++ b/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy @@ -41,6 +41,9 @@ class DistributionExtensionTest extends Specification { env 'a': 'b' env 'c': 'd' + + manifestExtensions 'a': 'b' + manifestExtensions 'c': 'd' } then: @@ -49,6 +52,7 @@ class DistributionExtensionTest extends Specification { ext.defaultJvmOpts == DistributionExtension.requiredJvmOpts + ['a', 'b', 'c', 'd'] ext.excludeFromVar == ['log', 'run', 'a', 'b', 'c', 'd'] ext.env == ['a': 'b', 'c': 'd'] + ext.manifestExtensions == ['a': 'b', 'c': 'd'] } def 'collection setters replace existing data'() { @@ -67,6 +71,8 @@ class DistributionExtensionTest extends Specification { setExcludeFromVar(['c', 'd']) setEnv(['a': 'b', 'c': 'd']) setEnv(['foo': 'bar']) + setManifestExtensions(['a': 'b', 'c': 'd']) + setManifestExtensions(['foo': 'bar']) } then: diff --git a/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy b/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy index 234380f3..02a26b48 100644 --- a/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy +++ b/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy @@ -219,7 +219,7 @@ class JavaDistributionPluginTests extends GradleTestSpec { then: String manifest = file('dist/service-name-0.1/deployment/manifest.yml', projectDir).text - manifest.contains('product-version: 0.1\n') + manifest.contains('"product-version": "0.1"') } def 'manifest file contains expected fields'() { @@ -231,11 +231,12 @@ class JavaDistributionPluginTests extends GradleTestSpec { then: String manifest = file('dist/service-name-0.1/deployment/manifest.yml', projectDir).text - manifest.contains('manifest-version: 1.0\n') - manifest.contains('product-group: service-group\n') - manifest.contains('product-name: service-name\n') - manifest.contains('product-version: 0.1\n') - manifest.contains('product-type: service.v1\n') + manifest.contains('"manifest-version": "1.0"') + manifest.contains('"product-group": "service-group"') + manifest.contains('"product-name": "service-name"') + manifest.contains('"product-version": "0.1"') + manifest.contains('"product-type": "service.v1"') + manifest.replaceAll(/\s/, '').contains('"extensions":{"service-dependencies":{"com.palantir.compass:compass-server":["1.75.0","2.0.0"]}}') } def 'produce distribution bundle with files in deployment/'() { @@ -252,7 +253,7 @@ class JavaDistributionPluginTests extends GradleTestSpec { then: // clobbers deployment/manifest.yml String manifest = file('dist/service-name-0.1/deployment/manifest.yml', projectDir).text - manifest.contains('product-name: service-name\n') + manifest.contains('"product-name": "service-name"') // check files in deployment/ copied successfully String actualConfiguration = file('dist/service-name-0.1/deployment/configuration.yml', projectDir).text @@ -459,6 +460,9 @@ class JavaDistributionPluginTests extends GradleTestSpec { serviceName 'service-name' mainClass 'test.Test' defaultJvmOpts '-Xmx4M', '-Djavax.net.ssl.trustStore=truststore.jks' + manifestExtensions 'service-dependencies': [ + 'com.palantir.compass:compass-server': ['1.75.0', '2.0.0'] + ] } sourceCompatibility = '1.7'