Hi Team, i want to setup a multi gradle module fo...
# community-support
s
Hi Team, i want to setup a multi gradle module for my use cases, where each module is one java library, so what is the best practice Project structure Root -> settings.gradle -> gradlew --> module1 ---> build.gradle --> module2 ---> build.gradle Requirement: • i want to define the common dependencies for the sub modules in a single place and use it for all sub modules • some times the sub modules might have their dependencies along with the common • I want to execute the precommit identification task to verify for any secrets while building any subproject, common for all subprjects • i want to run sonarquebe for all submodules but t want to maintain the separate project key and name for each submodule. How can we achieve in a better way? Tried the below approached: • using buildSrc I defined a custom plugins for all the submodules and it worked, but not sure how to do it for tasks and sonar • added the build.gradle in the root directly, defined all the dependecnies inside subproject but sub modules are not able to use common dependencies, but all other problems are resolved such as sonarquebe, etc. NOTE: i want to want anything to do at root level other than managing the submodules.
1
v
You should not configure any subprojects from the root project build script if that is what you meant last. That is called cross-project configuration and introduces project coupling which works against more sophisticated Gradle features and optimizations. Having convention plugins in
buildSrc
or an included build is exactly the right way to centralize and reuse build logic. For a set of common dependencies you could also use a bundle in a version catalog that you then use in the sub projects. Btw. I strongly recommend switching to Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support when using a good IDE like IntelliJ IDEA or Android Studio.
🙌 1
s
Hi @Vampire I am facing one difficulty to setup sonarqube in conventions.gradle file inside buildSrc, to run the sonarqube for each subproject, but when running the sonarqube for subprojects i am getting below error
Copy code
* What went wrong:
<https://github.com/chargebee/cb-openpay-libraries/actions/runs/13162527357/job/36734645017#step:6:37|36>
Execution failed for task ':buildSrc:compileJava'.
<https://github.com/chargebee/cb-openpay-libraries/actions/runs/13162527357/job/36734645017#step:6:38|37>
> Could not resolve all files for configuration ':buildSrc:compileClasspath'.
<https://github.com/chargebee/cb-openpay-libraries/actions/runs/13162527357/job/36734645017#step:6:39|38>
   > Could not find org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.0.0.2929.
<https://github.com/chargebee/cb-openpay-libraries/actions/runs/13162527357/job/36734645017#step:6:40|39>
     Searched in the following locations:
<https://github.com/chargebee/cb-openpay-libraries/actions/runs/13162527357/job/36734645017#step:6:41|40>
3 actionable tasks: 3 executed
It seems the sonar jar is not available, but i have added it in buildSrc build.gradle file as dependency, how can define this dependency to available for all sub gradle projects?
I also tried below approaches conventions.gradle file
Copy code
plugins {
    id 'java'
    id "maven-publish"
    id "org.sonarqube" version "4.0.0.2929"
}
Copy code
sonarqube {
    properties {
        property "sonar.projectKey", "${project.sonar_project_key}"
        property "sonar.projectName", "${project.sonar_project_name}"
        property "sonar.organization", "abc"
        property "sonar.host.url", "<https://sonarcloud.io>"
    }
}
I am getting "Invalid plugin request [id: 'org.sonarqube', version: '4.0.0.2929']. Plugin requests from precompiled scripts must not include a version number. Please remove the version from the offending request and make sure the module containing the requested plugin 'org.sonarqube' is an implementation dependency"
t
What does your buildSrc/build.gradle look like? Did you add gradlePluginPortal() as a repository?
👆 1
v
Also, when posting error messages, please post the full error, what you posted is just part of an error message with most relevant part left out. Make sure you are not fooled by https://youtrack.jetbrains.com/issue/IDEA-241844
s
Copy code
plugins {
    id 'groovy-gradle-plugin'
}

repositories {
    mavenCentral()
}
allprojects {
    dependencies {
        implementation 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.0.0.2929' // Add the SonarQube plugin dependency
    }
}
Copy code
Welcome to Gradle 8.12!

Here are the highlights of this release:
 - Enhanced error and warning reporting with the Problems API
 - File-system watching support on Alpine Linux
 - Build and test Swift 6 libraries and apps

For more details see <https://docs.gradle.org/8.12/release-notes.html>

Starting a Gradle Daemon (subsequent builds will be faster)
> Task :buildSrc:extractPluginRequests
> Task :buildSrc:generatePluginAdapters
> Task :buildSrc:compileJava FAILED
3 actionable tasks: 3 executed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compileJava'.
> Could not resolve all files for configuration ':buildSrc:compileClasspath'.
   > Could not find org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.0.0.2929.
     Searched in the following locations:
       - <https://repo.maven.apache.org/maven2/org/sonarsource/scanner/gradle/sonarqube-gradle-plugin/4.0.0.2929/sonarqube-gradle-plugin-4.0.0.2929.pom>
     Required by:
         project :buildSrc

* Try:
> If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources { ... }' of the repository declaration.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at <https://help.gradle.org>.
FYI, when i try ./gradlw module1sonar in my local terminal the task is getting succeeded but when i execute the same in github ci/cd, i am facing the above error, i was setting up java17 i
v
That's quite unlikely, and @Thomas Broyer already gave you the solution. ;-)
s
Really for asking this question, i am not sure where to add, can i get any doc to follow the process to add the gradlePluginPortal() as repo
v
You declared Maven Central as repository, but that dependency is not in that repository. So add the repository where it is, which is that one.
🙌 1
s
you mean
Copy code
repositories {
    gradlePluginPortal()
    mavenCentral()
v
Is switch it around as GPP automatically redirect to MC for things it does not have itself, but otherwise yes.
1
s
Thank you so much @Thomas Broyer and @Vampire it worked.
👌 1