How to determine what happens in settings.gradle vs. bulid.gradle?

I'm currently learning gradle by reading a bunch of gradle books and looking at gradle build scripts for large Java projects like spring, hibernate and gradle frameworks in order to understand best practices for using gradle.

I noticed that in the gradle multiproject assembly there are two settings.gradle and build.gradle my questions.

  • Is settings.gradle required to create multiple projects? can it be transferred to the build.gradle file?
  • In the several open source projects that I looked at, I noticed that settings.gradle actually contains the code, not just the settings, see examples of code that I found in the various settings.gradle files. What code is supposed to be put in settings.gradle ? is there any best practice regarding what happens in settings.grdale vs. build.gradle to follow? Do open source related projects violate these best practice rules?

From spring settings.gradle security project, select only a few lines of code from https://github.com/spring-projects/spring-security/blob/master/settings.gradle

 include modules modules.each {name -> def p = findProject(":${name}") p.name = "spring-security-${name}" p.buildFileName = "${name}.gradle" } include samples samples.each {name -> def p = findProject(":${name}") def fullName = name.replaceAll('/','') p.name = "spring-security-samples-${fullName}" p.projectDir = new File(settingsDir, "samples/${name}"); if(!p.buildFile.exists()) { def buildFile = fullName.replaceFirst("-xml","") p.buildFileName = "${buildFile}.gradle" } } 

And from the gradle project itself, a small fragment from https://github.com/gradle/gradle/blob/master/settings.gradle

 rootProject.children.each {project -> String fileBaseName = project.name.replaceAll("\\p{Upper}") { "-${it.toLowerCase()}" } String projectDirName = "subprojects/$fileBaseName" project.projectDir = new File(settingsDir, projectDirName) project.buildFileName = "${fileBaseName}.gradle" assert project.projectDir.isDirectory() assert project.buildFile.isFile() } 
+5
source share
1 answer

settings.gradle corresponds to the following class , and this is the script executable, as well as build.gradle . This is not necessary in projects with one module (but can be defined, for example, to declare a project name), but it is required for projects with several modules, since it configures such projects (see, for example, include ). It cannot be ported to build.gradle due to different APIs.

Yes, settings.gradle contains the code as described above (regular script + corresponding class). Basically, it should contain code related to the configuration of a project with several modules (including modules, paths, names) exactly what you can see in the above examples. There is no strictly defined set of good practices - you should mainly rely on documents. Over time and after gaining experience, you will know what should be there.

Hope this helps. Feel free to ask for any clarification.

+1
source

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


All Articles