Maven notes – Introduction part 1
What is Maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
Maven use Convention over Configuration which means developers are not required to create build process themselves. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml. In order to build the project, Maven provides developers options to mention life-cycle goals and project dependencies (that rely on Maven pluging capabilities and on its default conventions). Much of the project management and build related tasks are maintained by Maven plugins.
How maven works
What is POM?
XML file, it’s fundamental unit of work in Maven. Locate in the base directory of the project as pom.xml. The POM contains information about the project and various configuration detail used by Maven to build the project(s). Some of the configuration that can be specified in the POM are following:
- project dependencies
- build profiles
- project version
- mailing list
Only one POM in each project. All POM files require the project element and three mandatory fields: groupId, artifactId,version. Projects notation in repository is groupId:artifactId:version.
What is plugin?
- A plugin provides a set of goals that can be executed to perform a task.
- There are Maven plugins for building, testing, source control management, running a web server, generating Eclipse project files, etc.
- Some basic plugins are included in every project by default and they have sensible default settings.
What is goal?
- Like an ant target
- We can write our own plugins with goals or use those provide by Maven.
- For example, a Java project can be compiled with the compiler plugins compile-goal by running mvn compiler-conpile.
What is Archtype?
A template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user’s requirements.
What is repository?
A repository is a place i.e. directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily. There are three types of repository:
- Local (on your machine). When running Maven build, then Maven automatically downloads all the dependency jars into the local repository. First place to search.
- Central (provided by Maven community). When Maven does not find any dependency in local repository, it starts searching in central repository.
- Remote. Own custom repository containing required libraries or other project jars. Maven will go to this repository if the dependency not found at central repository.
What if dependency is not available in any of remote repositories and central repository? The answer is External Dependency. Put our jars into folder then mention that folder in dependency section using <systemPath>.
Build Life Cycle
A Build Lifecycle is a well defined sequence of phases which define the order in which the goals are to be executed. The individual steps are called phases and the same phases are run for every Maven build using the default life-cycle, no whater what will produce. Maven has following three standard life-cycle:
- clean. There are three phases:pre-clean, clean, post-clean
- default(or build). This is the primary life cycle of Maven and is used to build the application.
- site. Generally used to create fresh documentation to create reports, deploy site etc. Phases: pre-site, site post-site, site-deploy
A goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked.
Example: mvn clean dependency:copy-dependencies package
When a phase is called via Maven command, for example mvn compile, only phases upto and including that phase will execute. Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR).