Gradle本地仓库模块查找及多项目依赖路径配置咨询
Hey there! Let's clear up your questions about Gradle local modules and dependency management:
The \.gradle\caches\modules-2\files-2.1 directory is strictly for third-party dependencies (like jars pulled from Maven Central, JCenter, etc.) that Gradle downloads automatically. Your own custom modules (like your model module) won't show up here because this cache is reserved for external libraries, not local code you've written.
Manually configuring projectDir in every new project is definitely a hassle—here are two cleaner, scalable solutions:
Option 1: Use Gradle Composite Builds (Recommended for Local Development)
This is Gradle's official way to reuse local modules across projects without repetitive path configs.
- In your new project's
settings.gradle, just add this single line (replace the path with the actual root directory of yourmodelmodule, where its ownbuild.gradlelives):includeBuild '../path/to/your/model-module' - Then in your new project's
build.gradle, you can reference the module like any regular dependency:dependencies { implementation 'your.group.id:model:your-version' } - Bonus: When you make changes to the
modelmodule, all dependent projects will automatically pick up the updates—perfect for active development.
Option 2: Publish to Local Maven Repository (Great for Stable Modules)
If your model module is relatively stable and you want to treat it like a third-party library, publish it to your local Maven repo (default location: ~/.m2/repository on macOS/Linux, C:\Users\YourUsername\.m2\repository on Windows):
- Add the Maven publish plugin to your
modelmodule'sbuild.gradle:plugins { id 'maven-publish' } publishing { publications { maven(MavenPublication) { from components.java groupId = 'com.yourcompany' // Replace with your own group ID artifactId = 'model' version = '1.0.0' // Use your preferred version number } } } - Run this command from the
modelmodule's root directory to publish it locally:./gradlew publishToMavenLocal - Now any project can depend on it without any
settings.gradleconfig—just add the dependency inbuild.gradle:dependencies { implementation 'com.yourcompany:model:1.0.0' }
If you just need the compiled JAR of your model module, it's stored in the build/libs directory of the model module itself. But this isn't the recommended way to share dependencies across projects—stick to the two options above for better maintainability.
内容的提问来源于stack exchange,提问作者Raghuveer

