Gradle - Include Properties File

前端 未结 9 1019
天命终不由人
天命终不由人 2020-12-08 04:13

How would I include a properties file in Gradle?

For example in Ant, I could do the following:



        
相关标签:
9条回答
  • 2020-12-08 04:20

    In your task (jar or any other task) you can do this way to include any file into the destination folder

    jar{
    
        //jar task related code
    
       from("src/main/......(source)") { 
         include 'xxx.properties' into('com/.....(destination)') 
    
       }
    
    }
    
    0 讨论(0)
  • 2020-12-08 04:23

    I would actually recommend using Gradle's default properties file. If you put the properties in gradle.properties in the same directory as the build.gradle, they will automatically be available.

    See the user guide.

    0 讨论(0)
  • 2020-12-08 04:24

    Use GRADLE_USER_HOME env variable to set gradle's home directory. Put there gradle.properties file and set parameters.

    https://docs.gradle.org/current/userguide/build_environment.html

    The properties file in the user's home directory has precedence over property files in the project directories.

    0 讨论(0)
  • 2020-12-08 04:26

    For Gradle 1.x (deprecated in Gradle 2.x)

    To include your .properties file you can just use something like this:

        apply from: "version.properties"
    

    and that's it!

    0 讨论(0)
  • 2020-12-08 04:30

    Here is how I include a .csv file in my .jar that Gradle builds:

    sourceSets {
      main {
        java {
          srcDir 'src/main/java'
          output.classesDir   = 'build/classes/main'      
        }
        resources {
          srcDir 'src/main/resources'
          include '*.csv'
          output.resourcesDir = 'build/resources/main'
        }
      }
    }
    

    Then, when my application loads the resource, that is done independently of Gradle.

    0 讨论(0)
  • 2020-12-08 04:30

    I have been sorely missing this feature of Ant, especially the in-property replacement like work.dir=${basedir}/work. With some of the large builds I work with that perform lots of environment setup for automated tests, there are some tasks that just need user and environment configurable settings. The simple "gradle.properties" just isn't enough, especially for settings that are shared across multiple projects.

    I put together an example gradle file that manages a reasonable property loading scheme: load-properties.gradle. This lets the project define exactly the properties it uses in a default-properties.gradle file, and allows users to define their local settings to override them. Because the "property" files read in are gradle scripts, you can include extra logic in them, and the values aren't limited to just strings.

    Additionally, it allows for defining a per-environment properties file to use (such as "mac", or "jenkins-machine", or "remote") by passing in a "-Penv=(env name)" parameter to gradle.

    0 讨论(0)
提交回复
热议问题