Gradle passing arguments to a plugin task - groovy

I have this tasks from gradle-js-plugin:
combineJs {
source = sourceJs
dest = file(destDirJs + "/all.js")
}
minifyJs {
source = combineJs
dest = file( destDirJs + "/all-min.js")
closure {
warningLevel = 'QUIET'
}
}
gzipJs {
source = minifyJs
dest = file(destDirJs + "/all-gzip-min.js")
}
My problem is because I´ll have to change the values of sourceJs, and destDirJs multiple times. So I dont want to have alot of duplicated code.
So I´m trying something like this:
task gzipAll {
sourceJs = ["WebContent/plugin/bootstrap-modal/js/bootstrap-modalmanager.js", "WebContent/plugin/bootstrap-modal/js/bootstrap-modal.js", "WebContent/js/bootstrap-dropdown.js", "WebContent/js/mandatory/bootstrap-analytics-setup.js"]
destDirJs = "WebContent/js/mandatory"
tasks.combineJs().execute;
tasks.minifyJs().execute;
tasks.gzipJs().execute;
//Here I need to change sourceJs and destDir and call all the task again.
}
Actually I was tring a long shot in the dark, and I got the exception:
Caused by: groovy.lang.MissingPropertyException: Could not find property 'sourceJs' on task ':combineJs'.
I also tried something like this, but didnt work. Got some exceptions:
combineJs(sourceJs, destDirJs) {
source = $sourceJs
dest = file($destDirJs + "/all.js")
}
Can anyone with more experience help me solve this ? It´s kinda simple right?
Thanks.

I've not tested it but since gradle scripts are groovy scripts you should be able to do something like this.
['dir1', 'dir2'].eachWithIndex { dir, index ->
task "combineJs_$index"(type: combineJs) {
source = dir
dest = file(destDirJs + "/$index.js")
}
task "minifyJs_$index" (type: minifyJs) {
source = "combineJs_$index"
dest = file( destDirJs + "/$index-min.js")
closure {
warningLevel = 'QUIET'
}
}
task "gzipJs_$index" (type: gzipJs) {
source = "minifyJs_$index"
dest = file(destDirJs + "/$index-gzip-min.js")
}
}
Of course you need the correct TaskTypes and you'll probably need the change the input, but this should get you going.

Related

groovy, ant.fileScanner, fileset, dir - how to use a String variable for the dir parameter in groovy using ant.fileScanner?

I am relatively new in groovy/java, and struggle with a simple task, to process all files following a given pattern in a given directory.
I tried this below, but as soon as try to use a variable instead of a fixed string for the directory of a fileset, it tells me that it doesn't find the directory - although it found it when entered as literal string.
My question, how to fix the error in line 34/32?
My code:
import groovy.ant.AntBuilder
String hotinfoNumber = new String('5152');
String unzipSpec = new String('*.zip')
String hotinfoDownloadDir = new String('.\\hotinfo_5125')
String[] hotinfoUnzips
hotinfoUnzips = [ '*.zip' , 'H*.zip']
println ('GO, hotinfoNumber=' + hotinfoNumber + ', unzipSpec=' + unzipSpec + ', hotinfoDownloadDir=' + hotinfoDownloadDir + ', hotinfoUnzips=' + hotinfoUnzips)
AntBuilder ant = new AntBuilder();
println 'c+c'
ant.fileScanner {
fileset(dir: '.\\hotinfo_5152', includes: '*.zip')
}.each { File f ->
println "c+c - Found file ${f.path}"
}
println ("c+v")
hotinfoUnzips.each { unzipFilespec ->
println 'c+v-unzipFilespec=' + unzipFilespec
ant.fileScanner {
fileset(dir: '.\\hotinfo_5152', includes: unzipFilespec.toString())
}.each { File f ->
println "c+v - Found file ${f.path}"
}
}
println ("v+v")
hotinfoUnzips.each { unzipFilespec -> // line 32
println 'v+v-unzipFilespec=' + unzipFilespec
ant.fileScanner { // line 34
fileset(dir: hotinfoDownloadDir.toString(), includes: unzipSpec.toString())
}.each { File f ->
println "v+v - unzips-Found file ${f.path}"
}
}
and the result:
c+v - Found file G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5152\HotInfo_5125.zip
c+v-unzipFilespec=H*.zip
c+v - Found file G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5152\HotInfo_5125.zip
v+v
v+v-unzipFilespec=*.zip
Caught: G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5125 does not exist.
G:\TESTS_SAMPLES\GROOVY\test_misc\hotinfo_5125 does not exist.
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:512)
at DirAndFile2$_run_closure4.doCall(DirAndFile2.groovy:34)
at DirAndFile2.run(DirAndFile2.groovy:32)
Disconnected from the target VM, address: '127.0.0.1:57397', transport: 'socket'
Process finished with exit code 1
Any hints and suggestions are highly appreciated.
Use new String('string') or directly 'string' are equivalent.
The problem in your case is the typo in the hotinfoDownloadDir definition.
Note that you change the order of the last two numbers in new String('.\\hotinfo_5125') definition compared with the '.\\hotinfo_5152'.
new String('.\\hotinfo_5125') must be new String('.\\hotinfo_5152'), if you change this, your code will work.

Skip publication signing if signing properties are not defined

I have forked an android library on github, applied some fixes to it and now would like to get a snapshot version as a dependency to my own project, but the build task in jitpack.io fails
* What went wrong:
Execution failed for task ':project_name:signReleasePublication'.
> path may not be null or empty string. path=''
The project has publish-mavencentral.gradle script setup for publishing task with signing properties read from a file or envirnment variables which I guess is causing the issue
ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
ext["sonatypeStagingProfileId"] = ''
File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}
signing {
sign publishing.publications
}
Tried adding the required() directive, but it did not work
signing {
required { hasProperty("signing.keyId") }
sign publishing.publications
}
How to skip signing if the properties are empty?
The problem was in the key composite name. Renamed signing.keyId to signingKeyId and required() started to work as expected.

How to perform a Jooq code re-generation?

I'm using Jooq and gradle-jooq-plugin for code generation. It works fine, but I'm having a problem getting the generated code to update when a table is added or a column is dropped. I was able to force an update by changing the "packageName" config parameter and build a new package. And by going back to the original name the code was updated as expected.
What would be the correct way to re-generate code after schema change with my setup?
jooq {
version = '3.13.1'
edition = 'OSS'
generateSchemaSourceOnCompilation = true
sample(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://0.0.0.0:5432/victor'
user = 'postgres'
password = 'docker'
properties {
property {
key = 'ssl'
value = 'false'
}
}
}
generator {
name = 'org.jooq.codegen.DefaultGenerator'
strategy {
name = 'org.jooq.codegen.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.meta.postgres.PostgresDatabase'
inputSchema = 'public'
forcedTypes {
forcedType {
name = 'varchar'
expression = '.*'
types = 'INET'
}
}
}
generate {
relations = true
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = 'net.bravo.victor.model'
directory = 'src/'
}
}
}
I'm using https://github.com/etiennestuder/gradle-jooq-plugin
plugins {
id 'nu.studer.jooq' version '4.1'
}
I am not sure whether it is correct way but for me works this:
generateNavigoJooqSchemaSource {
dependsOn cleanGenerateNavigoJooqSchemaSource
}
task buildJooq(dependsOn: generateNavigoJooqSchemaSource)
So I have created task name (buildJooq) I can remember that depends on generate task (generateNavigoJooqSchemaSource) and that depends on clean (cleanGenerateNavigoJooqSchemaSource) task.
Previously I have used this code which works too:
tasks.named("generateNavigoJooqSchemaSource").configure {
outputs.upToDateWhen { false }
}
It also forces run every time.

Configure Jenkin's SonarQube section using Job-DSL

Using Job-DSL we can configure a C# project in Jenkins.
The SonarQube tasks is giving us a hard time.
StepContext.metaClass.sonar = {
-> NodeBuilder nodeBuilder = new NodeBuilder()
stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
jdk('(Inherit From Job)')
usePrivateRepository(false)
}
}
How to set the path to the sonar-project.properties config file, using the Job-DSL script?
Final script
Thanks to #Bruno César, I added pathToSonarProjectProperties as parameter.
StepContext.metaClass.sonar = { String pathToSonarProjectProperties
-> NodeBuilder nodeBuilder = new NodeBuilder()
stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
jdk('(Inherit From Job)')
usePrivateRepository(false)
project(pathToSonarProjectProperties)
}
}
The sonar function is called with the relative-to-project-root path of sonar-project.properties:
sonar("Framework\\xxx\\xxx\\sonar-project.properties")
In SonarRunnerBuilder class there is a project attribute that represents the path to a file with properties for the project.
In the same way in which you set the JDK (jdk('(Inherit From Job)')) you can set the path property. In your example, try like this:
StepContext.metaClass.sonar = {
-> NodeBuilder nodeBuilder = new NodeBuilder()
stepNodes << nodeBuilder.'hudson.plugins.sonar.SonarRunnerBuilder' {
jdk('(Inherit From Job)')
usePrivateRepository(false)
project('${your.path.here}')
}
}

How to set Excel Import Preferences in InDesign CS6 using extendscript?

This seems like it should be really simple, but for some reason I can't get it to work at all. The following code has no effect whatsoever:
function setExcelImportPrefs() {
with(app.excelImportPreferences){
rangeName = "A1:Z300";
sheetName = "whatever";
tableFormatting = TableFormattingOptions.excelFormattedTable;
}
}
And this doesn't work either:
function setExcelImportPrefs() {
with(app.excelImportPreferences){
app.excelImportPreferences.rangeName = "A1:Z300";
app.excelImportPreferences.sheetName = "whatever";
app.excelImportPreferences.tableFormatting = TableFormattingOptions.excelFormattedTable;
}
}
What am I doing wrong? I've Googled everything, and I'm at my wits end. (Note: I have InDesign CS6, version 8.0)
Edit: Put in a bracket that I accidentally left out when I was copying-and-pasting.
I'm not familiar with these settings, but you seem to have be missing a bracket. Just get rid of the with syntax all together and try
function setExcelImportPrefs() {
app.excelImportPreferences.rangeName = "A1:Z300";
app.excelImportPreferences.sheetName = "whatever";
app.excelImportPreferences.tableFormatting = TableFormattingOptions.excelFormattedTable;
}

Resources