How to pass a variable between Gradle buildSrc and the rest of the project?

I have a Gradle project that has a buildSrc directory in it. Both the main project and the buildSrc project must know the URL of the Artifactory server. I would like to keep the URL in one place, and I would like it to be contained in the original control. I tried adding the url to the gradle.properties file, but it looks like it is only picked up by the main project, not the buildSrc project.

How can I share a property between two?

+6
source share
3 answers

In Gradle, buildSrc is a different assembly , not just the project in the main project. So the easiest way to share properties, etc. Between the main assembly and buildSrc is to put it in a separate gradle/sharedProperties.gradle . Then your main build.gradle project can use

 apply from: 'gradle/sharedProperties.gradle' 

and buildSrc/build.gradle can use

 apply from: '../gradle/sharedProperties.gradle' 
+3
source

There is no built-in way to exchange information between the buildSrc construct and the main assembly. However, you can read your own properties file from both (using java.lang.Properties ).

0
source

To read the simple properties of a project, you can put the following snippet at the beginning of buildSrc/build.gradle :

 def props = new Properties() props.load(new FileInputStream(GRADLE_PROPERTIES)) props.each { key, val -> extensions."$key" = val } 
0
source

Source: https://habr.com/ru/post/977301/


All Articles