mavenemailtomcatjakarta-eeapache-tomee

TomEE Plus 9.1 - Jakarta EE Platform Maven Project - How to exclude geronimo-mail jar


I have an Email class that uses the following imports:

import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
import jakarta.activation.FileDataSource;
import jakarta.faces.context.FacesContext;
import jakarta.mail.BodyPart;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;

This class is built and tested in a package in a different project that I use to build and maintain utility classes. The package is then exported in a jar for use in other projects. The project in which the exported package is used is a web project running in TomEE 9.1 Plus.

Both the project where the class is built and the TomEE web project where the class is used, use the same maven dependencies [below].

<dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>9.1.0</version>
</dependency>   

<dependency>
    <groupId>jakarta.mail</groupId>
    <artifactId>jakarta.mail-api</artifactId>
    <version>2.1.2</version>
</dependency>

<dependency>
    <groupId>org.eclipse.angus</groupId>
    <artifactId>angus-mail</artifactId>
    <version>2.0.1</version>
</dependency>   

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.2</version>
</dependency>

Unit tests of the Email class correctly perform constructing and sending MimeMessages with all options like attachments functioning as expected.

The problem is when the Email class is used in the web project. It is not a compile or runtime exception. The methods in the exported class are executed without error in the web project. However, when the methods are executed in the web project, TomEE points to a jar geronimo-mail_2.1_spec-1.0.0-M1.jar that contains implementations of the jakarta.mail.internet.* classes.

jakarta.mail.internet.MimeBodyPart.class from the geronimo jar, uses a DataHandler object that builds a MimeMessage in which the email body has no content. When the email is received, the body is blank.

Is there a way to prevent Tomcat from using the geronimo-mail jar? I've tried excluding the jar without success by doing the following:

<dependency>
    <groupId>jakarta.activation</groupId>
    <artifactId>jakarta.activation-api</artifactId>
    <version>2.1.2</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.geronimo.mail</groupId>
            <artifactId>geronimo-mail_2.1_mail</artifactId>             
        </exclusion>
        <exclusion>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-activation_2.0_spec</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.geronimo.mail</groupId>
            <artifactId>geronimo-mail_2.1_provider</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-mail_2.1_spec</artifactId>             
        </exclusion>
    </exclusions>
</dependency>

and including the jar as a dependency, then setting the scope as 'provided'.

<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-mail_2.1_spec</artifactId>
    <version>1.0.0-M1</version>
        <scope>provided</scope>
</dependency>

I also tried putting the jakarta and angus-mail jars directly in WEB-INF/lib folder of the web project.

I also tried using Eclipse's "Configure Build Path", browsing to the Tomcat v10.0 runtime, locating the geronimo-mail spec and provider jars, and entering exclusion patterns of **/*.class.

All with no success. The Email class in the TomEE project still uses classes from the geronimo-mail jar.

Any suggestion how to force Tomcat to open the correct jar containing the correct jakarta.mail implementations from the maven dependencies?

Thank you


Solution

  • Had the same issue on this one... We're using "SimpleJavaMail" which has an Email dependency that was overridden via geronimo at runtime (specifically when including INLINE embedded images).

    Simple solution was to just wack the jar(s) related to geronimo mail in TomEE lib directory. SimpleJavaMail will bring along Activation & Jakarta Mail implementations. *** Probably not the most elegant solution, but it's just email. https://www.simplejavamail.org/

    Digging into the overlap on implementation MimeMessage/DataHandler wasn't worth the time.