Migrating from Javax to Jakarta
A Practical Guide Using Transformer-Maven-Plugin

Search for a command to run...
A Practical Guide Using Transformer-Maven-Plugin

No comments yet. Be the first to comment.
In this series, I would like to talk about small things that I use in my daily tasks to make my work and the work of my coworker easier, faster and safer mostly by using our tools.
When I am coding, there is always a bunch of lines that I have to search about, or that takes time to type even though I know them, or code that needs to be included but I don't remember what, or code that I just copy/paste from a project without loo...
Why did you say yes? When I joined Sportradar, there wasn't much work for me yet. I was still discovering the company, the team and the product. One day, my team lead came to me. "We don't really hav
If you had asked me ten years ago what becoming a better engineer meant, my answer would have been surprisingly simple. Something like: Learn new frameworks. Write cleaner code. Discover better archi
A Practical Look at Specificity

Streamline Your Test Cases with the Magic of Parameterization

Transitioning from javax to jakarta namespaces is pivotal in the Java EE ecosystem, especially when maintaining or lacking control over javax libraries. The transformer-maven-plugin facilitates this migration. In this article, we'll generate a javax library from an XSD file, build a jar, and then create its jakarta version within a Maven project comprising two modules: one for javax, the other for jakarta.
In this article, we'll be utilizing Java and Apache Maven. It's assumed that you have basic familiarity with these technologies. Additionally, we will employ the transformer-maven-plugin to aid in the migration process. Now, with the basics covered, let's proceed to set up our Maven project for migrating a javax library to a jakarta one.
Create a New Maven Project
mvn archetype:generate -DgroupId=com.example -DartifactId=javax-to-jakarta -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Convert to a Multi-module Project
Edit the pom.xml in the javax-to-jakarta directory to include the pom packaging type and Java 8 source/target:
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
Create a Module for Javax
mvn archetype:generate -DgroupId=com.example -DartifactId=javax-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Generate Javax Library from XSD
Create a directory src/main/xsd within javax-module, and place the following person.xsd file in it:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person" type="PersonType"/>
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Update javax-module/pom.xml to include the jaxb2-maven-plugin and javax dependencies:
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Build and Package the Javax Library
mvn package
Create a Module for Jakarta
mvn archetype:generate -DgroupId=com.example -DartifactId=jakarta-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Update Parent POM
Add jakarta-module to the parent pom.xml:
<modules>
<module>javax-module</module>
<module>jakarta-module</module>
</modules>
Configure Transformer-Maven-Plugin in Jakarta Module
Navigate to the jakarta-module directory and edit its pom.xml to include the transformer-maven-plugin and specify the dependency on the javax module:
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<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>
<id>default-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<artifact>
<groupId>com.example</groupId>
<artifactId>javax-module</artifactId>
<version>${project.version}</version>
</artifact>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Execute the Migration
From the root project directory (javax-to-jakarta), execute the following command to perform the migration:
mvn clean package
In this article, we walked through a structured approach to migrate a javax library to a jakarta library using the transformer-maven-plugin within a Maven multi-module project. This methodology proves to be beneficial especially when there's a need to maintain a javax library or when control over the javax library is not available. Our setup delineated clear, easy-to-follow steps for migration, which is not only reproducible but also sets a foundation for testing and validation, ensuring the integrity of the libraries across the transition.
The transformer-maven-plugin offers a range of features beyond what was explored in this guide, making it a robust tool worth exploring for similar migration projects.
The complete code for the project discussed is available on GitHub, providing a practical reference to jump-start your migration endeavors from javax to jakarta.