Skip to content

commons-email not migrated in Jakarta EE10 migration #695

@jepsar

Description

@jepsar

I ran

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
    -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:RELEASE \
    -Drewrite.activeRecipes=org.openrewrite.java.migrate.jakarta.JakartaEE10

and found that Apache commons-email library was not upgraded. This is an issue for example when you are using attachments with a jakarta.activation.DataSource.

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-email</artifactId>
      <version>1.5</version>
    </dependency>

should be replaced with:

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-email2-jakarta</artifactId>
      <version>2.0.0-M1</version>
    </dependency>

This is not a drop-in replacement unfortunately. I had to replace org.apache.commons.mail.EmailException with org.apache.commons.mail2.core.EmailException and
org.apache.commons.mail.HtmlEmail with org.apache.commons.mail2.jakarta.HtmlEmail. Probably there are more replacements needed.

Gemini gave me this list:

You're right to be thorough when migrating from Apache Commons Email 1.x to 2.x, especially the Jakarta EE-compatible version. While the core functionality remains similar, there are package changes and some API adjustments.

Here's a breakdown of the key changes and a guide to updating your code:

  1. Dependency Update

As discussed previously, ensure you've replaced your old commons-email dependency with:
XML

org.apache.commons commons-email2-jakarta 1.1 2. Package Changes

The most significant change is the package namespace. For the Jakarta EE version, all classes are now under org.apache.commons.mail2.jakarta.
3. Key Class Replacements

Here's a list of the most commonly used classes and their updated counterparts:

org.apache.commons.mail.Email -> org.apache.commons.mail2.jakarta.Email
org.apache.commons.mail.SimpleEmail -> org.apache.commons.mail2.jakarta.SimpleEmail
org.apache.commons.mail.MultiPartEmail -> org.apache.commons.mail2.jakarta.MultiPartEmail
org.apache.commons.mail.HtmlEmail -> org.apache.commons.mail2.jakarta.HtmlEmail
org.apache.commons.mail.EmailAttachment -> org.apache.commons.mail2.jakarta.EmailAttachment
org.apache.commons.mail.ImageHtmlEmail -> org.apache.commons.mail2.jakarta.ImageHtmlEmail
org.apache.commons.mail.resolver.DataSourceResolver -> org.apache.commons.mail2.jakarta.resolver.DataSourceResolver
org.apache.commons.mail.resolver.DataSourceUrlResolver -> org.apache.commons.mail2.jakarta.resolver.DataSourceUrlResolver
org.apache.commons.mail.resolver.FileDataSourceResolver -> org.apache.commons.mail2.jakarta.resolver.FileDataSourceResolver
org.apache.commons.mail.resolver.InternetDataSourceResolver -> org.apache.commons.mail2.jakarta.resolver.InternetDataSourceResolver
4. Find and Replace

The most efficient way to update your code is to use your IDE's "Find and Replace in Files" feature.
Search for org.apache.commons.mail. and replace it with org.apache.commons.mail2.jakarta..

And after complaining

You are absolutely correct, and I apologize for that oversight. The package changes in Commons Email 2.x go beyond just the root org.apache.commons.mail to org.apache.commons.mail2.jakarta or org.apache.commons.mail2.core.

You've highlighted a critical point: org.apache.commons.mail.EmailException is indeed moved to org.apache.commons.mail2.core.EmailException.

Revised Find and Replace Strategy

Given the more complex package structure in Commons Email 2.x, a more precise find-and-replace strategy is needed:

Jakarta-Specific Replacements:

Find: org.apache.commons.mail.
Replace: org.apache.commons.mail2.jakarta.
This will handle the core email classes.
Core Exception Replacement:

Find: org.apache.commons.mail.EmailException
Replace: org.apache.commons.mail2.core.EmailException
This will handle the exception class.
Resolver Replacements:

Find: org.apache.commons.mail.resolver.
Replace: org.apache.commons.mail2.jakarta.resolver.
Other Exceptions:

Carefully review any other exceptions that are used. There is a chance that other exceptions were also moved to the core package.

CC @melloware

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions