Master Maven: Complete Guide to Repositories, Dependencies, and Lifecycle
This comprehensive tutorial explains Maven's repository types, dependency management, conflict resolution strategies, best practices for adding dependencies, standard directory layout, lifecycle phases, and scope definitions, helping Java developers confidently master Maven for multi‑module projects.
In modern Java web development Maven is ubiquitous, offering repository management, dependency handling, inheritance, and aggregation to simplify project builds; understanding Maven is essential to avoid painful multi‑module issues and dependency conflicts.
1. Local Repository? What repositories does Maven have and how do they relate?
Maven uses three repository levels:
Local repository : a cache on the developer's machine where downloaded JARs are stored.
Private (internal) repository : a company‑wide server that mirrors the central repository and hosts internal artifacts.
Central repository : the public Maven repository at http://repo1.maven.org/maven2/ maintained by the Maven team.
Local repository path can be configured in settings.xml:
2. Using <dependency>
Dependencies are identified by three coordinates: groupId , artifactId , and version . You can search for these coordinates on private repositories, search.maven.org, or mvnrepository.com.
3. Why do dependency conflicts still occur and how to resolve them?
Only one version of a given groupId:artifactId can be used at runtime. Maven selects the version that is nearest to the project in the dependency tree (the "nearest‑first" strategy). Conflicts arise when transitive dependencies bring different versions.
Common resolution techniques:
Use <dependencyManagement> to enforce a single version across modules.
Exclude unwanted transitive dependencies with <exclusions>.
Declare the desired version explicitly with a direct <dependency> entry.
4. Best practices for adding dependencies early
Before adding a new dependency, run mvn dependency:tree to view its transitive dependencies and detect version conflicts, then apply the strategies above.
5. Maven’s standardized directory layout
Key points:
src/main contains production code and resources that are packaged into the final JAR/WAR.
src/test holds test code, which is not packaged.
src/main/resources files are copied to the output directory automatically.
6. Maven lifecycle
Important phases:
clean : removes previous build artifacts.
package : compiles and packages the code into a JAR or WAR.
install : installs the built artifact into the local repository.
deploy : uploads the artifact to a remote repository.
7. Dependency scopes
Scopes control when a dependency is available:
compile (default): needed at compile‑time and packaged.
provided : needed at compile‑time but supplied by the runtime container; not packaged.
runtime : not needed for compilation but required at execution.
test : only used for testing; not packaged.
system : external JARs referenced by an absolute path; rarely used.
Original source: https://www.jianshu.com/p/20b39ab6a88c
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack 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.
