Issue
Environment:
- Java 11
- JBoss 7.2
- Maven 3.5
I have a project with many maven modules and a common for all. Depend on the scope of dependency compile or provided I am getting WELD-001414: Bean name is ambiguous or java.lang.ClassNotFoundException.
How to solve these two errors to once?
When pom.xml (app-back) has dependency app-commons
When app-commons is scope provided
ERROR java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException
When app-commons is scope compiled
ERROR WELD-001414: Bean name is ambiguous. Name version resolves to beans: [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 [@Default @Any @Named]]"}}
Modules:
- app (pom)
- app-ear
- app-back (dependency to app-commons)
- app-front (dependency to app-commons)
- app-commons (jasperreport dependency)
- ...
app-commons have ReportManager.java that needs jasper dependency and Version.java of type @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 (app-commons)
...
@Named
@ApplicationScoped
public class Version {
private String version;
private String buildTime;
private String scmRevision;
private String jdkVersion;
private String projectName;
...
ReportManager.java (app-commons)
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.");
}
}
...
In app-back module I call ReportManager.addParam()
SessionScoped.java (app-back)
@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;
...
Solution
Because of transitive dependencies are not included when dependency is provided, so I had to restructure the modules this way.
Modules:
- app (pom)
- app-ear
- jasperreports (compiled) MOVED FROM APP-COMMONS
- app-back
- app-commons (provided)
- app-front
- app-commons (provided)
- app-commons ...
I was not able to change app-commons dependency to compiled because it is dependent of app-front and app-back. In app-commons as compiled I have an @ApplicationScope class that it would produce ambiguous Beans pointing to the class in app-commons because of jakartaee context management.
Answered By - Joe
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)