Gradle Goodness: Changing Name of Default Build File

Gradle uses the name build.gradle as the default name for a build file. If we write our build code in a file build.gradle then we don‘t have to specify the build filename when we run tasks. We can create build files with a different name other than build.gradle. For example we can define our build logic in a file sample.gradle. To run the tasks from this build file we can use the command line option -b or --build-file followed by the file name. But we can also change the project settings and set a new default build file name for our project. With the changed project settings we do not have to use the command line options -b or --build-file.

Suppose we have the following build file with the name sample.gradle:

view sourceprint?

0.// File: sample.gradle

1.task sample(description: ‘Sample task‘) << {

2.println ‘Sample task‘

3.}

4.

5.defaultTasks ‘sample‘

To run the sample task from the command line we can use the command line options -b or --build-file:

$ gradle -b sample.gradle

:sample

Sample task

BUILD SUCCESSFUL

Total time: 3.168 secs

$ gradle --build-file sample.gradle

:sample

Sample task

BUILD SUCCESSFUL

Total time: 2.148 secs

$

To change the default build file name for our project we create a file settings.gradle in our project. Inside the settings.gradle file we can change the property buildFileName for rootProject:

view sourceprint?

0.// File: settings.gradle

1.// Change default build file name for this project.

2.rootProject.buildFileName = ‘sample.gradle‘

Now we execute the tasks from sample.gradle without the options -b or --build-file:

$ gradle

:sample

Sample task

BUILD SUCCESSFUL

Total time: 3.312 secs

$

Code written with Gradle 2.1.

时间: 2024-08-07 17:14:31

Gradle Goodness: Changing Name of Default Build File的相关文章

Gradle Goodness: Rename Ant Task Names When Importing Ant Build File

Migrating from Ant to Gradle is very easy with the importBuild method from AntBuilder. We only have to add this single line and reference our existing Ant build XML file and all Ant tasks can now be executed as Gradle tasks. We can automatically rena

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T

Gradle Goodness: Run a Build Script With a Different Name

Normally Gradle looks for a build script file with the name build.gradle in the current directory to execute a build. But we can easily use a different name or directory for the build file. We only have to use the -b or --build-file command-line opti

Gradle Goodness: Continue Build Even with Failed Tasks

If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the comman

Eclipse项目导入Android Stuio 配置出现 Timeout waiting to lock buildscript class cache for build file &#39;H:\studioproject\Generic_SN\build.gradle&#39;

Eclipse项目导入Android Stuio 配置出现 Error:Timeout waiting to lock buildscript class cache for build file 'H:\studioproject\Generic_SN\build.gradle' (C:\Users\Administrator\.gradle\caches\2.2.1\scripts\build_81ep6udn3nlzszbotl32uedjz\ProjectScript\buildscri

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

Gradle Goodness: Check Task Dependencies With a Dry Run

We can run a Gradle build without any of the task actions being executed. This is a so-called dry run of our build. We can use the dry run of a build to see if the task dependencies we have defined or are defined in a plugin are defined properly. Bec