When building a multi-module Maven project, everything works fine for the first two modules, but compilation fails for module 3 with errors like:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \Module3\src\com\company\sharedartifact\package\SomeClass.java:[8,55] package com.company.sharedartifact.package does not exist
[ERROR] \Module3\src\com\company\sharedartifact\package\SomeClass.java:[9,55] package com.company.sharedartifact.package does not exist
[ERROR] \Module3\src\com\company\sharedartifact\package\SomeClass.java:[17,85] cannot find symbol
symbol : class SomeOtherClass
This issue arises despite the required JAR being listed as a dependency in both module 2 and module 3. Attempts to fix it by:
- Adding the dependency explicitly in module 3
- Relying on transitive dependencies
- Refreshing and re-downloading dependencies
…have not resolved the issue.
Why This Happens
This type of error often stems from:
- Incorrect Transitive Dependency Resolution: Maven does not always correctly propagate dependencies between modules.
- Classpath Issues in IDEs: Tools like Eclipse can sometimes misconfigure the build classpath for multi-module projects.
- Build Order Problems: Modules may not compile in the correct order, leading to missing artifacts during the build process.
Solution
Here’s a step-by-step approach to fix the issue:
1. Verify Dependency Declaration
Ensure the dependencies are correctly defined in the POM files. For module 3, explicitly declare the required dependencies:
Module 3 POM:
<dependency>
<groupId>com.company</groupId>
<artifactId>Module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>SharedArtifact</artifactId>
<version>1.1</version>
</dependency>
2. Check the Parent POM
Ensure the parent POM defines all modules and dependencies properly:
<modules>
<module>module-1</module>
<module>module-2</module>
<module>module-3</module>
</modules>
3. Build Modules in the Correct Order
Run the following command to build the modules step-by-step:
mvn clean install -pl module-1,module-2,module-3
This ensures each module is compiled and installed sequentially.
4. Use Dependency Plugin to Analyze Issues
Run the Maven Dependency plugin to inspect your dependency tree:
mvn dependency:tree
This helps identify conflicting or missing dependencies.
5. Configure Eclipse Properly
If you’re using Eclipse, perform these steps:
- Right-click the project > Maven > Update Project.
- Check “Force Update of Snapshots/Releases.”
- Ensure
M2_REPO
is set up correctly in your Eclipse classpath.
6. Clean and Rebuild
Sometimes, stale artifacts can cause issues. Try the following:
mvn clean install -U
The -U
flag forces Maven to update snapshots and releases.
Call to Action
Facing similar Maven issues? Share your experience or ask questions in the comments below! If this guide helped you, consider:
- Sharing this post with your colleagues.
- Subscribing to our newsletter for more developer tips and tricks.
- Contacting us for one-on-one Maven troubleshooting support.
Together, we can resolve your build issues and get your project back on track!