[MUSIC]
>> Greetings everyone. Welcome back
to Microsoft's Java for Beginners series.
My name is Brian Benz.
I'm a Cloud Advocate at Microsoft,
and I'm joined by Julien Dubois,
who's going to talk to us about build tools.
Julien, do you want to tell us a little bit about
yourself and what you'll be talking about?
>> Yes, thank you, Brian. I'm Julien Dubois.
I've been a Java developer for more than 20 years.
In fact, I've done so much Java that I've been
elected to Java Champion like you, Brian.
Usually, people know me in the Java world because I work in
open source and also I work at
Microsoft as a Developer Advocate like you.
Today, we're going to learn about build tools.
Build tools are necessary if you're
a Java Developer because they are here to help you compile,
package, test, and run your projects.
There are also here to help you manage your project dependencies.
For example, if you use a framework like
spring and you want a specific version of spring,
well, you need to use a build tool to select the dependency.
They're going to be very useful for you as a Java Developer,
and doing this eight-minute video,
we're going to have a look at one of my projects and see how
I use Maven in order to build and package my project.
You created your first Java project
and you were able to compile it
and run it from within your IDE.
But soon, you will want to do more.
You will want to share that project with other people.
You will want to use a continuous integration server.
For all of those, your IDE will not be
enough and you will want to use what we call a build tool.
Build tools are very popular amongst programming languages.
They are often bundled inside programming languages.
But that's not the case in Java,
and you need to use a third party tool.
There are several ones available.
The two most popular ones are called Maven and Gradle.
They are both free and open-source,
and for something basic like what we're going to do right now,
they both work the same.
We're going to use Maven here because it's more
popular and it's simpler to start with,
but if you have a more advanced use case, have a look at Gradle.
You probably won't be disappointed by it.
Let's take a project. This one is called NubesGen.com.
It's available on GitHub.
Like thousands of other projects on GitHub,
it's using Java and Maven.
In order to use it,
to compile it, to test it,
to package it, you will need to look for this file called pom.xml.
This is a Maven configuration file
and it's an XML file, which is good news.
XML is a bit verbose, as we can see here.
But with XML, you've got automatic validation and
documentation of your file
and this is going to be very useful here.
This file has got three main parts.
Let's have a look at the first part.
This is mostly metadata about our project.
For example, we have the name of the project,
we have the packaging type of the project,
we have the version of the project,
and we have a description of the project.
Very normal stuff when you want to configure your project.
There's one specific and optional part,
which is very important here.
This is a power on project.
Here, we are innovating from another pom.xml file,
which is provided by the Spring Boot Project.
Here I'm doing the Spring Boot Project
and we inherit from Spring Boot.
Spring Boot is going to provide us with
default versions and default configure libraries for us.
Let's have a look at the second part to understand this better.
In the second part, we are listing
the dependencies that our project is using.
That's the library that our project needs.
Let's have a look at the first one.
This one has got a group ID.
This is an organization that is providing the library for us.
This dependency is provided by the Spring Boot Project,
and our library here,
that's an artifact ID is called Spring Boot Starter Mustache.
That library is provided by Spring Boot,
and we don't provide a version here.
That's because the version is provided by
the Spring Boot Project which is here.
Using a pom project is a very good practice because it
removes me with a need to specify versions of my libraries,
like what I'm doing here.
Specifying the version could be
interesting if I know exactly what I'm doing,
but mostly that's maintenance stuff once
there will be a new version of
this library and we need to go ahead,
and test this version of the library.
That's more work and I'd rather not do that.
Second thing that is interesting in
those dependencies is that those dependencies are transitive.
What we mean here is that
this library is not just pulling that library.
This is pulling a tree of libraries that this one depends on.
Let's have a look graphically here.
If we go to dependencies and if
we have a look at our mustache library,
we can see this is pulling
those two libraries and this one is pulling those other libraries,
including this one which is pulling
all the libraries and so on and so forth.
It is a tree of libraries which all have
their versions pre-defined for me. That's very powerful.
Here I'm just putting one library and in fact,
I'm having a whole tree of libraries
which are all automatically configured for me.
This is very powerful for my project.
Last section of this file is a plug in section.
Maven comes with some default plug ins to clean, compile, test,
package your project, but you will want to use
some plug ins to have some other capabilities.
Like here, I'm using Spring Boot and
I'm using the Spring Boot Maven plug in.
This Spring Boot Maven plug in allows me
to run my Maven project from
Maven and also to package it specifically for Docker, for example.
That's what this configuration here is for.
There are thousands of plug ins available,
and they are very useful to enhance the capabilities of Maven.
Now that we have described this file,
let's have a look at how it works.
There are different ways to run this.
The easiest one is to use an IDE plug ins.
All major Java IDEs have got Maven support,
so you can just click on your IDE and run Maven.
If you don't have this plug in or if you
don't want to use it because maybe you do as a user terminal,
well, you can just open up your terminal.
Here we have two ways to use Maven.
Either you have installed Maven
on your computer like what I've done here,
so here I can clean my project using Maven.
This works very well,
but I'm using a version of Maven that is
installed by myself globally on my laptop.
That's not the best practice.
No, most projects provide what
we call a Maven Wrapper. Let me use it.
You can see here I've got a w,
and I'm going to call the same Maven command, which is clean.
This is going to do the same thing,
but this is doing it with the Maven Wrapper.
A Maven Wrapper is provided by the project.
We can see it in my folder here.
That means two things. First, I don't need to install Maven.
That's always better.
Second thing, I'm using the very Maven version
that is provided by the project,
so I have less risk of anything
failing as I'm using the same version as a project.
Now let's use the graphical interface because it's easier.
Maven provide some default lifecycle goals.
The most common one is a clean one.
This is just cleaning up
the generated and compiled files from Maven.
The second one, and you will want to use, is the compile one.
Here I am compiling my files.
The third one is a test one.
This is to test my project to use, for example, jar unit.
What is interesting is that this goal,
the test one, uses compile goal first.
When I'm doing test, I'm compiling first and then testing.
Same thing for the last one,
this package goal,
which is the most important one.
When I run package here,
this is going to compile my code,
test it, and then package everything.
As we remember, here we want to have a jar package of our
NubesGen Project version 0.1.2-snapshot.
This is going to create a new folder here,
which is the target folder.
This is where Maven is working.
Inside that target folder,
I can see my compiled classes.
Once the tests will have run,
I will have my test results here.
Once everything is packaged into the jar file,
well, the jar file will appear here and I will be able to use it.
If it's a Spring Boot executable jar file,
so I will be able to execute it straight away.
Let's wait just a little bit to see that file appearing.
It is being generated and here it is.
Here is my file and now I can use it or deploy it
maybe to another server where people can save it and use it.
I hope you've enjoyed this little introduction to Maven.
If you want to know more about Maven,
please go to maven.apache.org,
which is the official Maven website
where you will find lots of documentation,
where you will also be able to download and install Maven,
and also where you will find a lot of plug ins.
Again, plug ins are very useful if you want to
enhance what Maven does by default. Thank you.
>> Thanks, Julien. That was great.
Please, do check out
the other topics in Microsoft's Java for Beginners series.
[MUSIC]