Gradle - Project Property

About

Project property is a type property that has a project scope

Management

Get

Fix Property

You can access a project property in your build script simply by using its name as you would use a variable.

Example:

tasks.register("logprojectDisplayName") {
  doFirst {
    println("The project display name is  (${project.displayName})")
  }
}

// Using a getter method
println project.buildDir
println getProject().getBuildDir()

Runtime

project property may be added by plugin on the project level.

Example with the archivesBaseName project property added by the Java plugin.

val archivesBaseName: String by project
  • And using it in a task
tasks.register("logArchiveBaseName") {
  doFirst {
    println("The archiveBaseName value is  ($archivesBaseName)")
  }
}

Ref

Has

Doc

hasProperty('propertyName')

Example 1:

  • in a ci environment, gradle could be called with
gradle -q -PisCI=true --quiet
if (project.hasProperty("isCI")) {
    println("Performing release actions")
} else {
    throw InvalidUserDataException("Cannot perform release outside of CI")
}

Example 2: with a JavaExec

val cd: String by project
val arguments: String by project

tasks.register<JavaExec>("tabli") {
  main = "net.bytle.db.tabli.Tabli"
  classpath = sourceSets["main"].runtimeClasspath
  args = mutableListOf("conf","list","tabli*")
  if(project.hasProperty("cd")){
    workingDir = File(cd)
  }
  if (project.hasProperty("arguments")){
    /**
     * We split and escape the arguments to not have any file wildcard expansion
     * (ie star will not become a list of files)
     */
    args = arguments.split(" ").map{s->"\""+s+"\""}
  }
}

Set

Via System property

org.gradle.project.foo=bar

Via gradle.properties

Gradle - gradle.properties

foo=bar

Via env

  • Setting a project property via a environment variable
ORG_GRADLE_PROJECT_foo=bar

Via command line

At the command line -

gradle -P, --project-prop       

Example:

gradle -q -Pkey=value ...

In build script

project.buildDir = 'target'
getProject().setBuildDir('target')

via extra

See Gradle - ext

List

You see list the fix and runtime properties of a project:

gradlew properties

Example:

allprojects: [project ':bytle-api']
ant: org.gradle.api.internal.project.DefaultAntBuilder@4821b43
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@72aa051a
archivesBaseName: bytle-api
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@7c61c2d1
asDynamicObject: DynamicObject for project ':bytle-api'
autoTargetJvmDisabled: false
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@238c7d7d
buildDir: D:\code\bytle-mono\api\build
buildFile: D:\code\bytle-mono\api\api.gradle.kts
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@37c0324c
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@15e0737b
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@7f4a5781
components: SoftwareComponentInternal set
conf2ScopeMappings: org.gradle.api.publication.maven.internal.pom.DefaultConf2ScopeMappingContainer@29b7175b
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@67654c5c
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@27c2a258
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@1126a6be
defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@74b644a
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@4a81df53
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@78636439
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@5cd42edd
depth: 1
description: null
displayName: project ':bytle-api'
distsDir: D:\code\bytle-mono\api\build\distributions
distsDirName: distributions
docsDir: D:\code\bytle-mono\api\build\docs
docsDirName: docs
ext: org.gradle.internal.extensibility.DefaultExtraPropertiesExtension@616839
extensions: org.gradle.api.internal.plugins.DefaultConvention@1126a6be
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@445e5421
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@277fb23f
gradle: build 'bytle-parent'
gradleKotlinDsl.projectAccessorsClassPath: AccessorsClassPath(bin=[C:\Users\gerardnico\.gradle\caches\5.6.4\gradle-kotlin-dsl-accessors\b4tuqbkjf3upd09vbsfagd6g9\cache\classes], src=[C:\Users\gerardnico\.gradle\caches\5.6.4\gradle-kotlin-dsl-accessors\b4tuqbkjf3upd09vbsfagd6g9\cache\src])
group: net.bytle
identityPath: :bytle-api
inheritedScope: org.gradle.internal.extensibility.ExtensibleDynamicObject$InheritedDynamicObject@5aa0d2cb
install: task ':bytle-api:install'
jar: task ':bytle-api:jar'
java: org.gradle.api.plugins.internal.DefaultJavaPluginExtension_Decorated@518b5906
knows: task ':bytle-api:knows'
layout: org.gradle.api.internal.file.DefaultProjectLayout@67c2f04a
libsDir: D:\code\bytle-mono\api\build\libs
libsDirName: libs
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@3df648c7
logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@bb0b97e
logging: org.gradle.internal.logging.services.DefaultLoggingManager@619cf338
mavenPomDir: D:\code\bytle-mono\api\build\poms
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@6ad4d564
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@5efa528f
module: org.gradle.api.internal.artifacts.ProjectBackedModule@34d36b13
mutationState: project :bytle-api
name: bytle-api
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@1a6438d4
objects: org.gradle.api.internal.model.DefaultObjectFactory@155beb2f
parent: root project 'bytle-parent'
parentIdentifier: root project 'bytle-parent'
path: :bytle-api
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@13d8df8f
plugins: [org.gradle.language.base.plugins.LifecycleBasePlugin@5f6b4765, org.gradle.api.plugins.BasePlugin@647276c4, org.gradle.api.plugins.ReportingBasePlugin@2ebafbf6, org.gradle.api.plugins.JavaBasePlugin@7bcb7d09, org.gradle.api.plugins.JavaPlugin@7e9e4d7a, org.gradle.api.plugins.HelpTasksPlugin@92dd03b, org.gradle.buildinit.plugins.BuildInitPlugin@70b1ffa3, org.gradle.buildinit.plugins.WrapperPlugin@50b2f789, com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin@5ccb8578, com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin@3b002ad2, com.github.jengelman.gradle.plugins.shadow.ShadowPlugin@443386f6, org.gradle.api.plugins.MavenPlugin@37a3f46, org.gradle.kotlin.dsl.provider.plugins.KotlinScriptBasePlugin@39a9a14]
processOperations: org.gradle.process.internal.DefaultExecActionFactory$DecoratingExecActionFactory@39f6760f
project: project ':bytle-api'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@612a43f1
projectDir: D:\code\bytle-mono\api
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@3378d09d
projectPath: :bytle-api
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@64e5a6dc
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@61dae658
publicType: org.gradle.api.plugins.BasePluginConvention
reporting: org.gradle.api.reporting.ReportingExtension_Decorated@6e29f241
reportsDir: D:\code\bytle-mono\api\build\reports
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@5daf01be
resources: org.gradle.api.internal.resources.DefaultResourceHandler@28a6deae
rootDir: D:\code\bytle-mono
rootProject: root project 'bytle-parent'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@61bc4e20
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@402613ac
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@652158e7
services: ProjectScopeServices
shadow: com.github.jengelman.gradle.plugins.shadow.ShadowExtension_Decorated@7b6ca943
shadowJar: task ':bytle-api:shadowJar'
sourceCompatibility: 1.8
sourceSets: SourceSet container
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@619cf338
state: project state 'EXECUTED'
status: integration
subprojects: []
targetCompatibility: 1.8
tasks: task set
testReportDir: D:\code\bytle-mono\api\build\reports\tests
testReportDirName: tests
testResultsDir: D:\code\bytle-mono\api\build\test-results
testResultsDirName: test-results
uploadArchives: task ':bytle-api:uploadArchives'
uploadShadow: task ':bytle-api:uploadShadow'
version: 0.0.1-SNAPSHOT

Documentation / Reference





Discover More
Gradle - Property

Properties are a way to define the environment of a build. Gradle has three different property scope: - java system property - project property - plugin property extra properties ...



Share this page:
Follow us:
Task Runner