Gradle多根多模块项目构建问题:Jar无法运行及IntelliJ运行异常
Hey there! Let's tackle your problems one by one—getting those component Jars to run properly and fixing the IntelliJ execution quirks.
1. Align Project Structure with settings.gradle
Your current settings.gradle doesn't match your actual project directory hierarchy, which is likely causing hidden dependency resolution issues. Update it to reflect the components/ and modules/ folder structure:
// settings.gradle rootProject.name = 'gradle_root_project' // Include components with their correct directory paths include 'components:software_component_a', 'components:software_component_b' // Include modules under the modules folder include 'modules:module_a', 'modules:module_b', 'modules:module_c', 'modules:module_d'
This ensures Gradle recognizes all subprojects correctly, so your component projects can reference modules without path errors.
2. Fix Component Dependency References
Update your software component's build.gradle to use the corrected project paths from the updated settings.gradle:
// components/software_component_a/build.gradle plugins { id 'java' id 'application' id 'io.freefair.lombok' version '4.1.1' } application { mainClassName = 'io.cloudsystem.Controller' // Double-check this class has a valid main() method! } dependencies { // Match project paths to what's defined in settings.gradle implementation project(':modules:module_a') implementation project(':modules:module_b') implementation project(':modules:module_c') // Add module_d if your component depends on it }
3. Generate Runnable Fat Jars
The default jar task only packages your component's code, not its dependencies—this is why your current Jar won't run. Use one of these methods to create a self-contained runnable Jar:
Option A: Use the Shadow Plugin (Simpler)
Add the Shadow plugin to your component's build.gradle to automatically build a "fat jar" with all dependencies included:
plugins { // ... existing plugins ... id 'io.freefair.shadow' version '8.1.0' // Pick a version compatible with your Gradle setup } shadowJar { mergeServiceFiles() manifest { attributes 'Main-Class': 'io.cloudsystem.Controller' } }
Build the runnable Jar with:
gradle :components:software_component_a:shadowJar
The Jar will be located at components/software_component_a/build/libs/software_component_a-2.0.0-all.jar (the -all suffix marks it as a fat jar).
Option B: Manual Fat Jar Task
If you prefer avoiding extra plugins, define a custom fatJar task in your component's build.gradle:
task fatJar(type: Jar) { manifest { attributes 'Main-Class': 'io.cloudsystem.Controller' } // Include all runtime dependencies from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } // Include your component's compiled code with jar }
Build it with:
gradle :components:software_component_a:fatJar
The Jar will be in components/software_component_a/build/libs/software_component_a-2.0.0.jar.
4. Fix IntelliJ Execution Problems
- Refresh Gradle Configuration: In IntelliJ, open the Gradle panel (right sidebar), click the refresh icon ("Reload All Gradle Projects") to ensure the IDE picks up your updated settings.
- Run the Component Directly: Instead of running the root project, find your component's
runtask in the Gradle panel:gradle_root_project > components > software_component_a > Tasks > application > run. Right-click and select "Run"—this will execute your component with all dependencies loaded properly. - Validate Main Class: Confirm
io.cloudsystem.Controllerhas a validpublic static void main(String[] args)method—this is required for the application plugin to launch your code.
5. Clean Up Root Project Config
Move the test and JavaCompile settings into the subprojects block in your root build.gradle so all subprojects inherit them (right now they only apply to the root project):
// root build.gradle plugins { id 'java' id 'io.freefair.lombok' version '4.1.1' } subprojects { apply plugin: 'java' apply plugin: 'io.freefair.lombok' sourceCompatibility = '1.8' group = 'io.cloudsystem' version = '2.0.0' compileJava.options.encoding = 'UTF-8' compileTestJava.options.encoding = 'UTF-8' javadoc.options.encoding = 'UTF-8' repositories { mavenCentral() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2' } // Apply JUnit Platform to all subproject tests test { useJUnitPlatform() } // Enforce UTF-8 encoding for all subproject Java compiles tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } }
After making these changes, run gradle clean build to rebuild everything from scratch, then test the runnable Jar and IntelliJ execution again.
内容的提问来源于stack exchange,提问作者Felix Gaebler

