stevesaliman / gradle-properties-plugin

Gradle plugin to simplify loading project properties from external environment specific files
Apache License 2.0
192 stars 28 forks source link

Overwriting properties file does not do the "requiredProperties" check #27

Closed ravi-b-m closed 7 years ago

ravi-b-m commented 7 years ago

If the properties files that is being created already exists and when I run the prep task for a different env and the env-properties files does not have some of the "required Properties", the plugin does not do the check. So I tried the delete task first and then prep task using mustRunAfter and also dependsOn, the delete task is not deleting the file when prep task is called, but deletes the file when I call the delete separately. Am I missing something here ? Following is how I am doing

task deleteGradleProperties(type:Delete){
    outputs.upToDateWhen { false }
    doFirst{
        delete './gradle.properties'
    }

}

task prep() {
    dependsOn deleteGradleProperties
   //tried both same result
   // mustRunAfter deleteGradleProperties
    requiredProperties \
            "mlHost",\
            "mlAppName",\
            "mlRestPort",\
            "mlAdminUsername",\
            "mlAdminPassword",\
            "mlRestAdminUsername",\
            "mlRestAdminPassword",\
            "mlManageUsername",\
            "mlManagePassword",\
            "mlWriterUsername",\
            "mlWriterPassword",\
            "mlReaderUsername",\
            "mlReaderPassword",\
            "mlModulePermissions"

    outputs.file new File('data', 'gradle.properties')
    outputs.upToDateWhen { false }
    doFirst {
        copy {
            from "src/main/template"
            include 'gradle.properties'
            into '.'
            filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: project.filterTokens)
        }
    }
}
ravi-b-m commented 7 years ago

The only way I fixed my problem is by using project.delete('gradle.properties') in my prep task..

ravi-b-m commented 7 years ago

Sorry accidentally closed it... still does not work :)

stevesaliman commented 7 years ago

The first thing that catches my attention is that you are trying to delete and recreate the gradle.properties file. You probably shouldn't do this, as it defeats the purpose of this plugin. By using this plugin you can override the properties in gradle.properties with environment specific values in the various 'gradle-\<env>.properties' files. The kinds of files you'd typically delete and recreate from templates are files that need to be included in your build that might need to change from environment to environment, such as log4j.properties, Spring application.properties files, etc.

This may also explain why requiredProteries isn't working as you expect. If I unserstand your setup correctly you have a property with a value in gradle.properties from your last build, but that property is not in the particular 'gradle-\<env>.properties' for this build. In this case, Gradle will do the following:

  1. Read in all the property files in the order specified in this plugin's README file. At this point no tasks have actually run, so gradle.properties still exists with the values from the last run.
  2. Check the required properties of the prep task. At this point, the property in question has a value (from the not yet deleted gradle.properties file), so requiredProperties is happy.
  3. Run the tasks of your build. This will recreate the gradle.properties file, though it will probablt be incorrect.

In my builds, I have a clean task that removes generated files, and a prep task to regenerate them, but I don't have a dependency between them because prep should be out of date when Gradle sees that a property value has changed. As for build logic, I'd put properties in gradle.properties files that are likely to be standard across environments, such as mlAppname or mlRestPort, or mlAdminUserName. For properties that are different, like mlAdminUserPassword, I'd put the production value into gradle-prod.properties, the test value into gradle-test.properties, etc. If, for some reason, the test environment needed a different rest port than the standard, I could redefine mlRestPort in gradle-test.properties as well.

I hope this helps.