根据编译或提供的 Maven 依赖范围产生 Ambiguous Bean 或 ClassNotfoundException
Depending on maven dependency scope compiled or provided produces Ambiguous Bean or ClassNotfoundException
环境:
- Java 11
- JBoss 7.2
- 行家 3.5
我有一个包含许多 maven 模块的项目和一个公共模块。取决于依赖编译的范围或提供我得到 WELD-001414: Bean name is ambiguous or java.lang.ClassNotFoundException.
这两个错误怎么解决一次?
当 pom.xml (app-back) 有依赖 app-commons
当提供应用程序共享范围时
错误 java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException
当 app-commons 被范围编译时
错误 WELD-001414:Bean 名称不明确。名称版本解析为 bean:[Managed Bean [class es.caib.app.commons.utils.Version] with qualifiers [@Default @Any @Named],Managed Bean [class es.caib.app.commons.utils.Version] with qualifiers [@默认@Any @Named]]"}}
模块:
- 应用程序(pom)
- app-ear
- app-back(依赖于 app-commons)
- app-front(依赖于 app-commons)
- app-commons(jasperreport 依赖项)
- ...
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。
环境:
- Java 11
- JBoss 7.2
- 行家 3.5
我有一个包含许多 maven 模块的项目和一个公共模块。取决于依赖编译的范围或提供我得到 WELD-001414: Bean name is ambiguous or java.lang.ClassNotFoundException.
这两个错误怎么解决一次?
当 pom.xml (app-back) 有依赖 app-commons
当提供应用程序共享范围时
错误 java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException
当 app-commons 被范围编译时
错误 WELD-001414:Bean 名称不明确。名称版本解析为 bean:[Managed Bean [class es.caib.app.commons.utils.Version] with qualifiers [@Default @Any @Named],Managed Bean [class es.caib.app.commons.utils.Version] with qualifiers [@默认@Any @Named]]"}}
模块:
- 应用程序(pom)
- app-ear
- app-back(依赖于 app-commons)
- app-front(依赖于 app-commons)
- app-commons(jasperreport 依赖项)
- ...
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。