Gradle - Project Property
Table of Contents
About
Project property is a type property that has a project scope
Articles Related
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.
- Step 1 - Binding it to a variable
val archivesBaseName: String by project
- And using it in a task
tasks.register("logArchiveBaseName") {
doFirst {
println("The archiveBaseName value is ($archivesBaseName)")
}
}
Has
hasProperty('propertyName')
Example 1:
- in a ci environment, gradle could be called with
gradle -q -PisCI=true --quiet
- in a task
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
- Setting a project property via a system property
org.gradle.project.foo=bar
Via gradle.properties
foo=bar
Via env
- Setting a project property via a environment variable
ORG_GRADLE_PROJECT_foo=bar
Via command line
In build script
project.buildDir = 'target'
getProject().setBuildDir('target')
List
You see list the fix and runtime properties of a project:
- on the doc:
- API: Project properties
- with the command below:
gradlew properties
Example:
allprojects: [project ':bytle-api']
ant: [email protected]
antBuilderFactory: [email protected]a
archivesBaseName: bytle-api
artifacts: org.gra[email protected]7c61c2d1
asDynamicObject: DynamicObject for project ':bytle-api'
autoTargetJvmDisabled: false
baseClassLoaderScope: [email protected]38c7d7d
buildDir: D:\code\bytle-mono\api\build
buildFile: D:\code\bytle-mono\api\api.gradle.kts
buildPath: :
buildScriptSource: [email protected]
buildscript: [email protected]737b
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: [email protected]f4a5781
components: SoftwareComponentInternal set
conf2ScopeMappings: org.gradle.api.p[email protected]29b7175b
configurationActions: org.gradle.c[email protected]67654c5c
configurationTargetIdentifier: [email protected]8
configurations: configuration container
convention: [email protected]
defaultArtifacts: org.grad[email protected]74b644a
defaultTasks: []
deferredProjectConfiguration: [email protected]1df53
dependencies: org.gradle.api.interna[email protected]78636439
dependencyLocking: org.gr[email protected]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: or[email protected]616839
extensions: [email protected]
fileOperations: [email protected]
fileResolver: [email protected]
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.intern[email protected]5aa0d2cb
install: task ':bytle-api:install'
jar: task ':bytle-api:jar'
java: org.g[email protected]518b5906
knows: task ':bytle-api:knows'
layout: [email protected]
libsDir: D:\code\bytle-mono\api\build\libs
libsDirName: libs
listenerBuildOperationDecorator: org.gradl[email protected]3df648c7
logger: or[email protected]bb0b97e
logging: [email protected]338
mavenPomDir: D:\code\bytle-mono\api\build\poms
modelRegistry: [email protected]
modelSchemaStore: org.grad[email protected]5efa528f
module: [email protected]
mutationState: project :bytle-api
name: bytle-api
normalization: org.gradle.no[email protected]1a6438d4
objects: [email protected]
parent: root project 'bytle-parent'
parentIdentifier: root project 'bytle-parent'
path: :bytle-api
pluginManager: [email protected]3d8df8f
plugins: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]8578, [email protected]2ad2, [email protected], [email protected], [email protected]a9a14]
processOperations: org.gradle.proces[email protected]39f6760f
project: project ':bytle-api'
projectConfigurator: org.gra[email protected]612a43f1
projectDir: D:\code\bytle-mono\api
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: [email protected]09d
projectPath: :bytle-api
projectRegistry: [email protected]
properties: {...}
providers: [email protected]
publicType: org.gradle.api.plugins.BasePluginConvention
reporting: [email protected]
reportsDir: D:\code\bytle-mono\api\build\reports
repositories: repository container
resourceLoader: org[email protected]5daf01be
resources: [email protected]e
rootDir: D:\code\bytle-mono
rootProject: root project 'bytle-parent'
script: false
scriptHandlerFactory: org.gradle.api.internal.initializ[email protected]
scriptPluginFactory: [email protected]
serviceRegistryFactory: [email protected]e7
services: ProjectScopeServices
shadow: com.g[email protected]7b6ca943
shadowJar: task ':bytle-api:shadowJar'
sourceCompatibility: 1.8
sourceSets: SourceSet container
standardOutputCapture: [email protected]338
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