Can't add String variable to my manifest - gradle

I have a build.gradle file and I want to insert some variables into my manifest. This is what I have
def dateformat = new java.text.SimpleDateFormat("yyyy.MM.dd")
version = dateformat.format(new Date());
int userlimit = System.properties['user.limit'] == null ? 1 : System.properties['user.limit'] as int
def owner = System.properties['owner'] == null ? 'Demo version' : System.properties['owner']
task release(dependsOn: [jar, libs, obfuscate]) {
doLast {
def classesDir = new File('expanded-libs')
classesDir.mkdir()
configurations.embed.each {
println "Unzipping $it to expanded-libs.."
ant.unzip(src: it, dest: 'expanded-libs')
}
task combinedJar(type: Jar) {
from zipTree('build/foo-bar-pg-' + version + '.jar')
from configurations.embed.collect { println "Adding $it to the fat jar"; zipTree(it) }
archiveName "../foo-bar-${version}${outputFileNameSuffix}.jar";
exclude 'META-INF/*'
duplicatesStrategy 'EXCLUDE'
manifest {
attributes 'Implementation-Title': 'Foo Bar Module', 'Implementation-Version': version, 'User-Limit': userlimit, 'Licensed-To': owner
}
}
combinedJar.execute()
}
}
Now the interesting thing is that it works without the 'Licensed-To': owner piece, but somehow I can't use that variable in the manifest. For integers, it works, but owner is a String.
This is the error message I get if I run gradle release -Downer="foo" -Duserlimit=20
* What went wrong:
Execution failed for task ':release'.
> Could not find method task() for arguments [{type=class org.gradle.api.tasks.bundling.Jar}, combinedJar, build_36jmpdf
2mspiolilq5o7ipbd86$_run_closure14_closure27_closure29#547a8dfd] on task ':release'.
Can anyone help me how could I put String typed JVM arguments into my manifest?

You missed a closing } brace after the manifest attributes.

Related

Refactor duplicated code in gradle task "type: Copy"

A gradle project contains several similar task type:Copy and some of them require additional checks.
task copyPackage1(type: Copy) {
from buildPackage1
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
...
task copyPackage2(type: Copy) {
from buildPackage2
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
How it is possible to refactor duplicated checks and specify same target directory for all similar tasks (but not all Copy tasks)?
You could either implement this with a custom Gradle plugin (as suggested in this similar question on Gradle forum), or use simple Groovy method to create and configure your tasks, as follows:
// define a "task creator" method
ext.createCopyToPkgTask = { String taskName , String fromDir ->
return project.tasks.create(taskName, Copy.class){
from fromDir
into "/pkgs"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
}
/* declare your similar tasks using the creator method above */
createCopyToPkgTask("copyPackage1","buildPackage1")
createCopyToPkgTask("copyPackage2","buildPackage2")
// ...

variable is read-only when it shouldn't be

When i execute this in my script
artifacts = ['abc123-com', 'abc123-ejb', 'abc123-spec', 'abc123-war', 'abc123-war2']
task clone_workspace() << {
for (item in artifacts) {
println item
}
}
i get
> Cannot set the value of read-only property 'artifacts' on root project 'abc123'.
I have tried scoping to project with project.artifacts, and to ext with project.ext.artifacts.
what am I doing wrong here?
The following minimalist Gradle file illustrates that project already has a property for artifacts (documented here):
println "TRACER : " + project.artifacts.class
output:
bash$ gradle
TRACER : class org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated
By contrast, this version of the original is happier:
def myArtifacts = ['abc123-com', 'abc123-ejb', 'abc123-spec', 'abc123-war', 'abc123-war2']
task clone_workspace() << {
for (item in myArtifacts) {
println item
}
}

Can gradle script property extensions be shared between different scripts

If there is a build.gradle file as follows:
...
apply from: 'Other.gradle'
task hello {
project.ext.hello = "hello"
}
And Other.gradle has:
task getHello {
println project.ext.hello
}
I get an error saying:
Cannot get property 'hello' on extra properties extension as it does not exist
Is there a way to share property extensions between the scripts?
Try setting ext.hello then have the tasks update it
== build.gradle
ext {
hello = null
}
apply from: 'Other.gradle'
task hello {
doLast {
hello = "hello"
}
}
== Other.gradle
task getHello {
doLast {
println hello
}
}
If you really want to be able to set info on a task, you can also use the ext on a task and scope it to a task. If you were implementing a larger plugin you could create an extension and set it on the task.

(No such file or directory) how to make non existant file to be ignored by gradle

when i run the command 'gradle tasks' or anything for that fact, i got the following error:
Caused by: java.io.FileNotFoundException: /Users/maxit/workspace/Backbone/modules/contact-form/public/build/contact-src.js (No such file or directory)
Here is my gradle build file:
configurations {
sshAntTask
}
dependencies {
sshAntTask 'org.apache.ant:ant-jsch:1.7.1', 'jsch:jsch:0.1.29'
}
// Pull the plugin from Maven Central
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.eriwen:gradle-js-plugin:1.5.0'
}
}
// Invoke the plugin
apply plugin: 'js'
apply plugin:'base'
def appName = "some"
def version = "0.0.1"
def jsSrcDir = 'public/js'
javascript.source {
dev {
js {
srcDir jsSrcDir
include "*.js"
exclude "*.min.js"
}
}
prod {
js {
srcDir jsSrcDir
include "*.min.js"
}
}
}
task combineSrc(type: com.eriwen.gradle.js.tasks.CombineJsTask) {
source = ["${projectDir}/public/templates/templates.js","${projectDir}/public/js/models/contact_model.js", "${projectDir}/public/js/views/contact_form_view.js", "${projectDir}/public/js/app.js" ]
dest = file("${projectDir}/public/build/${appName}-src.js")
}
task appendJQuery(dependsOn: 'combineSrc') {
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
It appears, that gradle doesn't ignore a file that is none existant. The task 'combineSrc' hasn't been run, yet to create the file....and i am unable to run the task 'cobineSrc' to create the file in the first place. Its kind a dead end. what am i doing wrong and how to make this work? Thank you
The reason you're failing is, that all the stuff you currently doing during the configuration of the appendJQuery task should be done in the execution phase.
just refactor your appendJQuery task to do:
task appendJQuery(dependsOn: 'combineSrc') {
doLast{
String backboneSrc = file(new File("${projectDir}/public/build/${appName}-src.js")).text
new File("${projectDir}/public/build/${appName}-jqueryWraped.js").withWriter{ out ->
out << "(function(\$){" + file("${projectDir}/public/build/${appName}-src.js").text + "})(jQuery); \n"
}
}
}
hope that helps!
René

how to get version number or something else into my zip archive in gradle

EDIT:
I am trying to run these two commands and get these results in a gradle subproject...
gradle assemble -> databus-Developer-Build.zip
gradle -DmyVersion=1.0.2 -> databus-1.0.2.zip
Currently, my output is databus-null.zip IF I use $version instead of $myVersion. When using $MyVersion, I get the error "myVersion is not a property on that task". :( :(.
EDIT
So, trying out the first answer completely failed with "Could not find property $myVersion on task ':webserver:myZip"
NOTE: I am trying to do this in a subproject right now. Here is the subproject gradle code...
project(':webserver') {
project.ext.genLibDir = file('lib')
project.ext.fixedLibDir = file('../master/libother')
dependencies {
compile project(':master')
compile fileTree(dir: '../webserver/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework/lib', include: '*.jar')
compile fileTree(dir: '../webserver/play-1.2.4/framework', include: 'play-*.jar')
}
task deleteJars(type: Delete) {
ext.collection = files { genLibDir.listFiles() }
delete ext.collection
}
task copyJars(type: Copy) {
from(configurations.compile) {}
from(fixedLibDir) {}
into genLibDir
}
copyJars.dependsOn('deleteJars')
classes.dependsOn('copyJars')
task myZip(type: Zip) {
archiveName "dashboard-"+$myVersion+".zip"
from('..') {
include 'webserver/run*.sh'
include 'webserver/app/**'
include 'webserver/conf/**'
include 'webserver/play-1.2.4/**'
include 'webserver/public/**'
}
}
assemble.dependsOn('myZip')
//playframework has it's own generation of .classpath and .project fils so do not
//overwrite their versions. NEED to call "play.bat eclipsify" here...
task eclipse(overwrite: true) {
}
gradle.taskGraph.whenReady {taskGraph ->
if (taskGraph.hasTask(assemble) && myVersion == null) {
myVersion = 'Developer-Build'
}
}
}
thanks,
Dean
I think the reason that you get the error "myVersion is not a property on that task" is, that you have to pass the property with -P instead of "-D"
ok, the correct answer is doing something like this...(add this to allprojects section)..
if (project.hasProperty('myVersion')) {
project.ext.xVersion = project.myVersion
} else {
project.ext.xVersion = 'Developer-Build'
}
and interestingly enough this below one does NOT work because 'version' seems to be some sort of reserverd property and is set to unspecified on startup...
if (project.hasProperty('version')) {
project.ext.xVersion = project.version
} else {
project.ext.xVersion = 'Developer-Build'
}
Seeing as version seems to be a reserved property, it is most likely used in publishing artifacts so the best solution then maybe the following
if (project.hasProperty('myVersion')) {
project.version = project.myVersion
} else {
project.version = 'Developer-Build'
}
and the automated build passes in myVersion and developers of course don't.

Resources