Maven编译失败排查:拆分Spring Boot公共代码后找不到类
Here are the key steps to diagnose and fix your issue:
1. Confirm the Missing Class Exists in the Common Jar
First, verify that the ExpressionToken class is actually present in the installed common jar. Run this command in your terminal:
jar tf ~/.m2/repository/com/beast-code/devops/prometheus-common/1.0.0/prometheus-common-1.0.0.jar | grep ExpressionToken
If this returns no results, the class wasn't packaged into the jar. You'll need to fix the common project's build configuration.
2. Fix the Common Project's Maven Packaging
Your common project uses the spring-boot-maven-plugin, which is designed for building executable Spring Boot applications—not reusable libraries. Even with layout=NONE, it can alter the jar structure in unexpected ways. Replace it with the standard maven-jar-plugin:
In your common project's pom.xml, update the build section:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </build>
Then re-run mvn clean install on the common project to rebuild and reinstall the jar.
3. Double-Check Package Name Consistency
Java package names cannot contain hyphens, so ensure:
- The
ExpressionTokenclass in the common project is in the packagecom.beastcode.devops.prometheus.types(no hyphen in "beastcode"). - The import statement in your original project's
ReferenceTreeAggregation.javamatches exactly:import com.beastcode.devops.prometheus.types.ExpressionToken;
A mismatch here (even a single character) will cause the "cannot find symbol" error.
4. Force a Clean Rebuild of All Projects
- On the common project:
mvn clean install - On the original project, force Maven to update dependencies and rebuild:
mvn clean compile -U
The -U flag tells Maven to check for updated snapshots/releases, ensuring it picks up the latest version of your common jar.
5. Clear IDE Caches (If Using an IDE)
IDEs like IntelliJ or Eclipse often cache Maven dependencies and compiled classes. Invalidate caches and restart your IDE to ensure it loads the latest version of the common jar:
- IntelliJ:
File > Invalidate Caches... > Invalidate and Restart - Eclipse:
Project > Clean... > Clean all projects
6. Verify Dependency Scope
Ensure the common dependency in your original project is set to compile scope (the default if not specified). Your current pom.xml looks correct here, but double-check that no accidental scope tag was added (like provided or test).
内容的提问来源于stack exchange,提问作者Joseph Gagnon

