hasProperty returns null in version 1.5 on Windows 7


(Ricardo Memoria) #1

I’m using Gradle 1.5 on Windows 7 and I got the following error:

PROBLEM:

The hasProperty(“var”) returns null

Example:

if (hasProperty(‘ftpServer’)) {

… some code to be executed if ftpServer parameter is available }

Always returns null.

WORKAROUND:

use project.hasProperty(“var”) instead of hasProperty(“val”)

Thanks, Ricardo


(Peter Niederwieser) #2

Cannot reproduce. Are you doing this in a nested scope? In that case, you may have to use ‘project.hasProperty’ to call the desired method.


(Ricardo Memoria) #3

Hi Peter,

Thanks for the prompt reply. Actually I’ve created a simple gradle script to execute MySQL backups. I’m using

Gradle 1.5 build time march, 27 2013 Groovvy: 1.8.6 JVM: 1.6.0_32 OS: Windows 7 6.1 x86

I’ve created a gradle script that generates a MySQL database backup and keeps the backup files of the last 7 days. If the parameter ftpServer is defined in gradle.properties, the generated backup is transmited by FTP to a backup server (where the hasProperty(‘ftpServer’) returned null)

backup.gradle

import java.text.SimpleDateFormat;
  defaultTasks 'exec'
  // initial settings
def backupFileName = "${databaseName}.bkp"
def bkpFile = new File(backupDir, backupFileName)
def zipFileName = bkpFile.path + ".zip"
  configurations {
 ftpAntTask
}
  repositories {
 mavenCentral()
}
  dependencies {
 ftpAntTask 'ant:ant-commons-net:1.6.5', 'commons-net:commons-net:3.2'
}
    /*
 * Initialize the backup
 **/
task init << {
 println "database
 = ${databaseName}"
 println "mysqldump
= ${mysqldump}"
 println "backup dir = ${backupDir}"
  println "zip file
 = ${zipFileName}"
 }
    /*
 * Generate a MySQL backup file
 **/
task backup(type: Exec, dependsOn: init) {
 File dir = new File(backupDir)
 if (!dir.exists())
    dir.mkdir()
   // runs mysqldump and set the output file (backup file)
 commandLine "${mysqldump}", "--user=root", "--password=admin", "${databaseName}"
 File bkpfile = new File(backupDir, backupFileName)
 standardOutput file(bkpfile).newOutputStream()
}
    /*
 * Generate a zip file of the backup file
 **/
task zipBackup(type: Zip, dependsOn: backup) {
 // delete previous backup file
 file(zipFileName).delete()
   // set the arguments to generate the zip file
 from bkpFile.path
 archiveName zipFileName
}
    /*
 * Duplicate the zip backup file including date information in its
 * file name, so a history of backup files will be created
 */
task duplicateCurrentZip(type: Copy, dependsOn: zipBackup) {
 def destFileName = "${databaseName}.${currentDateString()}.bkp.zip"
 def sourceFileName = "${backupFileName}.zip"
 (new File(backupDir, destFileName)).delete()
   from backupDir
 into backupDir
 include sourceFileName
 rename sourceFileName, destFileName
}
    /*
 * Execute the backup
 **/
task exec(dependsOn: duplicateCurrentZip) << {
 println "Database name = ${databaseName}"
 println "Backup dir = ${backupDir}"
   def files = fileTree(dir: backupDir, include: "**/${databaseName}*.zip")
    // get files older than 7 days
 FileTree filesToDelete = files.matching {
  Date lastWeek = new Date() - 7
  include { FileTreeElement file ->
   return file.lastModified < lastWeek.time
  }
 }
   // delete old backup files
  filesToDelete.each { File file->
  file.delete()
 }
   // delete backup file (not compressed volume)
 bkpFile.delete()
   // the FTP backup transmition is only executed if FTP server was defined
 if (project.hasProperty('ftpServer')) {
  // load dependency libraries
  ant.taskdef(name: 'ftp', classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP', classpath: configurations.ftpAntTask.asPath)
      def ftpfile = backupFileName + ".zip"
  // send file via FTP to the backup server
  ant.ftp(server: ftpServer, userid: ftpUser, password: ftpPassword, remotedir: ftpDir, action: 'put') {
   fileset(dir: backupDir) {
    include(name: ftpfile)
   }
  }
 }
}
  def currentDateString() {
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd")
 return sdf.format(new Date())
}

And gradle.properties

databaseName = mydatabase
backupDir = backup
mysqldump = C\:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe
ftpServer = ftp.server.com
ftpUser = username
ftpPassword = userpassword
ftpDir = /remotedir

(Peter Niederwieser) #4

It’s expected that you have to use ‘project.hasProperty’ here. ‘hasProperty’ will resolve to ‘Task.hasProperty’.


(Ricardo Memoria) #5

Ok… Thanks for the reply.

It makes sense now :slight_smile: