使用Eclipse Gradle插件:如何离线托管与使用依赖库(JAR文件)
Alright, let's tackle this fully offline Gradle setup in Eclipse—since you can't hit any online repos at all, we need to manually manage every dependency yourself. Here's how to do it step by step:
First, you'll need a local repository to host your JARs. Gradle plays nicely with Maven-style directory structures, which makes dependency management cleaner. For example, if you have a JAR like commons-lang3-3.12.0.jar with group ID org.apache.commons, artifact ID commons-lang3, and version 3.12.0, your directory structure should look like this:
your-local-repo/ └── org/ └── apache/ └── commons/ └── commons-lang3/ └── 3.12.0/ ├── commons-lang3-3.12.0.jar └── commons-lang3-3.12.0.pom (optional but recommended)
If you don't have a POM file, you can create a minimal one to avoid Gradle warnings. Just make sure the groupId, artifactId, and version match your dependency details:
<project> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </project>
Alternatively, if you prefer a simpler flat structure (just a folder full of JARs), you can use a libs directory in your project root—we'll cover that in the next step.
Open your project's build.gradle file and update the repositories block to point to your local repo instead of online sources. You have two options here:
Option A: Structured Maven-Style Repo
This is the most scalable approach for managing multiple versions and transitive dependencies:
repositories { // Point to your local repository (use relative path for portability) maven { url uri('../your-local-repo') } // Disable any online repos like mavenCentral() to avoid accidental download attempts }
Option B: Flat Directory (Simple JAR Folder)
If you just want to drop JARs into a folder and reference them directly:
repositories { flatDir { dirs 'libs' // Points to a 'libs' folder in your project root } }
Now reference your local JARs in the dependencies block, matching the repository style you chose:
For Structured Repos
Just use the standard dependency notation—Gradle will find the JAR in your local repo:
dependencies { implementation 'org.apache.commons:commons-lang3:3.12.0' }
For Flat Directory
You can either reference the JAR directly by file path, or use the name/version notation:
dependencies { // Direct file reference implementation files('libs/commons-lang3-3.12.0.jar') // Or using name/version (no group ID needed) implementation group: '', name: 'commons-lang3', version: '3.12.0' }
Once your build.gradle is configured, make sure Eclipse doesn't try to reach online repos:
- Right-click your project in the Package Explorer → Gradle → Toggle Offline Mode. This ensures Gradle only uses local resources.
- If you haven't imported the project yet: When importing via Gradle, check the Offline mode box in the import wizard.
- To confirm, go to Window → Preferences → Gradle and ensure Offline mode is checked.
- Finally, right-click the project → Gradle → Refresh Gradle Project to sync the changes.
If your dependencies have their own dependencies (transitive ones), you'll need to manually download those too. Here's how to get the full list:
- On a machine with internet access, create a temporary project with the same dependency declaration.
- Run
gradle dependenciesin the terminal—this will output all transitive dependencies. - Download each required JAR (and their POMs if you're using a structured repo) and add them to your local repository following the same directory structure.
That's it! Your Eclipse Gradle project should now build successfully without any internet access.
内容的提问来源于stack exchange,提问作者JBel




