Upgrading your Java & Spring Boot applications with OpenRewrite in IntelliJ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what if you need to make some changes to your application you could go the manual route you could make these changes yourself you could use the ID to refactor some things but this is a very manual process is this going to scale across two five 10 100 applications probably not now you could automate this somehow you can kind of hack together your own scripts and write these scripts to automate this process and change all the things across your applications but we're not going to do that we are going to use a project called open rewrite and that's what we're going to look at today open rewrite includes these scripts if you will we're going to call them recipes and these recipes are a way to go ahead and make a particular change across your application once you have all this figured out in one app you kind of script this for the rest of your app using this collection of recipes that's because these recipes can be chained together if you will so we're going to look at a couple different examples today we'll talk about a simple example in Java but I also really want to take a look at this for spring if you are on something like spring boot 2.7 and you want to get to 3.2 how do we do it well we can move from one recipe to like 3.0 and then to 3.1 and then to 3.2 we can automate this process we can do it in one of our apps and then when we get that kind of Secret Sauce figured out we can go ahead and push this to other apps and and really make make some Headway on upgrading to the latest and greatest so here I am at the open rewrite docs. openen rite. there's a nice introduction there's a nice video there is a way to get started so if I go into runin recipes and I look at the quick start here are some prerequisites you can clone a project and test it out here um you can add a rewrite Maven Plugin or rewrite greata plugin to your project this will allow you to basically type in a MAV command and run a particular recipe that's fine and dandy that works that's going to be needed like when we get into like uh trying to automate this across many many repositories but there's also a way to basically um do this from the IDE now so here's some getting started from this what I'm going to show you today is we're going to use intellig we're going to add some recipes into a directory and then I'll show you how you can run these right from the ID this is really cool I think this gives you that visual of just being able to see it uh in your ID so I really like this approach and I think you will too with that let's head over to intellig idea and talk through some of these recipes all right so here I am in my application I will put all the source code for this example into a repository and you can find that below I have this silly this is a spring boot app let's start there actually um so I have a spring boot app this is written in using 2.7x we are using Java 8 and we just have things like the spring boot starter web spring boot starter validation not a whole lot going on here but if you're just a Java developer and you don't use spring this will be helpful too we're going to start with more of a simple Java example so I have this silly string calculator and this has some methods for adding and subtracting Based On A String so you could pass in a string something that looks like this you could say add three and or I'm sorry with a string that is like three and one and it will split that and use the three and the one turn them into numbers and add them together so I have this silly class and I want to be able to write some tests for this so I'm going to go ahead and create a test we'll call this the string calculator test and in here I will say let's go ahead and write a test um this should sorry should add two string numbers right um that looks pretty good uh yeah so all we're saying here and I want to do this because we're going to run a recipe against that so I want to assert that um 3 and 1 equals 4 and uh it does and then I'll just add one more so again I want to do um assertions dot assert equals and that should run and that will go ahead and pass as well okay so now I have two tests this first recipe is going to be extremely simple but hey we got to walk before we can run right so what I'm going to do is uh I'm going to create a new uh directory here in my resources I'm going to call this recipes now I will point out that I am using the latest beta of intellig this is 20241 beta there is a plugin for open rewrite if you are before this version you have to manually install this plugin yourself if you are in the beta or further this is actually bundled in intellig now which I find really exciting so the plugin is what is going to make this happen but now we can go back to the open rewrite site we can go ahead and um check out the recipe C catalog so there are a bunch of recipes in here so I want to go ahead and pick Java all right so here are a bunch of java recipes that you can look for you can also do a search from here I know that the one I'm looking for is called use static import and then this is for the junit Jupiter assertions so you can go to this um page and kind of find out more about the recipe it says always use static Imports for assertion methods and that's what we've done remember we've we're using the fully qualified class and Method name we want to remove that and use the static import so here's the recipe Source if you want to like see how this was written to make this work here's the usage so if you're using Gradle or Maven uh you can include this in your palm and then you can just run a maven command to run it there's also a definition so I will say not all recipes have a definition but um there easy to kind of construct they're just yl um so here's what that looks like so what I would do is copy this and then I'm going to go back to um my IDE and under recipes in resources I'm going to create a new file I'm going to say use static uh import. yaml and inside of there I am going to uh pop paste this code in so now I am using this name as the recipe name I don't think this matters you can probably name it whatever you want I like to keep these the same so now that this is in place what I would probably do in the real world so again this isn't the real world this is the demo world where I don't have to do that I would go in and create a new branch and this would be like a rewrite feature Branch right like I'm trying to uh update all the static Imports I'm just going to do this in the main branch um but that's okay so we can run this we can also run right click on this and modify our options so if you wanted to do something like just do a dry run at first um you could do that um or you can um like you could do a whole bunch of different things I don't think this is I don't know why our options aren't there is it oh yeah yeah open reate version exclusion active dry run okay so yeah so we could do all of those different things from there but I just want to run this recipe and ultimately what this should do is in my test it should remove this assertions Dot and use the static import so let's go ahead and run this and see if that works all right so we have a success you can see changes have been made to string calculator test and here are the changes now if you do a dry run it will basically stick a patch file in the Target open rewrite folder but we've actually said just go ahead and make those changes for us so now if we look at this we can see the changes to the string calculator test um it went ahead and oh this is probably from me doing that before let's go ahead and open this up now you can see that the assertions dot is gone and we've added uh a static import for that so recipe number one has been done so that's pretty cool so now what we can do is I'm going to um copy and paste a few recipes in here and uh we'll talk about them in a second all right so now we have these three new recipes in here and these are for upgrading spring boot I got these right off the open rewrite website um and these you can go ahead and copy from there as well you can see these are a little bit more complicated those there's some more things going on you see a recipe list so we can start to chain together recipes um and now you'll notice I have 3 0 31 and 32 if you are upgrading from something like spring boot 2.7 it is advised to make the jump to 3 make the jump to Java 17 if you haven't yet then go ahead and make the jump to 31 and then 32 going from 27 to 32 might be a very big jump and if you're application has a bunch of things going on you'll probably want to take this one step at a time so you'll see that it has multiple recipes hey upgrading to Spring boot 2.7 making sure it's on the latest there um renew enable uh remove enable batch processing upgrade to Java 17 so that's a requirement so it's going to make you upgrade to Java 17 um it has some things around time leaf and all the things that you might have to consider in moving one of those is like hey properties change between 2.7 and 3 how can I upgrade those spring Cloud Spring Security there are a bunch of changes in Spring Security 6 so as you can see just this one recipe to upgrade to 3.0 is pretty involved now I have probably the most basic application so not a lot is going to change but that's okay we're just taking a look at how to run these recipes and how to add them to our project all right so I'm going to go ahead and run the um upgrade here uh to 3.0 and we'll see what happens um I've seen this before I got a little error here and that's okay and a whole bunch of stuff went on um it went to look through a whole bunch of things and now we'll take a look at this so um I want to go ahead and look at the commit and really the biggest thing that happened here was in the Palm you can see that we moved from 2.7.6 to 3.0 uh you could see that we moved from java 8 to Java 17 here and then we just had a group ID change here so cool uh that was able to uh do some things again very simple app but that's okay uh let's take a look at one more thing I think the to-do W changed right or the to-do class I had a normal to-do class in here and we were using um the not empty and we were importing uh the Java x. validation and now we're importing the Jakarta validation so it knew as part of that upgrade process that hey anywhere we're using Java X um from java e we need to upgrade that to Jakarta dot so that was part of that migration as well so really cool so now when you're done with three uh 3 0 you can go to 31 so if we wanted to we can come through and run this one and then run this one so 3.2 would get us up to jdk I think there's probably a way to move to jdk 21 but I think this is just moving to 3.2 all right so I think that's really all I wanted to show you may see a lot of examples out there where you have to run the maven uh command from the command line and I know that can get a little bit confusing for me I'm much more of a visual person being able to see the recipe right here in my IDE along with what it's supposed to do uh then seeing that output on the uh count for me that kind of like triggers everything and I know what's going on so I really like that this plugin is now bundled into intellig and you get the little icon over there you could see that it's an open rewrite recipe and again this is the collection of recipes from open rewrite you can find other rewrite recipes out there you can create your own for your own organization if you have things going on that you are trying to automate create your own recipes test them out out right here in your ID everybody is a winnner I hope you learned something new today and if you did Friends do me a big favor give me a thumbs up subscribe to the channel and as always happy [Music] coding
Info
Channel: Dan Vega
Views: 10,224
Rating: undefined out of 5
Keywords: dan vega, java, spring framework, spring boot, upgrade spring, upgrade spring boot, open rewrite
Id: e4R6AZHpAD8
Channel Id: undefined
Length: 13min 52sec (832 seconds)
Published: Fri Mar 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.