maven tutorial for complete beginners
TRANSCRIPT
-
8/11/2019 Maven Tutorial For Complete Beginners
1/15
Maven
Build tool
Similar to ant in this senseQuality Management
Used to run tests and generate reports
-
8/11/2019 Maven Tutorial For Complete Beginners
2/15
Common activities
Multiple jars
Dependencies and versions
Project structureBuild, publish and deploy
-
8/11/2019 Maven Tutorial For Complete Beginners
3/15
Maven POM
Project Object Model
Describes a ProjectName & Version
Artifact Type
Repositories
DependenciesPlugins
Profiles (Alternate Build Configurations)
-
8/11/2019 Maven Tutorial For Complete Beginners
4/15
Project Identifier (GAV) Maven identifies a project using
GroupId
ArtifactId
Version (Snapshot for in-development version) Describes a Project
Name & Version
Artifact Type
Source Code Locations
Dependencies
Plugins
Profiles (Alternate Build Configurations)
-
8/11/2019 Maven Tutorial For Complete Beginners
5/15
Project Identifier (GAV)
4.0.0
maven-trainingorg.lds.training
1.0
jar
Packaging element tells maven how to build the project Examples are jar, war, ear, custom
Default is jar
-
8/11/2019 Maven Tutorial For Complete Beginners
6/15
Project Inheritance POM files can inherit configurations
groupId, version
Project Config
Dependencies
Plugin Configurations
etc
maven-training-parent
org.lds.training
1.0
4.0.0
maven-training
jar
-
8/11/2019 Maven Tutorial For Complete Beginners
7/15
Standard Directory Layouttarget: Default work directorysrc: All project source files go in this directorysrc/main: All sources that go into primary artifact
src/test: All sources contributing to testing projectsrc/main/java: All java source filessrc/main/webapp: All web source filessrc/main/resources: All non compiled source filessrc/test/java: All java test source files
src/test/resources: All non compiled test source files
-
8/11/2019 Maven Tutorial For Complete Beginners
8/15
Maven Build Lifecycle
Maven build follows a lifecycle
Some common phasesgenerate-sourcescompile
test
package
installdeploy
clean
-
8/11/2019 Maven Tutorial For Complete Beginners
9/15
Maven repositories
Dependencies are download from repository via http
Downloaded dependencies are cached in a local repository
Usually found in ~/.m2/repository
Repository follows a simple folder structure.{groupId}/{artifactId}/{version}/{artifactId}-{version}.jar
groupId . is replaced with /
By default, maven uses the following repositoryhttp://repo1.maven.org/maven2
-
8/11/2019 Maven Tutorial For Complete Beginners
10/15
Defining Repositories
Repositories are defined in pom or settings
Repositories can be inherited from parent
Repositories are identified by id
...
lds-main
Uworx Nexus
http://10.210.40.37:8081/nexus/content
true
never
-
8/11/2019 Maven Tutorial For Complete Beginners
11/15
Dependencies
Register dependencies in pom.xml
Concept of transitive dependencies Can be controlled using exclusions or optional
declarations
Only compile and runtime scopes are transitive
Dependencies consist of
G.A.V (GroupId, ArtifactId, Version) Scope (compile, test, provided)
If version is not supplied, maven downloads thedefault version.
-
8/11/2019 Maven Tutorial For Complete Beginners
12/15
Adding dependencies
...
javax.servlet
servlet-api
2.5
provided
Dependencies can be made optional so that theyare not resolved transitively
-
8/11/2019 Maven Tutorial For Complete Beginners
13/15
Dependency exclusions
Exclusions exclude transitive dependencies
...
org.springframework
spring-core
3.0.5.RELEASE
commons-logging
commons-logging
-
8/11/2019 Maven Tutorial For Complete Beginners
14/15
Settings.xml
Contains configurations that
Should not be bundled to any specific project
Should not be distributed to an audience (user specificsettings)
Found at location
~/.m2/settings.xml
Contains configurations such as:
Remote repositories
Profiles
-
8/11/2019 Maven Tutorial For Complete Beginners
15/15
Archetypes
Maven project templating toolkit
Quickly generate projects
Best practices employed by your organization