Use IntelliJ IDEA to create an EXECUTABLE JAR With Dependencies Using Gradle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're going to use IntelliJ IDEA to create an executable jar using Gradle we'll be using IntelliJ IDEA Community Edition which is free let's create a brand new project by clicking on new project and we'll walk through this dialog and discuss the options we select we'll give our project a name of my Gradle project notice the default language is Java and the default build system is Gradle we're using jdk 17 and for the Gradle DSL let's select groovy the DSL or domain specific language is the language we use to write Gradle build scripts in this case we'll be using The Groovy language under advanced settings let's take a look at a few options first we'll talk about the Gradle distribution option when we're using Gradle we can either make use of a local installation that we already have in place or IntelliJ can configure our project with a version of Gradle by downloading it for our project this downloaded version is called a wrapper since there are scripts that run which wrap the calls that perform the necessary downloads if we uncheck Auto Select we can see in the drop down list that we can choose the Gradle version we wish to use version 8 which is the most current major release is already selected by default under distribution if we select local installation we can choose the version of Gradle that we wish to use in this case IntelliJ has detected that I already have version 8.1.1 of Gradle installed on my machine for our project let's go ahead and use the wrapper and we'll check the Auto Select box once again so that IntelliJ will download the latest version for us my group ID is already set to come dot begin secure so let's go ahead and click create as our project is loading you'll notice a few things in the lower right corner we get a pop-up from IntelliJ telling us that our IDE has detected Windows Microsoft Defender running on this machine let's take a look at that message it's telling us that using Defender will be negatively impactful on the performance of IntelliJ and it's recommending that we add some directories to an exclusion list so the defender won't scan those directories it gives us the option to manually add those directories to the exclusion list or if we select automatically it'll do it for us of course we wanted to do it for us so let's select automatically if you run into this situation on your work computer you may need to speak to your help desk or system administrator since you may not have the permission to add these directories either manually or automatically to your own exclusion list we get a pop-up dialog box from IntelliJ telling us it's about to perform the task so we'll go ahead and select yes and let it continue in the upper right corner it's asking us to select which jdk we want to use let's go ahead and select 17. let's take a look at the Gradle build file the file contains a series of blocks that are used to configure different aspects of the build process plugins are Gradle specific bundles of functionality to enhance how our builds are run the group property was specified by us when we created our project the version property is supplied by default these properties plus the artifact name that we'll see in the settings file are used to name any artifact that we create the repository section tells Gradle where we'll be pulling our dependencies from in this case we'll be using Maven Central the dependencies section lists the required packages for building your application we didn't ask for June at Jupiter but IntelliJ supplied it by default taking a look at the settings.gradle file we can see the project name that we supplied when we created our project this plus the group inversion property that we saw earlier are used to naming the artifacts we create now let's take a look at the application code IntelliJ created for us it's a bit more than an ordinary hello world application but it's still a very simple message is printed along with the numbers one through five let's run The Code by selecting the green arrow in the gutter in the output window we see the results of the hello world message and the output from the for Loop over on the right let's select the Gradle window and then let's select the task drop down here we see groups of all the tasks that are available for us to run let's start by running the jar task when that's done we see in the build lives directory that a jar has been created let's open a terminal and CD into the build Libs directory let's try running the file by entering Java Dash jar migradle project zero one snapshot.jar we get the error no main manifest attribute which means there's no manifest file in the jar that specifies the primary location of the file for a jar when we run it however we know the jar file contains our code so let's try running it in a different way let's try to add the jar file to our class path and then try to invoke the main class file we'll enter Java Dash CP and the name of our jar file to add it to our class path then space and the fully qualified name of our main class com Dot beginsecure.main and when we do we get the expected output which is the Hello message followed by the output from the for Loop now let's add a dependency to our project and then update our code to use that dependency first we'll close some of these windows and open up the build.gradle file and go to the dependency section let's right-click and select generate and then add dependency in the search bar let's look for Json from the list presented to us we'll select the first one from org.json and we'll select the latest version from February 27 2023. when I was recording this video I verified that there were no reported vulnerabilities for this package before clicking add let's scroll through the information on the right here we can find links to the documentation license information the location on GitHub and more all this information can be helpful when you're researching a new piece of Open Source let's click the add button and close out the window we see the dependencies been added to our build.gradle file now let's make use of the package in our code back in your main.java file let's get rid of the generated code and create an object called Json object of type Json object notice that it's red even though we added dependency to our build.gradle file it hasn't been downloaded and made available in our project yet if we click the Gradle elephant with the recital symbol on it that will cause Gradle to download the dependency and make it available in our project the Json object name will go from red to Black and will be asked if we'd like to import the class so let's do that now we can create our Json object literal by adding a pair of curly braces we'll add a key called color and Surround it with quotes that will escape with the backslash then a colon and we'll set it equal to the value of blue next we'll use the printf statement to print our results in our string We'll add the message the color is followed by percent s and a percent n or the string placeholder and a new line then a comma and will return the get stream method of the Json object reference and return the value for the key color now let's run that code buried in the messages from Gradle we see our output the color is blue great so let's jar that up and create our fat jar over in the Gradle window let's run the jar task when it's done let's open the terminal window we're already in the libs directory let's run the dir command hmm the jar doesn't seem very big but that's okay let's try to run it anyway we'll enter Java Dash jar migradle project 0.1 snapshot.jar okay we get the Manifest thing again so let's try to run it using the class path technique that we saw earlier we get an error about the Json dependency not being found let's take a look at what's in the jar by running the command jar space tvf in the name of the jar this will show us the contents of the jar without extracting it and we can see the class file is there not the dependency we need to add the dependency to our jar file we'll need to make a change to the build.gradle file there are a few ways you can do this but the most reliable method I've found is what I'm about to show you and the result will be an Uber jar that we're after in the build file we'll create a jar section within that we'll create a manifest section and we'll include the attributes that we need to set for the Manifest there are optional values that we can set like the implementation title or the version but they aren't needed the version is always 1.0 and it will be set by default however we do need to set the main Dash class attribute followed by a colon in the name of our class file com.beginsecure.maine next we'll add another subsection called from and enclose that in curly braces this will pick up the name of the jars that we need to include in our Deployable artifact we'll enter configurations dot runtime class path which will pick up the jars use it runtime for our application this will exclude any test code like the junit code that we have listed as dependencies we'll invoke the collect method and use a closure to get our list we'll use it as the variable name and invoke the is directory method on each element of the closure and then we'll use the ternary operator to make a decision if the element is a directory we'll return it if not we'll invoke the zip tree method to zip up the elements and return it to the collection and that's it notice we've overridden the jar task so let's go back to the Gradle window and select the jar task and run it once again once it finishes let's go back to the build Libs directory and run the IR notice this time the jar is much larger around 72k let's run that jar by entering Java Dash jar migradle project 1.0 snapshot.jar we get our expected result the message the color is blue if we run the jar tvf command on our jar file we can see all the Json code that was included in the library along with the code we wrote and the Manifest file we generated if you found this video useful Please Subscribe for more videos like this in the future and remember to always begin secure
Info
Channel: Begin Secure
Views: 10,764
Rating: undefined out of 5
Keywords: gradle, how to create jar file, export jar file in intellij idea, create an uber jar with gradle, creating runnable jar with gradle, how to create fat jar in gradle, build jar with gradle, gradle fat jar with dependencies, gradle build jar with dependencies, getting started with gradle, how to run a gradle project, how to build a self-executable jar with gradle, generate runnable jar pakage dependencies - gradle, build application with gradle, creating a fat jar in gradle
Id: tKdFrVzDLV4
Channel Id: undefined
Length: 11min 12sec (672 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.