A Beginner's Guide to Maven
Purpose
To give a brief overview about Maven and detail its most important components.
What is Maven?
Build Tool
- gives an artifact as an output (component, jar, zip)
- manages dependencies
Project Management
- versioning
- Meta information about the project
- produces Javadocs/site info
Folder Structure
- looks for source code in
src/main/java
- compiles all code into
target
- using the
pom.xml
file (also add the special plugin needed to make sure it works for Java 10+) - everything goes by //
src/main/java
- where all Java code is stored
- package declaration starts from here
- for other languages,
src/main/resources
orsrc/main/groovy
or something like that is used.
src/test/java
- for all test code
- specifically for unit tests
- automated tests
target
- compilation directory
- tests
- artifacts
pom.xml
- groupId: same as the package. They designate the business/application name as web address
- artifactId: name of the application and the artifact.
- version: snapshot v/s release
- packaging: how to distribute the application. default - jar
- dependencies:
- imported by naming convention
- groupId, artifactId, version has to be known
- once specified in the dependencies section, any transitive ones are automatically pulled in
Scopes
- compile: default, all resources are available everywhere inside the application
- provided: like compile, the artifact is available throughout the build cycle but it is not included in the final artifact. Eg: Servlet APIs, XML APIs
- runtime: not needed during compilation but during execution, like a driver that gets loaded at runtime. Eg: JDBC, JARs
- test: only during testing compilation and execution phase. nothing for compilation, packaging, or execution
- system: very brittle, not to be used
- import: deals with dependency mgmt, sharing resources across multiple POMs.
Repositories:
- HTTP accessible location to download files from
- default location (repo.maven.apache.org) defined in the Super POM inside the Maven installation which should be edited as overriding can be done in other ways as well.
- looks in local first, else pulls from remote
- local maven repo is in ~/.m2
- default storage location is /m2/groupId/artifactId
- Corporate Repo - Nexus, Artifactory
- Dependency Repo
- Plugin Repo
Plugins:
used to build & package applications
Phases:
- validate: validate project source & structure
- compile: compile source code
- test: compile test course code
- package: packages code in the defined packaging
- integration-test: deploy and run integration tests
- verify: runs checks to verify integrity
- install: install and package to local repo
- deploy: same as install but to remote repo
Different Plugins:
Compile:
- used to compile source & test code (in different phases)
- invokes Javac
- builds classpath based on dependencies & scope specified
- defaults to older versions of Java
Jar:
- packages code to a Jar file
package
phase- include & exclude files
- builds manifest
Source:
- package source code to distribute for context-sensitive help
- make code-intensive javadoc
package
phase
Javadoc:
- package javadocs to our source code
package
phase- tied to a Maven site goal
TODO:
- Add installation instructions
- Add examples -> files, screenshots
- Link projects
- Research tips & tricks