Weitere Ă€hnliche Inhalte Ăhnlich wie Apache Maven (20) KĂŒrzlich hochgeladen (20) Apache Maven1. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Automated build management.
Maven
2. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Manual build management (life before maven)
â Download the dependencies to compile the source code.
â Run the javac command to compile the source code.
â Run the java commands to test the source code.
â Run the jar command to package the source code.
â Run the javadoc command to create the documentation.
3. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Hey, but..
Anything, which is not the
part of business logic,
must be automated!
Thatâs correct! Maven is at our rescue.
4. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
What is Maven?
â At its simplest, build automation tool
â Produces deployable artifact (jar, war etc) from source code.
â Helps us manage the dependencies.
â Has built in build lifecycle.
â Can also be used as a project management tool
â Can produce the project documentation
â Supports plugins to provide various report.
â Supports versioning and releasing the project.
5. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven in action!
Lets create a simple maven based project.
6. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Creating a simple java application
â mvn archetype:generate
â Creates an application in interactive mode; will prompt for
â archetype - default 414 (maven-archetype-quickstart)
â archetype version - default 1.1
â groupId - com.hashedin.hu
â artifactId - HelloWorldMaven
â version - default 1.0-SNAPSHOT
â package - default groupId
â mvn archetype:generate -DgroupId=com.hashedin.hu -
DartifactId=HelloWorldMaven -DinteractiveMode=false -
DarchetypeArtifactId=maven-archetype-quickstart
7. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Directory Structure
â src/main/java
â The source code directory.
â com.hashedin.hu package
created automatically by
archetype.
â src/test/java
â The test directory.
â The test cases are also placed
inside the same default
package.
â pom.xml
8. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Contents of pom.xml
9. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Goals - Automated build commands
â mvn compile
Compiles the source code, generates any files, copies resources to our
target directory.
â mvn clean
Deletes the target directory and any generated resources.
â mvn package
Runs the compile command first, run test cases and packages the app
based on its packaging type.
â mvn install
Runs the package command then installs it in local repo.
10. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
The âtargetâ directory
â Automatically created by maven.
â This is where everything gets compiled to.
â This is also the place where test cases run from.
â Contents in this directory gets packaged into jar, war
etc.
11. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
The pom.xml
â Holds the meta information about the project.
â Project Information
â groupId
â artifactId
â version
â packaging
â Dependencies - Direct dependencies used in our application.
â Build - Plugins, Directory Structure etc.
â Repositories - Holds all the dependencies and artifacts.
12. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Creating a web application
â src/main/webapp
â The base directory containing
all the assets like JSP, Images
etc.
â src/webapp/WEB-INF
â web.xml - Deployment Desc.
â src/main/java
â Standard java classes: Servlets,
POJOs etc.
â pom.xml
mvn archetype:generate -DgroupId=com.hashedin
-DartifactId=hu-webapp
-DarchetypeArtifactId=maven-archetype-webapp
13. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Dependencies
14. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Dependencies
â Resources that our project relies upon to function.
â To include, just list the dependency in pom.xml
â Transitive dependencies will be pulled in by maven.
â Dependencies are identified by their coordinates
â groupId
â artifactId
â version
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.5.Final</version>
</dependency>
Example:
15. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Transitive Dependencies
â Our project dependenciesâs dependencies.
â Eg: Hibernate has dependency on 8 other resources:
â hibernate-core
â hibernate-commons-annotations
â jboss-logging etc
â All the transitive dependencies are automatically
downloaded and added by maven.
â The main reason people start off with maven.
16. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
â Not all resources are needed at all times!
â There are 6 available scopes:
â compile - default scope, artifacts available everywhere.
â provided - like compile, but not packaged into an artifact. It will be
available where artifact is deployed. (servlets-api)
â runtime - needed only for execution not for compiling.
â test - available for test-compilation and execution phase.
â system - provided, you specify the path to the jar on file system.
â import - deals with dependencyManagement and multi modules.
â Lets add few dependencies to our simple application.
Dependencies Scope
17. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Repositories
18. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Local Repository
â Where maven caches everything it downloads
â /home/<username>/.m2/repository.
â When maven needs to resolve a dependency, it first looks into the local
repository and then goes about downloading it.
â Stores the artifacts using the information we provide for artifactId, groupId
and version.
â ~/.m2/repository/com.hashedin/HelloWorldMaven/1.0-SNAPHOST/HelloWorldMaven-1.0-
SNAPSHOT.jar
â No need to copy the artifact into every project and store it in the SCM (git
etc)
19. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Remote Repository
â Just a network accessible location that maven downloads
dependencies from.
â Default location: http://repo.maven.apache.org/maven2/
â We can add multiple repositories.
â All the artifacts that remote repository contains are open
source. Dont add any private artifact of the organization.
20. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Adding a new remote repository
We can add a remote repository to search for dependencies if
these dependencies arenât available on default location.
21. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build lifecycle and plugins
22. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build Lifecycle
â Software projects undertake various steps before they are actually
distributed. (eg. compile, test, package etc).
â Build lifecycle defines the process of building and distributing the
artifacts.
â There are three builtâin build lifecycles:
â Default/Build - Handles project building and deployment.
â Clean - Handles project cleaning
â Site - Handles projectâs site generation.
â Each build lifecycle is made up of phases.
23. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Build / Default lifecycle
â You can invoke a specific phase on the lifecycle
â Example: mvn test
â When you invoke a specific phase, every previous phase
run, including the one you specified. If we run, mvn test
Refer: All lifecycle phases
Validate DeployCompile
Tes
t
Package Install
Validate Compile
Tes
t
24. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Clean and Site lifecycle phases
â Clean
â Handles project cleaning.
â Deletes target directory and any generated files.
â Site
â Generates project documentation.
â Used to generate various kind of reports.
Pre - Clean Clean Post - Clean
Pre - Site Site Post - Site Site - Deploy
25. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture
Maven - Core Engine
Remote
Repositories
Plugin 1 Plugin 2 Plugin 3 Plugin N
Remote
RepositoriesRemote
Repositories
Local
Repository
Fig: Mavenâs plugin based architecture.
26. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture - Core Engine
â Project processing
â Read the project configuration file and configure the
project accordingly.
â Build lifecycle management
â Run a series of phases (as seen before).
â Framework for plugâins
27. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven Architecture - Plugins
â Provides the core operations to build your project.
â For example, to create a jar the maven jar plugin will do the job, not
maven itself.
â Plugâins provides one or more âGoalsâ
â A Goal perform some operation on the project.
â Ex: compile, create a Jar, deploy to Jboss, etc.
â Goals can be bound to build lifecycle phases
28. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
â Goals can be bound to buildâlifecycle phases
â Eg: $ mvn test
29. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
We can bind a pluginâs goal to a lifecycle phase
30. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Maven lifecycle, plugins, and goals
â Putting lifecycle phases plugâins and goals together
â A lifecycle is a series of phases
â A phase is made of goals
â Goals are provided by plug ins
â Each phase have default bindings
31. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Jetty
The web interface for Maven.
32. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Jetty - The Web Container
We can use the jetty plugin to run our web application
For jetty to function without any configuration, keep:
â resources in ${basedir}/src/main/webapp
â classes in ${project.build.outputDirectory}
â web.xml in ${basedir}/src/main/webapp/WEB-INF/
Plugin configuration:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-
plugin</artifactId></plugin>
Usage:
Start: $mvn jetty:run
Stop: <ctrl-c>, $mvn jetty:stop
33. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Summary - Benefits of maven
â Declarative way of defining the dependency.
â Transitive dependency management.
â Keeps the code organized.
â Various plugins available for almost any task we would need
to undertake.
â Reduces build size by eliminating the need to check in jar
files into source code repository.
34. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
References
Apache Maven Guides
Maven by example
Apache Maven tutorial
35. Copyright © 2014 HashedIn Technologies Pvt Ltd. All rights reserved. CONFIDENTIAL
Thatâs It!
Questions, Suggestions Or Feedback?