简体   繁体   中英

How to access the project properties from within a class in gradle build?

I have the following in a gradle build file, what I am trying to do is filter the content of the files before I copy them, so I created a custom filter class which would be called each time a file is copied. Now I am trying to access the project properties from MyFilter class, for example I tried to print the profile property, but I did not find a way to do that so far, can this be done?

Usually I would pass the profile when starting the build file with gradlew build -Pprofile=prd , still I really don't see a way to access those properties from MyFilter class.

apply plugin: 'java'
tasks.build.finalizedBy('copyFiles')

class MyFilter extends FilterReader  {


    MyFilter(Reader input) {
        super(new StringReader(""))
        //tried many things to print the profile property, nothing worked.
    }
    }
def profile = project.properties['profile'] ?: "dev"
def amqa = project.properties['amqa'] ?: "127.0.0.1"
def amqaEG = project.properties['amqaEG'] ?: "127.0.0.1"

task copyFiles(type: Copy) {
project.properties['amqa']
    from ('config')
    into ('.')
    include('**/*_#' + profile + '*')
    rename { filename ->
        filename.replace '_#' + profile, ''
    }

filter MyFilter

}

You need to extend BaseParamFilterReader , then you get parameters in setParameters(final Parameter[] parameters) method.

Copy task has filter overload that takes parameters, for example:

filter(ReplaceTokens, tokens: [version: '2.3.1'])

You'd probably write something like

filter(MyFilter, profile: profile)

Investigating ReplaceTokens might help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM