Maven Tutorial: Installation, Repository Management, Project Creation, pom.xml Basics, and Common Commands
This article provides a comprehensive guide to Maven, covering its installation, the structure and purpose of local and remote repositories, step‑by‑step project creation, detailed explanation of the pom.xml file, essential Maven commands, and best‑practice tips for Java developers.
0. Preface
Jason Van Zyl, the creator of Maven, is introduced as a key figure in Java development who standardized project builds and dependency management.
1. Install Maven
Maven can be downloaded from http://maven.apache.org/ , extracted (e.g., to D:/tool/maven), and configured with the environment variables M2_HOME=D:/tool/maven and optionally MAVEN_OPTS=-Xms128m -Xmx512m. Running mvn -v verifies the installation.
2. Understand Maven Repository
Maven stores downloaded artifacts in a local repository (default C:\Users\<username>\.m2) which acts as a cache. When a required jar is missing locally, Maven fetches it from a remote repository such as Maven Central ( http://search.maven.org/ ).
3. Create Maven Project
Use the command mvn archetype:generate (interactive mode) or provide parameters in batch mode, e.g.,
mvn archetype:generate -DinteractiveMode=false -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.smart -DartifactId=smart-demo -Dversion=1.0 -Dpackage=com.smart.demo. Maven creates a standard directory layout ( src/main/java, src/test/java, src/test/resources) and a pom.xml file.
4. Understand pom.xml
The pom.xml (Project Object Model) defines the project’s coordinates ( groupId, artifactId, version), packaging type, dependencies, build plugins, and other configuration. A minimal formatted example is shown below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart</groupId>
<artifactId>smart-demo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- additional dependencies such as MySQL, Servlet API, JSTL, etc. -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- other plugins: surefire, war, tomcat7, etc. -->
</plugins>
</build>
</project>Key points include using properties for reusable values, setting appropriate scope for dependencies (compile, test, runtime, provided, system), and configuring plugins for compilation, testing, packaging, and deployment.
5. Use Maven Commands
Common lifecycle commands are: mvn clean – removes the target directory. mvn compile – compiles source code. mvn package – creates the artifact (jar/war). mvn install – installs the artifact to the local repository. mvn deploy – uploads the artifact to a remote repository.
Commands must be executed from the project root where pom.xml resides.
6. Conclusion
Maven standardizes Java project builds, automates dependency management, and integrates with IDEs and CI pipelines. Mastering the basics presented here paves the way for exploring advanced Maven features.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
