You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven编译失败排查:拆分Spring Boot公共代码后找不到类

Troubleshooting "Cannot Find Symbol" Error When Using a Local Maven Common Library

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 ExpressionToken class in the common project is in the package com.beastcode.devops.prometheus.types (no hyphen in "beastcode").
  • The import statement in your original project's ReferenceTreeAggregation.java matches 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 11:06:01