根据编译或提供的 Maven 依赖范围产生 Ambiguous Bean 或 ClassNotfoundException

Depending on maven dependency scope compiled or provided produces Ambiguous Bean or ClassNotfoundException

环境:

我有一个包含许多 maven 模块的项目和一个公共模块。取决于依赖编译的范围或提供我得到 WELD-001414: Bean name is ambiguous or java.lang.ClassNotFoundException.

这两个错误怎么解决一次?

当 pom.xml (app-back) 有依赖 app-commons

模块:

app-commons 有 ReportManager.java 需要 jasper 依赖和 Version.java @ApplicationScope

类型

pom.xml app-commons

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>es.caib.accfor</groupId>
        <artifactId>app</artifactId>
        <version>8.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-commons</artifactId>
    <packaging>jar</packaging>
    <name>app-commons</name>
    ...
    <dependencies>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.15.0</version>
        </dependency>
    </dependencies>
    ...

Version.java(应用程序共享)

...
@Named
@ApplicationScoped
public class Version {

    private String version;
    private String buildTime;
    private String scmRevision;
    private String jdkVersion;
    private String projectName;
...

ReportManager.java(应用程序共享)

public class ReportManager {

...
    public static void addParam(Map<String, Object> params, String nom, byte[] compiledReport) throws ReportException {
        try {
            params.put(nom, SerializationUtils.deserialize(compiledReport));
        } catch (Exception e) {
            throw new ReportException("Error deserialize compiledReport.");
        }
    }
...

app-back 模块中我调用 ReportManager.addParam()

SessionScoped.java(应用程序返回)

@Named
@SessionScoped
public class SessionBean implements Serializable {

    @Inject
    private Version version;
...

StartupListener.java (app-back)

@WebListener
public class StartupListener implements ServletContextListener {

    private static final Logger LOG = LoggerFactory.getLogger(StartupListener.class);

    @Inject
    private Version version;
...

因为提供依赖时不包括传递依赖,所以我不得不以这种方式重构模块。

模块:

  • 应用程序(pom)
  • app-ear
    • jasperreports(已编译)从 APP-COMMONS 移动
  • 应用程序返回
    • app-commons(已提供)
  • 应用前端
    • app-commons(已提供)
  • 应用程序共享 ...

我无法将 app-commons 依赖项更改为已编译,因为它依赖于 app-front 和 app-back。在编译后的应用程序共享中,我有一个@ApplicationScope class,由于 jakartaee 上下文管理,它会产生指向应用程序共享中的 class 的模棱两可的 Bean。