What is Maven?
Building a software project typically consists of multiple tasks such as
- downloading dependencies,
- putting additional jars on a classpath,
- compiling source code into binary code,
- running tests,
- packaging compiled code into deployable artifacts such as JAR, WAR, and ZIP files, and
- deploying these artifacts to an application server or repository.
Apache Maven automates these tasks, minimizing the risk of humans making errors. So it is not only build tool. 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.
Project Object Model (POM)
The configuration of a Maven project is done via a Project Object Model (POM), represented by a pom.xml file. The POM describes the project, manages dependencies, and configures plugins for building the software.
1. Project Identifiers
groupId – a unique base name of the company or group that created the project
artifactId – a unique name of the project
version – a version of the project
packaging – a packaging method (e.g. WAR/JAR/ZIP)
2. Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.16</version>
</dependency>
3. Repositories
A repository in Maven is used to hold build artifacts and dependencies of varying types. The default local repository is located in the .m2/repository folder under the home directory of the user.
If an artifact or a plugin is available in the local repository, Maven uses it. Otherwise, it is downloaded from a central repository and stored in the local repository. The default central repository is Maven Central.
Some libraries, such as the JBoss server, are not available at the central repository but are available at an alternate repository. For those libraries, you need to provide the URL to the alternate repository inside the pom.xml file:
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
4. Properties
Maven properties are value-placeholders and are accessible anywhere within a pom.xml by using the notation ${name}, where name is the property.
<properties>
<spring.version>5.3.16</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
5. Build
The build section is also a very important section of the Maven POM. It provides information about the default Maven goal, the directory for the compiled project, and the final name of the application.
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
//...
</build>
6.Profiles
A profile is basically a set of configuration values. By using profiles, you can customize the build for different environments such as Production/Test/Development
<profiles>
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
//...
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
//...
</plugin>
</plugins>
</build>
</profile>
</profiles>
the default profile is set to development. If you want to run the production profile, you can use the Maven command:
mvn clean install -Pproduction
Maven build lifecycles
validate – checks the correctness of the project
compile – compiles the provided source code into binary artifacts
test – executes unit tests
package – packages compiled code into an archive file
integration-test – executes additional tests, which require the packaging
verify – checks if the package is valid
install – installs the package file into the local Maven repository
deploy – deploys the package file to a remote server or repository
We can execute Java project with maven
we are going to execute our Java project with the exec-maven-plugin. You have to configure the necessary plugins in the pom.xml:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>com.baeldung.java.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
The first plugin, maven-compiler-plugin, is responsible for compiling the source code using Java version 1.8. The exec-maven-plugin searches for the mainClass in our project.
To execute application run the below command
mvn exec:java