Migrating from javax to jakarta namespace
Aug 15, 2023
Migrating from javax to jakarta namespace
In this video, we’ll look at how to migrate an application from using the javax namespace to the jakarta namespace. This change is expected to impact a lot of enterprise Java developers, especially as they’re moving their Java EE 8 or older APIs to use Jakarta EE 9 or later. 00:00 - Introduction 00:40 - The application 05:10 - Add jakarta dependencies 07:45 - Migrate packages/classes 11:43 - Migrate configuration files GitHub Repo: https://github.com/daliasheasha/MyWeb … The persistence.xml file is here: https://github.com/daliasheasha/MyWeb … Tomcat 10 Migration page: https://tomcat.apache.org/migration-1 … Background on the javax to jakarta namespace decision: https://eclipse-foundation.blog/2020/ … Download IntelliJ IDEA: https://jb.gg/download-intellij-idea Top 15 IntelliJ IDEA shortcuts: • Top 15 IntelliJ IDEA shortcuts Visit this web page for a more detailed list of the features for working with Jakarta EE:https://jb.gg/jakarta-ee *Author: Dalia Abo Sheasha Join us: Website https://jb.gg/website Blog https://jb.gg/blog Twitter https://twitter.com/intellijidea Facebook https://www.facebook.com/IntelliJIDEA/ #IntelliJIDEA #javax #jakarta #migration #refactor #Servlet #Persistence
Content
4.83 -> Hi, I'm Dalia.
6.37 -> And in this tutorial, I want to show you how
I migrated my application from using the javax
12.54 -> namespace to the jakarta namespace.
16.09 -> This is a change that will impact a lot of
enterprise Java developers, especially as
20.57 -> they're moving their Java EE APIs to use Jakarta
EE 9 or later.
26.359 -> I actually stumbled upon the fact that I need
to do this when I was doing a separate upgrade
33.47 -> and I didn't realize that I needed to switch
my namespaces.
36.6 -> So I want to show you what happened and then
we can dive into how to do it.
41.019 -> So the first thing I want to show you is my
application.
43.98 -> So I have a really simple app and all it does
is take in some data and, let's see...
53.69 -> Then you submit the data.
55.98 -> Saves it in a database and it tells you it
got saved if the data got saved successfully.
63.11 -> So very simple application.
65.129 -> Now this application, under the covers, is
using Tomcat and I'm specifically using Tomcat
71.81 -> 9.
73.01 -> So I thought there's a later version of Tomcat,
which is Tomcat 10 that came out.
77.6 -> Why don't I upgrade my application to use
Tomcat 10?
80.43 -> It should be pretty straightforward, especially
that this is a really simple application.
87.1 -> So I went ahead, went back to my app.
90.39 -> I happened to be using Docker.
93.43 -> If you're not familiar with Docker, this is
the equivalent of going off downloading Tomcat
97.88 -> 10 and redeploying your application.
100.6 -> Because I'm using Docker, it's a lot simpler.
103.91 -> And it's just replacing this.
106.07 -> So I'm going to replace Tomcat 9.
109.96 -> To Tomcat 10 and I'm using JDK 17.
113.58 -> So that's it.
115.2 -> This is all I need to do.
118.62 -> And that's partially why I'm using Docker
because it makes it really simple to upgrade
121.81 -> my app servers and dependencies.
126.08 -> So I went ahead.
128 -> I replaced my Tomcat 9 with Tomcat 10.
132.99 -> So I'm going to go ahead and run Docker compose
here, because that's what I'm using to deploy
137.73 -> my two services.
140.12 -> I'm going to use the one that rebuilds my
image because I just replaced my Tomcat 9
145.53 -> with 10.
146.53 -> So I need to be rebuilding my image.
149.2 -> I see that my images got rebuilt and now my
application is running.
155.75 -> Once I do that, I like to look up the logs
just to make sure that everything started
160.13 -> fine in the container itself.
162.73 -> And take a look at some of the logs here.
166.64 -> So let's take a look at, let's see.
169.24 -> So server version number is 10.
171.49 -> That means that I am now using Tomcat 10.
174.44 -> Great!
175.44 -> So I'm going to bring my application back
up and I'm going to go back and I'm going
182.4 -> to try to enter in some other data.
192.97 -> And I get an error.
194.06 -> This is what happened last time I tried this
and I looked at it and I had no idea what
200.49 -> was going on because I was thinking this is
a really simple application.
204.97 -> Why am I getting an error?
207.61 -> And the error is vague, so I really didn't
know what to do.
212.209 -> So novel idea!
213.489 -> I decided to actually look at the migration
docs.
215.8 -> I went off read the migration docs and it
turns out starting Tomcat 10, they switch
222.209 -> from the Java EE Servlet APIs to Jakarta Servlet.
228.41 -> So it turns out they actually need to change
my application from using the javax namespace
235.349 -> to the jakarta namespace.
238 -> I do want to pause here for a second and do
mention that if you don't want to change your
243.849 -> actual source code, there's two tools that
change your binaries for you.
251.12 -> One is called the Eclipse Transformer, the
other is the Tomcat Migration Tool.
256.38 -> And what those tools are going to do is they're
going to take your binaries and they're going
261.509 -> to change the byte code so that it uses the
Jakarta packages instead of the javax.
270.539 -> Now that's useful if you're not really wanting
to change your source code or maybe your app
276.8 -> is not being actively developed.
278.86 -> And you don't anticipate that you're going
to use any of the new features that is going
282.909 -> to come out in Jakarta.
284.889 -> But in this case, I am actively using this
application and I actively want to develop
290.919 -> it and use a new Jakarta EE APIs when they
come out.
296.139 -> So I will be migrating my namespace from javax
to Jakarta.
302.819 -> So let's go back to our application code.
307.289 -> And I'll just collapse this view.
309.86 -> The first thing that you'll want to do is
go to your pom.xml file, or if you're using
314.869 -> gradle you'll go to your build.gradle file.
319.139 -> And what we're going to do in this file is
that we're going to bring in the Jakarta dependencies.
324.729 -> I can see that I obviously have some javax
dependencies here, but something I like to
331.58 -> do is I like to take advantage of the Maven
window view and expand my Dependencies node.
340.999 -> So you can do that using the "*" on your NumPad,
and then you can see all the different dependencies
348.77 -> and the way that I like to use this is I like
to search for javax and see which of my dependencies
356.189 -> are bringing on javax packages.
358.55 -> Because even though the javax servlets API
is pretty obvious.
363.21 -> Hibernate is actually also bringing in some
of the javax.persistence packages.
368.21 -> So I will need to also switch to a hibernate
version that uses jakarta.
374.449 -> So let's do that.
375.779 -> In my pom.xml file, I'll go ahead and press
Alt + Insert.
382.27 -> Click Dependency.
383.3 -> L ook for jakarta.servlet.
386.449 -> And I see the servlet-api dependency option.
392.12 -> So I'll click that.
394.139 -> And that inserts it in.
395.83 -> Eventually we will be removing the javax.servlet-api,
but I'm going to wait to do that in an incremental
401.86 -> fashion.
403.18 -> The next thing I'm going to do is I'm going
to import the Hibernate version that brings
410.389 -> in the Jakarta package.
412.12 -> And right now it's in beta, but that's all
right.
415.15 -> I'm going to bring it in, mainly because even
though technically speaking, you don't have
421.289 -> to upgrade your Hibernate.
424.379 -> Eventually you'll probably want to, because
you don't want to be mixing and matching the
429.339 -> javax and the jakarta namespace, because it's
going to introduce some weird behavior in
433.52 -> your application, which is why I'm choosing
to do it right now, because I'm eventually
437.58 -> going to do it anyways.
439.379 -> So I'm going to upgrade my hibernate to the
6.0.0.B eta.
445.929 -> And add that in here.
447.789 -> And that's going to bring in the new Jakarta
APIs.
450.36 -> Of course, if I click that add button.
455.71 -> Awesome!
457.229 -> So that's what you need to do in your pom.xml
file or your build.gradle file.
462.61 -> Now that I have my Jakarta packages, I can
start migrating my application from using
469.099 -> the javax namespace to jakarta.
472.649 -> And for that, I can actually use a really
useful option that's available in IntelliJ
477.159 -> IDEA.
478.209 -> If you go to Refactor and then go to Migrate
Packages and Classes, you'll see this option:
485.309 -> Java EE to Jakarta E E. Once you click that,
you'll see a new window that lets you replace
494.759 -> all of the javax and jakarta packages in your
projects using this Package and Class Migration
503.289 -> window.
504.289 -> So what this is going to do is it's going
to look for all your javax imports and replace
508.969 -> them with jakarta imports.
511.369 -> One question that comes up a lot is: why don't
I just do a global find and replace in all
516.599 -> my projects?
517.88 -> Wouldn't that be much simpler?
520.38 -> The problem with that is that not all javax
packages were migrated to use jakarta.
527.28 -> For example, the javax.transaction.xa package
is not going to be using Jakarta.
532.51 -> So if you go ahead and do a global find and
replace, that's going to be a problem.
538.73 -> So go ahead and use this window for this purpose.
543.64 -> And the first thing that you want to select
is what scope you want the find and replace
548.38 -> or the refactor to happen in.
549.961 -> And for me, I just have one module.
552 -> So I'll just select that module.
553.91 -> If you have multiple ones, you can select
which module you want to do this on.
558.66 -> And before I hit run here, I do want to mention
this Duplicate and edit...
564.87 -> Option because I've used it just out of curiosity.
567.83 -> I was wondering what kind of packages are
going to be replaced when I run this migration.
575.03 -> So if you click this, you'll see all the different
packages that are being looked for from IntelliJ
582.39 -> IDEA and being replaced.
585.23 -> The other reason you might want to look at
this option is if you have your own application
592.51 -> and, for example, you decided not to migrate
Hibernate to use jakarta instead of javax
598.47 -> persistence, you could create your own migration
map and remove javax.persistence from your
605.34 -> migration map here.
607.42 -> And once you remove it and run the rule, it
won't look for javax.persistence and replace
612.33 -> it with jakarta.persistence.
614.34 -> So just FYI, in case you would use this option.
618.24 -> I'll go ahead and cancel out of that because
I'm just using the default migration map.
623.43 -> And I'm going to go ahead and run my refactor.
629.59 -> So what's going to happen here: it's going
to scan all my source code and find any places
635.61 -> that I'm using the javax packages that need
to be a refactored to jakarta.
641.33 -> And I see that it's pointingout javax.
644.11 -> persistence.
645.11 -> I'm using javax.
646.79 -> servlet.
647.79 -> I'm using it in a couple of classes.
650.05 -> So it also points it out in those classes.
653.38 -> So you can run that and see what kind of packages
that you're using and the packages that are
658.99 -> going to be refactored.
661.45 -> So I've reviewed this.
662.59 -> This looks good.
663.59 -> I want to go ahead and do the refactor and
replace javax to jakarta.
667.54 -> So I'll click Do Refactor.
671.4 -> And that's going to do the refactor for me.
675.01 -> So if I open up one of the classes that I
have here, like the Person class, for example,
682.13 -> I see that it's using jakarta.persistence
instead of javax.persistence.
687.22 -> All right.
688.55 -> Awesome!
689.55 -> So I see that the refactor happened just fine.
693.13 -> And I think I don't have any problems.
695.01 -> Let's just open the Problem view.
696.75 -> I don't see any project problems or errors.
700.65 -> So we should be good to go.
701.92 -> The other thing I do want to mention is that
this refactor will work if I run it right
702.92 -> now, but I do want to point out that there
are certain files that you might need to visit
708.81 -> when you're doing this migration.
711.31 -> So for example, I do like to run find on my
files, not my classes.
718.23 -> But my actual files.
719.85 -> So I can see who's using javax in all of my
different files.
726.76 -> So for example, I see here that the pom.xml
file is still using javax.
731.8 -> So I'm going to go in here, remove those dependencies
because we no longer need them.
736.04 -> We have the Jakarta dependencies.
737.7 -> Now I don't need this one either.
740.14 -> So I'm going to remove that as well.
743.69 -> And we're going to go ahead and reload.
746.42 -> The other place that it was finding the javax
package is my persistence.xml file.
753.04 -> So actually right now, if I run my application,
it's going to work and I did go ahead and
759.11 -> I have it this way for a while.
763.32 -> But then I was thinking eventually, I'm also
going to need to upgrade my properties.
771.2 -> Even though right now, hibernate is being
nice and accepting my old properties.
774.84 -> I will have to do it eventually.
777.1 -> So I'm going to show you how to replace the
Jakarta properties in your persistence.xml
783.37 -> files.
784.37 -> First thing that you're going to need to do
is replace your schema here.
787.75 -> I went ahead and - let's open up our JPA spec.
793.27 -> I don't know if you've ever had a chance to
take a look at some of the Java EE or Jakarta
799.9 -> EE specs, but I'm very familiar with them
from my time as a JPA committer.
805.63 -> So I just opened up the spec and copied the
schema from here and I pasted it up here.
814.61 -> And I will be pasting in my GitHub repo link
so that you can copy-paste this and you don't
821.74 -> have to worry about writing everything out,
but basically it's upgrading your schema from
828.61 -> using the Java EE schema to the Jakarta EE
schema.
834.54 -> And now that you have the new schema, what
you need to do is you need to replace all
839.84 -> your javax properties to the jakarta properties.
843.1 -> So I'm going to do Ctrl + Shift + R and I'm
going to replace all my "javax."
851.37 -> To - oh, I did this before.
853.24 -> If you can see - but you want to type in "jakarta."
858.029 -> for all your "javax."
859.98 -> And we're going to replace all these packages.
862.22 -> It's asking me: do you want to replace all
these property names?
866.12 -> Yes, I do.
868.66 -> And that's all you need to do if you want
to upgrade your properties to use jakarta
874.221 -> as well.
875.3 -> All right, so now we're good to go.
878.12 -> So what I'm gonna do is I'm gonna run mvn
clean install.
885.24 -> And see if my application builds successfully.
889.17 -> Hopefully I did this correctly.
891.18 -> Awesome!
892.47 -> So it did build the application, looks like
it was built successfully.
898.58 -> And now let's go ahead and rerun our Docker
Compose up command.
906.26 -> So I'm going to go ahead and just use my Shift+F10
keys that way it's gonna rerun and actually
916.279 -> it's going to rebuild my image with the new
package names and it's going to redeploy my
923.22 -> application with the jakarta packages with
Tomcat 10.
928.32 -> So let's go ahead and take a look at the app
and see if it's working now.
933.05 -> All right, let's go back.
936.41 -> I'm going to try this again.
940.71 -> Actually let's refresh our app.
951.46 -> And now it works!
953.38 -> So what we did here was we migrated our application
from using javax to Jakarta.
959.91 -> We replaced all the class files.
962.46 -> We replaced the property files as well.
966.4 -> And now our application works.
968.41 -> I hope this was helpful and hopefully this
helps you when you're upgrading your application
973.029 -> from using the javax namespace to jakarta.
975.99 -> Thanks for watching!
Source: https://www.youtube.com/watch?v=mukr2Q_zBm4