MAVEN

MAVEN

A project object model

ou may be wondering, what is Apache Maven? Apache Maven, is a software project management, and comprehension tool, based on the concept of a project object model, or POM. Maven can manage a project's build, reporting, and documentation from a central piece of information. This is a quote directly from the Apache Maven project website. A more comprehensive definition of Apache Maven, is that Maven is a project management tool, which encompasses a project object model.

It follows a set of standards, it includes a project life cycle, a dependency management system, and logic for executing plugin goals at defined phases in a life cycle. Maven is designed to provide a simple project setup, that uses best practices as a guide. With Maven, your projects follow a consistent structure. Projects become IDE agnostic, by enforcing a

consistent structure, it makes modifications easier in the future, when new developers are introduced to the project.

It also ensures that programmers always get the most recent version of compilers, et cetera. Most Java projects rely on other projects, and open source frameworks, to function properly. It can be cumbersome to download these dependents manually, and keep track of their versions, as you use them in your project. Maven provides a convenient way to declare these project dependencies, in a separate, external, POM.XML file. It then automatically downloads these dependencies and allows you to use them in your project. This simplifies project dependency management greatly.

It is important to note, that in the POM.XML file, you specify the what, and not the how. The POM.XML file, can also serve as documentation tool, conveying your project dependencies and their versions. Software developers refer to Maven, as a build tool. Since it is used to build deployable artifacts from source code. On the other hand, if you asked a project manager they might call it a project management tool, since it follows a development life cycle. In reality, it is both.

Installation in Maven

To get started using Apache Maven, start by downloading Maven from the maven.apache.org website. From this page we can either use the link on the left that says Download, or we can just use the link in the middle where it says Use Download. Maven is currently at version 3.3.9 at the time of this series. Always make sure you download the latest version. Let's look quickly at the system requirements. Probably one of the most important ones is that it requires Java Development Kit or JDK 1.7 or higher.

There is no minimum memory requirement, and as far as disk space goes, you need approximately 10 MB for the Maven installation itself, but in addition to that, additional space will be needed for your local Maven repository. The size might vary depending on your usage, but expect at least 500 MB, and there's no minimum requirement for your operating system. Let's scroll down and take a look at the files. One of the nice things is under Files, there is a link in the first paragraph there that takes you to installation instructions.

We'll take a look at that in a minute, but for right now, under the link we have Binary tar file, a Binary zip file, and two source files. I'm gonna go ahead and download the apache-maven-3.3.9-bin.zip file. It doesn't take long to download and you can see it's only 8.2 MB. Let's switch over to the installation instructions next. It's always a good idea to check the installation instructions in case something has changed from the time of this video.

The first thing we need to do is extract the archive file that I just download, so I'm gonna go down to my archive, and I'm gonna click on it and I'm going to click on Extract, and I'm gonna say Extract all. The Maven website recommends that you extract the file to the Program Files directory on your C: drive so I'm gonna do that. I'm gonna click on Browse, I'm gonna go into my C: drive, to my Program Files, and I'm going to go ahead and say Select Folder, and I'm gonna say Extract.

It is important to understand you'll need Administrative Access in order to copy the file to your Program Files so I'm gonna click Continue. Now I have a folder called apache-maven-3.3.9, and if I open it up, I'll see there's a bin folder, a boot folder, a conf folder, and a lib folder. At this point, I've completed my download. The instructions for using Maven depends on whether you're running a Windows machine or a Mac OS or Linux machine.


Install Maven on Windows

Now that we have the Maven file downloaded and extracted into our program files directory, we can go ahead and install Maven on our Windows machine. As I stated earlier, the Maven download is not very large. That's because Maven's power is included in its plugins which are located and retrieved from a central repository on an as-needed basis and allowing for greater code reuse. Before the installation we must verify our Java version from the command line using java -version.

Remember it must be 1.7 or higher. I'm going to open up a command window. I'm going to type cmd and hit enter. Now, I'm going to type, java -version and hit enter. The version on my machine is 1.8.0_91. That's higher than 1.7, so I'm good to go. To make life easier, we need to update our environment variables. We can do that using either the command window, but, if you use the command window you're going to have to update them every time, or, we can go to the control panel.

For now, let's use the control panel. I'm going to go back to start again and type "control panel". From here, I'm going to go to System and Security. I want to go to System. Now, I need to go to my Advanced system settings. Then you'll see at the bottom it says Environment variables. Let's click on Environment variables. The top half of the screen are specific user variables for the producer profile. The second half, the bottom half, is my system variables.

What we want to do here is we want to add a variable to indicate where the Java home is. This is where we stored our JDK. In my case, mine is located on my C drive, in my program files directory. Let me show you. I'm going to open up File Explorer. I'll go to my C drive. I'll go to Program Files, Java, and there it is, jdk1.8.0_91.

It's probably in the same location for you. Go ahead and find the path for your JDK file. Let's add a new system variable. We're going to click on new and we're going to call it java_home. The value for the variable will be the path that takes me to my JDK file. In our case it's going to be c:\program files\java\jdk, mine was 1.8.0_91 and I'm going to click OK.

Now you'll see it's added to your list of system variables. The next thing we need to do is we need to add our new Apache Maven directory, which is also in my program files. Let's go back and take a look. If I go to my C drive, to Program Files, the very first folder is Apache Maven 3.3.9. Inside there is the bin folder. That's what I need, I need to know that path. Let me close that. What I want you to do is to click on the path variable that already exists and say "edit".

We're going to add a new variable. It's going to say, c:\program files\apache-maven-3.3.9 this time I also want to include, \bin. Now, my environment knows

where to find the Maven commands. I'm going to click OK. I'm going to click OK, again, and OK again, and I'll close my control panel.

Now, we do need to open a new command prompt. If you have any command prompt windows open, go ahead and close them and start a new window so it will pick up those environment changes that we just made. Now, from the command prompt, I'm going to type mvn -v and hit enter. We can see that this is Apache Maven 3.3.9. It tells me my Maven home folder. It gives me the Java version that we're using, the Java home, as well as the default local and operating system name.

At this point I'm ready to get started using Maven on my Windows machine.]

IDE integration

aven's popularity has grown, and many of the integrated development environments used with Java have started to integrate Maven with their product. For example, Maven is already set up to integrate with NetBeans, Eclipse, and IntelliJ, just to name a few. Starting with the NetBeans version 6.7 and higher, there is full Maven support. You can even use the NetBeans IDE to create Maven projects from archetypes using the new project wizard.

Finally, the IDE also includes a Maven repository browser that enables you to view local repository and register external Maven repositories. If you happen to be running NetBeans 6.5 or lower, you can add Maven by adding the necessary plugin. Under Tools, Plugins, choose Available Plugins, from there choose Maven, click Install, then Close. There is also good documentation on the NetBeans Wiki site for using NetBeans with Maven.

Let's take a look at the Wiki site. From the Wiki page, definitely take notice of the tutorials, tips, and tricks for using Maven. If you are accustomed to using Eclipse, then you wanna use the Eclipse version M2Eclipse, which is specifically designed for Maven integration. This version of Eclipse includes the ability to launch Maven builds, handle dependency management based on Maven's pom.xml file, automatic download of required dependencies, and wizards for creating new project and search capabilities for Maven remote repositories.


Here is the homepage for this version of the M2Eclipse website. As you can see, this version of Eclipse is designed specifically for integration of Apache Maven. Maven integration is already included in the IntelliJ IDEA platform. Programmers can start using IntelliJ to import existing Maven projects, create new projects, run Maven goals, and more. It has full editing support for the pom.xml files, and similar to NetBeans, JetBrains IntelliJ IDEA also has detailed documentation for the integration with Maven.

Here's another good place to start. So no matter which IDE you're using, chances are Maven support is already included.

Project Object Model (POM)

As I've stated, Maven use of the concept of a Project Object Model, or POM. This model has a a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at certain phases in the lifecycle process. One of the things that makes Maven so powerful is that it relies on the concept that projects are set up with default behaviors. For example, the pom.xml file is always located in the base directory.

The source code must be in a certain directory. Resources necessary for the project are in a another folder or directory. Test cases are in a specifically named folder. And a target folder is always created that's used for the final JAR file. Let's take a look at the folder structure

created by Maven for a calculator program. As you can see the base directory is called calculator. Inside calculator we have our src folder, as well as our target folder, and our pom.xml file.

Inside the source folder is where we'll find our main Java programs and our source code, as well as any needed resources. And, the test folder, which contains again, the Java test programs. And any resources needed for that. Finally, the JAR file will be stored in the target folder. This folder structure is an important example of how Maven has adopted convention over configuration. By always using a standard folder structure, it allows developers to concentrate on coding. Once the code and resources are placed in the correct directories, and the POM file is updated.

Maven handles the rest. A project model includes: A project description, a unique set of coordinates, project attributes, the project's license information. The project version, any authors or contributors to the project, and a list of project dependencies. Before we go further, let's take a look at a sample POM file. This is the file for the calculator project. The POM file is stored as an XML file. XML files use tags similar to HTML.

Except for the names inside the tags are different. In the case of Maven, we have tags such as group ID, artifact ID, packaging, version, etc. The artifact ID is used for the name of the program. In our case, calculator. Since it's a Java program, the packaging is going to be to create a JAR file. And the version in this case is 1.0. The description, name, and URL are all optional. Below that are the dependencies. When you create a sample program using Maven, it automatically adds a j unit dependency to allow us to do unit testing for our Java program.

You might have noticed the three red asterisks next to the three fields: group ID, artifact ID, and version. That's because these three fields together, make up what we call the coordinates of the project, and they must be a unique combination. So, if I wanted to create a second version of my calculator project. I'd have to change the version number from 1.0 to 2.0, or 1.1,

or something, to make it unique. Features that are enabled by using the POM include dependency management, access to remote repositories, universal reuse of build logic, tool portability and integration, allowing IDEs such as Eclipse, NetBeans, and IntelliJ, to have a common place to find information about a project.

And, easy searching and filtering of project artifacts. The Project Object Model, the POM, and the POM file are the heart of Maven projects. And provide key information about the project.

Maven lifecycle

When using Maven it's important to understand the Maven life cycle. Let's take a look at a high level overview of the flow when using Maven. Maven starts by generating a project. A project consists of a POM or Project Object Model and source code that's assembled in the

Maven standard directory layout. Next, we execute Maven with a life cycle phase as an argument that prompted Maven to execute a series of plugin goals. After that, we can install a Maven artifact into our local Maven repository.

And finally, we can run the app. Let's take a closer look at the default life cycle phases. One of the phases is

Validate. Validate is used to validate the project to make sure it is correct and all necessary information is available. Another phase is

Compile. We compile the source code of the project.

Test. Test compiles the source code using a suitable unit testing framework. These tests

should not require the code be packaged or deployed just yet.

Package,

take the compiled code and package it in its distributable

format. For example, a Java program will be packaged as a Java file or a Java archive file.

Integration-test. Process and deploy the package if necessary into an environment where integration tests can be run.

Verify runs any checks to verify the package is valid and meets quality criteria.

Install.

Install the package into the local repository for use as a dependency in other projects locally.

And finally, Deploy. This is done in an integration or release environment. It copies the final package to the remote repository for sharing with other developers and projects. Plugin goals can be attached to each lifecycle phase. As Maven moves through the phases in a lifecycle it will execute the goals attached to each particular phase. Each phase may have zero or more goals bound to it. For example, when we run mvn install we will see that more than one goal is executed.

In the package phase it'll automatically execute the JAR goal in the JAR plugin. Let's run an mvn install on an existing project and we can see the different goals that are executed. I've navigated to my directory for Hello World. If I run mvn install, which is a lifecycle phase,

we can see what goals are executed. At the top you'll see Maven resources-plugin 2.6 resources.

So the resources plugin was executed. Next, the compile was executed. There was nothing to compile because all classes were up to date. Next is the test resources,

the test compile, and finally using the surefire plugin it ran a test. The result of the test are listed below. It said the number of tests run was one. Zero failures and zero errors. So everything worked according to our specifications. The last plugin that you'll see here is the JAR plugin.

The JAR plugin again is used to create a Java archive file or a JAR file. After the JAR file is created the install then moves the JAR file to local Maven repository. You can see here it says that the installing of the Hello World to

c:/users/producer/ .m2/repository/com/lynda/hello world, the version number 1.0/helloworld1.0.jar.

It also installed the POM file into that same repository. That repository is automatically created by Maven and that's where all our local programs are stored.

Maven Repository

One of the big benefits of using Maven is you now have access to the Maven repository. There's actually two repositories that we're gonna talk about. The first one is the central Maven repository that contains a large collection of Java and other open-source components. Again, the power of Maven is that these open-source components are available to you but they will not be downloaded unless you need them. That enforces that you have the most recent version of each component. The second repository is a local repository that Maven creates on your computer.

It's usually located on your home drive in a folder called .m2. This directory contains your Maven repository. When you download a dependency from a remote Maven

repository, Maven stores a copy of the dependency in your local repository. In addition, it also places a copy of your jar file and the pom.xml file for each installed project. Let's take a look at both of these. Let's start by looking at a website that allows you to browse the central repository. You can find this at https://search.maven.org As you can see, you can enter in any component that you want to search for and it does have an advanced search feature and even a browse feature.

Let's just try searching for junit, which is one of the components that we use to do unit testing within Java. As you can see, there's several versions of the artifact ID, junit, that we can use in our program. Now, let's take a look at the local Maven repository. As you can see, my .m2 folder is located under Users, Producer. So yours might be located in a different place but you can find it by searching .m2. Inside the local repository, you'll see an index folder and a repository folder.

Let's open the repository and you'll see, since I've compiled and installed several Maven projects, it's already downloaded a lot of different components that I needed, including junit. I also created a project called HelloWorld and I installed the HelloWorld. Let's look at the installation for that. Since all my project so far, I've actually installed inside a package called com dot lynda. I need to go there and you'll see, there's HelloWorld.

Inside HelloWorld, there is a version 1.0 and inside 1.0 is a copy of my POM file, as well as my executable jar file. So all of your projects should be available inside your local repository.

Maven's dependency management

Another feature of Maven is the way it handles Dependency Management. As a programmer, we often take advantage of code reuse, especially in the realm of open-source programming. For example, in Java, we use APIs that contain libraries of software components that we can use instead of recreating the source code from scratch. Almost all Java programmers have, at some time, used the Math functions or the String functions in their coding. These functions include math.pow to find the value of a number raised to a power, or math.min to find the smallest of two numbers.

Some of the String functions include the .length command, the .substring, two lower, two upper. These are just a few examples of what it means to reuse existing code. Maven also allows us to reuse existing components and plugins using the dependency section of our POM file, our Project Object Model file. For example, a common dependency when working with Java is the junit component. We define dependencies inside a project's POM file using it's Maven coordinates.

Remember, the coordinates include the group ID, artifact ID and version. Let's refer back to the POM file we saw earlier. As you can see, there's a tag called dependencies, and inside these, there is one dependency called junit. The three tags groupID, artifactId, and

version are the coordinates that make this particular dependency unique. The scope identifies what part of the life cycle this dependency is going to be used in.

In this case, it's the test phase. It is easy to add additional project dependencies by updating this pom.xml file. By adding a list of dependencies here in one place, it is also easy for someone to identify what dependencies are required for this particular project. Finally, by including the dependencies in this external file, it is easy to update the version numbers in one place as dependencies might change.

POM categories and configuration

[Instructor] The Project Object Model or POM Categories and Configuration. The POM file contains all the information about a project. The file is stored with an .XML extension. Here's an example of POM.XML file that has the minimum amount of information required. As you can see, it has a groupId, an artifactId, and a version. Remember those three things make up the Maven coordinates and are required for all projects. Although this is not a comprehensive list, some of the most common categories that are usually included in POM file include the project coordinates, which we just saw, the project's license information, a list of developers and contributors to the project, a list of project dependencies, the name of the project, the URL associated with this project, the packaging type, the scope of the element listed, and even information about inheritance.

Each of these categories has its own XML tag. For example, the scope tag would be the less than sign, the word scope, and the greater than sign.

Although these are not all the categories that you can use in a POM file, these are the ones that you'll see most often.

POM syntax

The Project Object Model Syntax. The Project Object Model, or POM, is documented in an XML file, where XML stands for Extensible Markup Language, which is always located in the base directory of your project. The syntax for XML looks similar to HTML, using the angle brackets to indicate the beginning and end of an element. The difference is that the elements do not follow any standard list of keywords. Instead they are descriptive of the content inside the tags.

Every open XML tag must have a corresponding closing tag. And tags can be nested one inside the other. The file can start with an XML declaration,

but that is optional. All values in the file are declared as XML elements. All projects extend the super Project Object Model, or POM, automatically. And the specific project POM contains all pertinent information for that project. Here is an example of a pom.xml file that includes the XML declaration, and the required maven coordinates.

As you can see, the XML declaration takes up the first four lines. Below that, we have the modelVersion and then the maven coordinates, the groupID, the artifactID, and the version number.

The POM file contain many more XML tags depending on the complexity of your project.

Project dependencies

Project Dependencies. As a programmer, we often rely on other components available to us. Maven provides support for both internal and external dependencies. One of the most common dependencies is the junit dependency.This is used for testing a Java program. Other examples include log4j and jaxen, just to name a few. Here is some sample

code. As you can see, I am only displaying two of the dependencies for this project, but more can be added using the XML tag dependency.?

要查看或添加评论,请登录

David Dayakarun Chinnesh Erothi Preach Christ resurrected , AI health care in MIT的更多文章

  • ICONS AND DESIGNS

    ICONS AND DESIGNS

    Prelude to Icon Design Icons are Omnipresent Visualizing a universe without icons is impossible. You will get to know…

  • Delivering Employee Feedback

    Delivering Employee Feedback

    Observing employee behavior Selecting transcript lines in this section will navigate to timestamp in the video - One…

  • COPILOT

    COPILOT

    introducing Microsoft 365 co-pilot your co-pilot for work copilot combines the power of large language models with your…

  • Tableau

    Tableau

    Introduction Welcome back to the course on Tableau. From the previous course on Tableau: The Prequel, you have learned…

  • CREATING LINUX VM IN AZURE

    CREATING LINUX VM IN AZURE

    Linux on Azure Microsoft Endorsed some versions of Linux in the Azure Marketplace in which, The Linux Integration…

  • SOAR

    SOAR

    ingestation of AWS alerts 0:26 / 2:19

  • Let your Cloud be Secured

    Let your Cloud be Secured

    Understand the basics of cloud security—a core component of cloud computing. Beginning with the basics, instructor…

  • AI at Work

    AI at Work

    Python MACHINE LEARNING DATA SET Powered by 2-MachineLearningModels-Reference(unsaved changes) Beginners Data Science…

  • BLOCKCHAIN

    BLOCKCHAIN

    Prelude Welcome to the course Blockchain Intermedio. This course is a continuation of the course - Blockchain -…

    1 条评论
  • Threats And Delivering Cloud Workload Protection

    Threats And Delivering Cloud Workload Protection

    Hands-on Workshop: Hunting Security Threats And Delivering Cloud Workload Protection Using Azure Security Center and…

社区洞察

其他会员也浏览了