[payara-micro] --addLibs with CDI ebabled jar file give the unsatisfied dependencies exception.

114 views
Skip to first unread message

Charlee Chitsuk

unread,
Mar 7, 2019, 10:21:07 PM3/7/19
to Payara Forum
I'm trying to reduces the size of the war file by moving all jar files from WEB-INF/lib to other directory, e.g. /opt/lib. Then passing this  directory via --addLibs during starting the payara micro as the following example: -

My environment is

My environment is as the following:-

From Docker `payara/micro:latest`

-  Payara Version:  Payara Micro  5.184 #badassmicrofish (build 89)
- Edition: Micro
- JDK Version:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (IcedTea 3.9.0) (Alpine 8.181.13-r0)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

- Operating System:

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.8.1
PRETTY_NAME="Alpine Linux v3.8"


The Dockerfile is

FROM payara/micro:latest
ARG WAR_FILE
COPY target/lib /opt/lib
COPY target/${WAR_FILE} $DEPLOY_DIR

The docker-compose.yml is

  service-a:
    image: my/service-a:1.0.0-SNAPSHOT
    restart: always
    entrypoint:
      - java
      - -jar
      - /opt/payara/payara-micro.jar
      - --deploymentDir
      - /opt/payara/deployments
      - --addLibs 
      - /opt/lib/

The multi module structure is

my-project
+-- util
|   +-- some-classes
|   |
|   `-- META-INF
|       `--beans.xml
`-- service-a
    `--WEB-INF
       +-- classes
       |   `-- some-classes
       +--lib
       |  `--util.jar
       `-- beans.xml

the beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                           http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0" bean-discovery-mode="all">

</beans>


The example for util.jar

@ApplicationScoped
public class MyUtil {
    public String say(final String name) {
        return "Hello " + name;
    }
}

The example for service-a.war

@Path("myResource")
@ApplicationScoped
public class MyResource {
    @Inject
    private MyUtil myUtil;

    @GET
    @Path("/{name}")
    public Response hello(
@PathParam("name")
                                                   final String name) {
        String message = this.myUtil.say(name);
        return Response.ok(message).build();
    }
}


If the util.jar is in the WEB-INF/lib, everything works fine. Anyhow when the util.jar is outside and is added by --addLibs, the unsatisfied dependencies exception is thrown at the MyResource.myutil during starting up the paraya micro.

I'm not sure if I'm doing something wrong or not. Could you please help to advise?


Ondro Mihályi

unread,
Mar 14, 2019, 4:44:48 AM3/14/19
to Payara Forum
Hi,

It seems that you're doing everything correctly, however, what you want to do isn't currently supported by Payara Micro. Your setup doesn't work because Payara Micro can't inject CDI beans from libraries added with --addlibs.

The class MyUtil is on the classpath but it's not treated as a CDI bean. Therefore injection fails. You can use the MyUtil class without the injection or create a producer to inject it, like this:

@ApplicationScoped
public class MyUtilProducer {
//    @Produces
    MyUtil getUtil() {
        return new MyUtil();
    }
}

However, with this approach, you can't use injection in the MyUtil class itself. If you would like that Payara Micro also scans libraries added with --addLibs for CDI beans, please raise a github issue at https://github.com/payara/Payara/issues/. Also include a ZIP file with the project to make it easy to reproduce.


All the best,
Ondro
Reply all
Reply to author
Forward
0 new messages