Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use startParameters in BuildGradle task?

Tags:

gradle

I would like to pass deployDir (with value /my_archive) to uploadArchives task in my_project:

task build (type: GradleBuild) {
        buildFile = './my_project/build.gradle'
        tasks = ['uploadArchives']
        /* startParameter =  [deployDir:"/my_archive"] ??? */      
}

I do not know how to declare the start parameters. I have tried different ways, e.g.,

startParameter = [deployDir:"/my_archive"]

Without success.

How to declare startParameter in the GradleBuild task?

like image 434
Skarab Avatar asked Oct 20 '12 16:10

Skarab


People also ask

How do you pass arguments in Gradle task?

Types of Input Arguments When we want to pass input arguments from the Gradle CLI, we have two choices: setting system properties with the -D flag. setting project properties with the -P flag.

How do you pass JVM arguments in Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


2 Answers

I assume you mean to pass the deployDir as a project property. In this case, you'll find there is a setProjectProperties(Map) method you can use:

task build (type: GradleBuild) {
    buildFile = './my_project/build.gradle'
    tasks = ['uploadArchives']
    startParameter.projectProperties = [deployDir: "/my_archive"]
}

This will enable you to access deployDir as a variable from the called build script:

uploadArchives {
 repositories {
  mavenDeployer {
   repository(url: deployDir)
   // --- or, if deployDir can be empty ---
   repository(url: project.properties.get('deployDir', 'file:///default/path'))
  }
 }
}
like image 62
rodion Avatar answered Sep 27 '22 20:09

rodion


we can set the project properties and system properties via the api

setProjectProperties(Map<String,String> projectProperties)  
setSystemPropertiesArgs(Map<String,String> systemPropertiesArgs) 

here is the sample from my local for startParameter:

task startBuild(type: GradleBuild) {
StartParameter startParameter = project.gradle.startParameter;

Iterable<String> tasks = new ArrayList<String>();
Iterable<String> excludedTasks = new ArrayList<String>();

startParameter.getProjectProperties().each { entry ->
    println entry.key + " = " + entry.value;

    if(entry.key.startsWith('t_')){
        tasks << (entry.key - 't_');
    }

    if(entry.key.startsWith('build_') && "true" == entry.value){
        tasks << (':' + (entry.key - 'build_') +':build');
    }

    if(entry.key.startsWith('x_') && "true" == entry.value){
        excludedTasks << (entry.key - 'x_');
    }
}

startParameter.setTaskNames(tasks);
startParameter.setExcludedTaskNames(excludedTasks);

println startParameter.toString();
}

we can reference the api from this link StartParameter

the startparameter is really powerful in gradle when you need to customize your gradle build logic.

like image 35
Wales Wu Avatar answered Sep 27 '22 21:09

Wales Wu