Search code examples
gradleenvironment-variablesbuild-script

How do I define a gradle build script environment variable from a file in my src/main/resources directory?


I'm using Gradle 2.7 on Windows 7. I have a properties file, "src/main/resources/liquibase.properties", whose properties I would like to reference in my build.gradle script. So for instance in my properties file I have

url=jdbc:mysql://localhost:3306/my_db
username=myuser
password=mypass

I would like to reference these in my script like so ...

liquibase {
  activities {
    main {
      changeLogFile 'src/main/resources/db.changelog-1.0.xml'
      url '${url}'
      username '${username}'
      password '${password}'
    }
  }
}

Also I would like to do this by just runing "gradle build" without having to specify any additional parameters on teh command line. How can I do this?

Thanks, - Dave


Solution

  • You can load the properties file then get the values from that... Here is an example

    liquibase {
      activities {
        main {
          File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
          Properties properties = new Properties()
          properties.load(new FileInputStream(propsFile))
          changeLogFile 'src/main/resources/db.changelog-1.0.xml'
          url properties['url']
          username properties['username']
          password properties['password']
        }
      }
    }