Reputation: 38787
Is it at all possible to use older Java EE libraries (with javax
package) in new Jakarta EE applications (with jakarta
package)?
All the APIs would be backwards compatible if it weren't for the trademark issue. Is there any runtime library or build tool available that can get them to work together? Is the only solution to fork old libraries and updated them manually?
I'm in particular looking at the Servlet API, but validation, persistence, webservices and mail may bite me too.
Upvotes: 15
Views: 12652
Reputation: 38787
Based on this answer, this is the sort of Maven config I ended up using:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.transformed</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.5.2.RELEASE</version>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.transformer</groupId>
<artifactId>transformer-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<rules><jakartaDefaults>true</jakartaDefaults></rules>
</configuration>
<executions>
<execution>
<phase>prepare-package</phase>
<goals><goal>jar</goal></goals>
<configuration>
<artifact>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>${project.artifactId}</artifactId>
</artifact>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<artifactSet>
<includes>
<include>*:${project.artifactId}</include>
</includes>
</artifactSet>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<shadedArtifactAttached>false</shadedArtifactAttached>
<useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>
</configuration>
<executions>
<execution>
<goals><goal>shade</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${spring-security-oauth.version}</version>
</dependency>
</dependencies>
</project>
Upvotes: 0
Reputation: 38163
You can use the Eclipse Transformer project to transform your older Java EE compatible jar with javax.*
imports to a jar using jakarta.*
imports.
The transformer takes care of some additional things like renaming constants in configuration files and such. It's a work in progress, but does the job quite well already.
To quote the documentation:
Eclipse Transformer provides tools and runtime components that transform Java binaries, such as individual class files and complete JARs and WARs, mapping changes to Java packages, type names, and related resource names.
While the initial impetus for the project was the Jakarta EE package renaming issue, the scope of the project is broader to support other renaming scenarios. For example, shading.
Upvotes: 11
Reputation: 24590
Like you mention, the APIs are compatible except for the trademark changes, so one solution is to build wrappers around them that delegate from the new library to the old. Might not always be straightforward, but depending on your use case, it could work.
public class LegacyServlet implements jakarta.servlet.Servlet {
private final javax.servlet.Servlet delegate;
public LegacyServlet(javax.servlet.Servlet delegate) {
this.delegate = delegate;
}
@Override
public void service(jakarta.servlet.ServletRequest req, jakarta.servlet.ServletResponse resp) {
delegate.service(new LegacyServletRequest(req), new LegacyServletResponse(resp));
}
}
Upvotes: 1