简体   繁体   中英

Maven dependency provided on submodule ear throws ClassNotFoundException

Environment:

  • Java 11
  • JBoss 7.2
  • Maven 3.5

I am getting this ERROR java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException when I try to execute a method from a class in app-commons module. Jasper dependency in app-commons is compiled and app-commons dependency in app-back is provided.

How could I solve this?

Error

09:29:24,013 SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-1) net/sf/jasperreports/engine/JRException: java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRException
...
Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException from [Module "deployment.accfor2.ear" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:255)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    ... 78 more

Modules:

  • app (pom)
  • app-ear
  • app-back
    • app-commons (provided)
  • app-front
    • app-commons (provided)
  • app-commons
    • jasperreports (compiled)...

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>com.name.app</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>
    ...

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.");
        }
    }
...

pom.xml (app-back)

<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">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.name.app</groupId>
        <artifactId>app</artifactId>
        <version>8.0.0</version>
    </parent>

    <artifactId>app-back</artifactId>
    <packaging>war</packaging>
    <name>app-back</name>
    <description>Módul back (intranet)</description>

    <dependencies>
        ...
        <dependency>
            <groupId>com.name.app</groupId>
            <artifactId>app-commons</artifactId>
            <scope>provided</scope>
        </dependency>
     ...

ExecuteReportBean.java (app-back)

public class ExecuteReportBean.java
    ...
    public void compileReport(Informe informe, Map<String, Object> parametresSend) {

        ReportManager.compilaReport(parametre.getJasperReport());//ERROR java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRException
    ...

If you declare the dependency as provided , it will not be packaged into the WAR.

Either change the scope to compile or let the application server provide it (by eg putting it into a JBoss module).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM