View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
What is Maven● A build tool.● A Project Management Tool.● An abstract container for running Build Tasks.● A tool providing guidelines for best practice development.
What is Maven(Contd.)● It is a tool to manage applications that graduate beyond simple and need to start finding consistent ways to manage and build large collections of independent modules and libraries which make use of tens of hundreds of third-party components.● According to the official definition - Maven is a project management tool which encompasses a project object model, a set of standards, a project lifecycle, a dependency management system, and logic for executing plugin goals at defined phases in a lifecycle. When you use Maven, you describe your project using a well-defined project object model, Maven can then apply cross-cutting logic from a set of shared (or custom) plugins.
Convention over Configuration● Maven incorporates this concept by providing sensible default behavior for projects.● It goes further than just simple directory locations. Mavens core plugins apply a common set of conventions for compiling source code, packaging distributions, generating websites, and many other processes.● However, most of the defaults provided by Maven can be customized.
Conceptual Model of a Project● Maven maintains a model of project. It does not just compile source code into bytecode, it develops a description of a software project and assigns a unique set of co-ordinates to a project.● It helps to mange projects license, the developers and contributors to the project, other dependent sub-projects etc.
Installing Maven● Verify that youve installed Java in your system.● Download Maven.● Set up Maven (remember to set up JAVA_HOME and MAVEN_HOME, and add maven home directory to PATH).● Test your installation (mvn –version in Windows).● Google it if any problem occurs/ask in our group channel.
Creating a Simple Project● We will use the Maven Archetype plugin to generate a simple skeleton project.● Open up your command prompt/shell, go to a particular location where you want the project to be stored, and execute the following command and keep hitting ENTER for now to accept the default options. The whole command should be typed in one line - mvn archetype:generate -DgroupId=org.wikiengine.intro -DartifactId=intro -Dpackage=org.wikiengine.intro -Dversion=1.0-SNAPSHOT● Please be patient. It will require considerable amount of time to complete. You will also need internet connection.
Creating a Simple Project(Contd.)● An Archetype is defined as - An original model or type after which other similar things are patterned; a prototype.● A number of archetypes are available in Maven for anything from a simple Swing Application to a complex web application, and the archetype:generate offers a huge list of archetypes to choose from, which you just did when you kept hitting ENTER. Here, archetype is the plugin prefix, and generate is called a GOAL.
Generated Project Skeleton● Open up the intro folder. The directory structure should look something like this - ● intro ● src – main ● java ● …. ● App.java – test ● java ● … ● AppTest.java ● pom.xml
Generated Project Skeleton(Contd.)● The Maven Archetype plugin creates a directory called intro that matches the artifactId. This is known as the projects Base Directory.● Every Maven project has what is known as a Project Object Model, or, POM, for short, in a file named pom.xml. This file describes the project, configure plugins, and declare dependencies.● Projects source code and resources are placed under src/main. This folder may contain some simple java classes and some properties file, or it may be the document root of a web application, or it may contain configuration files for an application server (remember Glassfish, anyone?). In a java project, Java classes are placed in src/main/java and classpath resources are placed in src/main/resources.
Generated Project Skeleton(Contd.)● Projects test cases are located in src/test. Under this directory, Java classes such as JUnit tests are placed in src/test/java, and classpath resources for tests are located in src/test/resources.● For the time being, the Maven Archetype plugin has generated a single Java class org.wikiengine.intro.App, which is a pretty simple Hello World program.
Building and Running the Project● Once again, open up your command prompt/shell and traverse into the projects Base Directory, which is intro in our case. Then execute the following command - mvn install● Be patient, it will take considerable amount of time in this case too. Also you will need to be connected to the internet.● After your build is successful, run the program to test it - java -cp target/intro-1.0-SNAPSHOT.jar org.wikiengine.intro.App
The POM File● Take a look at the generated pom.xml file. This is the most basic POM we will ever deal with for a Maven project, usually a POM is considerably more complex.● The first few elements – groupId, artifactId, packaging, version – are what is known as the Maven co-ordinates which uniquely identify a project.● name and url are descriptive elements of the POM providing a human readable name and associating the project with a web site.● The dependencies element defines a single, test-scoped dependency on a unit testing framework called JUnit.
The POM File(Contd.)● Maven will always execute against an effective POM. This effective POM is constructed from the current projects pom.xml, all parent POMs, a super-POM defined within Maven, user-defined settings, and active profiles. All projects ultimately extend super-POM, which defines a set of sensible default configuration settings.● To see the effective POM, run the following command in the project base directory - mvn help:effective-pom● You should see a much larger POM which exposes the default settings of Maven. This can come in handy while debugging.
What Have We Done So Far● A project is generated which consisted of a POM and some code assembled in the Maven standard directory layout. We generated this project by executing a plugin goal.● We then executed Maven with a lifecycle phase as an argument, which prompted Maven to execute a series of Maven plugin goals.● Lastly, we have installed a Maven artifact into our local repository.
Goals● Goals are unit of work in Maven. A goal is a specific task that contributes to the building and managing of a project. Goals may be executed as a standalone goal or along with other goals as part of a larger build.● Examples of goals include the compile goal in the Compiler plugin, which compiles all of the source code for a project, or the test goal of the Surefire plugin, which can execute unit tests.● When referring to a plugin goal, we often use the shorthand notation: pluginId:goalId (remember our archetype:generate portion of the command before?).
Goals(Contd.)● They define parameters that contain sensible default values. In our example, the goal stopped for some input from us because there were no sensible defaults for this case. If we had run the archetype:create goal, Maven would have assumed that we wanted to generate a new project using the default maven-archetype-quickstart archetype. This is also an example of the convention over configuration principle.● Each goal has access to the information defined in the projects POM. They always execute in the context of a POM.
Plugins● A Maven plugin is a collection of one or more goals. Example plugins are Compiler, Surefire, Jar etc.● The Maven Core basically does nothing when it comes to project build. It basically knows how to parse the command line, manage a classpath, parse a POM file, and download Maven plugins. All other important tasks are taken care of by the various plugins.● This ensures universal re-usability of common build logics.
Maven Lifecycle● The second command that weve executed contained a Lifecycle Phase as an argument.● Maven is based around the central concept of a Build Lifecycle. It is a list of named phases that can be used to give order to Goal Execution, wherein a phase represents a stage in the lifecycle. Goals are chosen and bound by the packaging type of the project being acted upon.● There are three built-in lifecycles – default, clean and site. The first one handles project deployment, the second one handles project cleaning, while the last one handles the creation of the projects site documentation. Each of these is defined by a different list of build phases. To see the total list, go here.
Maven Lifecycle(Contd.)● The default lifecycle contains quite a lot of build phases. They are executed sequentially to complete the default lifecycle.● To fully execute a whole lifecycle, you only need to invoke the last build phase to be executed. This is because if you call a build phase, it will execute not only that build phase, but also every build phase prior to the called build phase. So, for the default lifecycle, executing mvn deploy will cause the whole default lifecycle phases to be executed.● A build phase is basically made up of various goals as plugin goals can be attached to it.
Maven Lifecycle(Contd.)● 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 attached to it. If it has no goals bound to it, it will not execute.● A goal which is not bound to any build phase could be executed outside of the build lifecycle by direct invocation (remember archetype:generate, anyone? ) .● The lifecycle is what allows a developer to jump from one Maven project to another without having to know very much about the details of each particular projects build.
Next Meeting Topics● Understanding Maven Coordinates, Repositories, Dependencies.● Customizing Maven, managing dependencies● Generating complex web application, adding Java EE dependencies.● Managing multi-module projects.
References/Acknowledgments● Maven by Example by Sonatype.● Official Maven Documentation.● Our handsome guy Sharif Shahnewazs awesome slide.