Using SMTPAppender in UberJar deployment is not possible

I have tried to use the logback SMTPAppender in a Cuba 7.2.7 project but are running into a problem - Apparently because javax.mail is not present in app.jar

logbacksmtp.zip (81.4 KB)

The app.log contains:

app.log (151.7 KB)

There is no problem when run from Studio

1 Like

Hi,

You need to specify dependencies used by logback on server level in the build file as described in the documentation (section configure):

For your case, configuration might look like this. Please note server section in the dependencies list:

configure([globalModule, coreModule, webModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('org.junit.jupiter:junit-jupiter-api:5.5.2')
        testCompile('org.junit.jupiter:junit-jupiter-engine:5.5.2')
        testCompile('org.junit.vintage:junit-vintage-engine:5.5.2')
        server('com.sun.mail:javax.mail:1.6.2')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
    test {
        useJUnitPlatform()
    }
}

Hope this will help.

1 Like

I actually already tried that, but it makes no difference

It seams that CUBA already has a dependency on ‘com.sun.mail:javax.mail:1.6.2’ as it is present in app.jar.

See also here from Intellij

image

Yes, but this dependency must be packed as the servlet container library, but not as the CUBA library. That’s why the server dependency type is used.

Did you try exactly the same configuration that I provided?

Yes - but to make sure I updated the project and attached below:

logbacksmtp.zip (81.6 KB)

Looking at the console log I get this:

12:13:27,231 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
12:13:27,235 |-ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [ch.qos.logback.classic.net.SMTPAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to i
nstantiate type ch.qos.logback.classic.net.SMTPAppender
        at ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
        at      at 

..
..
..
..
..
ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:69)
        at      at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:45)
        at      at com.haulmont.uberjar.CubaJettyServer.createServer(CubaJettyServer.java:143)
        at      at com.haulmont.uberjar.CubaJettyServer.start(CubaJettyServer.java:126)
        at      at com.haulmont.uberjar.ServerRunner.execute(ServerRunner.java:249)
        at      at com.haulmont.uberjar.ServerRunner.main(ServerRunner.java:39)
Caused by: java.lang.NoClassDefFoundError: javax/mail/internet/AddressException

Yes, your project is OK and works like a charm for me.

I created an uberJar and started it using java -jar app.jar.

Console log

13:28:05,801 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
13:28:05,811 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [emailAppender]
13:28:05,827 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:28:05,828 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [Console]
13:28:05,830 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:28:05,831 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [Console] to Logger[ROOT]
13:28:05,832 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [File] to Logger[ROOT]
13:28:05,832 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [emailAppender] to Logger[ROOT]

Please, open the packed uberJar and make sure that you have folder javax/mail in the root of the jar file like on the picture below:

image

Hi,

Thanks for helping.

After having cleaned the distribution folder and rerun the build, it also works here.

Br,
Torben

1 Like