Master Maven: Essential Tips for Java Backend Projects
This guide walks you through Maven installation, local repository configuration, dependency coordinates, common Maven commands, profile usage, multi‑module project setup, the difference between dependencyManagement and dependencies, and practical Maven tricks such as installing local jars, removing version numbers from generated artifacts, and resolving dependency conflicts.
Maven is a powerful dependency management tool that eliminates the need to manually add libraries in IDEs like Eclipse. Understanding its basic structure helps beginners feel confident when working on Java web projects.
1. Download Maven custom configuration
1.1 Configure local Maven
After downloading Maven, set the PATH environment variable to point to the bin directory so you can run Maven from the terminal. The default configuration file is settings.xml, which you can modify, for example to change the local repository path.
Local repository address: ${user.home}/.m2/repository (on macOS: /Users/linshidream/.m2/repository).
Remote repository mirror: Alibaba Cloud mirror is recommended.
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>2. Maven dependency coordinates
Each dependency is identified by groupId, artifactId and version, allowing easy version changes and automatic download from remote repositories.
<dependency>
<groupId>com.example</groupId>
<artifactId>my-module</artifactId>
<version>1.0.0</version>
</dependency>3. Common Maven commands
mvn -version– view Maven version information. mvn clean – delete compiled files. mvn compile – compile source code and generate the target directory. mvn package – create a JAR (without installing it to the local repository). mvn install – package and install the JAR into the local repository.
Additional options: -D<property>=<value> to pass parameters (e.g., -Dmaven.skip.test=true) and -P<profileId> to activate specific profiles.
4. Using Maven in IDAE
To integrate Maven into IDAE, configure three items:
Maven home path.
User settings file.
Local repository path.
5. Creating a multi‑module project
A typical structure includes a parent pom.xml and several sub‑modules, each with its own src, *.iml and pom.xml. The parent pom can define common dependencyManagement to centralize version control.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
...
</dependencies>
</dependencyManagement>Modules inherit the groupId and artifactId from the parent; they only need to declare the dependencies without specifying versions.
6. Maven usage tricks
Install a local JAR into the repository:
mvn install:install-file -Dfile=openbank-api-sdk-4.0.4.jar -DgroupId=com.cmbc.openbank -DartifactId=openbank-api-sdk -Dversion=4.0.4 -Dpackaging=jarRemove version numbers from generated JARs by setting <finalName>${artifactId}</finalName> in the spring-boot-maven-plugin configuration.
Resolve dependency conflicts using mvn dependency:tree and excluding transitive dependencies in <exclusions>.
If a dependency cannot be resolved, delete the corrupted artifact from the local repository and run Maven again to re‑download it.
Lin is Dream
Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.
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.
