Skip to content

Commit 2d51a28

Browse files
apanousisclaude
andcommitted
Initial commit: Maven training examples
This repository contains various Maven issue examples and their fixes for training purposes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
0 parents  commit 2d51a28

File tree

42 files changed

+2610
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2610
-0
lines changed

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Maven Issues Demonstration
2+
3+
This repository demonstrates the most common Maven issues with working examples and fixes.
4+
5+
## Issues Covered
6+
7+
### 1. [Wrong Scopes](issues/issue1-wrong-scopes/)
8+
- Test libraries in production JARs
9+
- Server-provided libraries included
10+
- Runtime dependencies at compile time
11+
- **Examples**: JUnit as compile, Servlet API missing provided scope
12+
13+
### 2. [Wrong Dependency Management](issues/issue2-dependency-management/)
14+
- Missing `<dependencyManagement>` section
15+
- Version conflicts between related libraries
16+
- Inconsistent versions across modules
17+
- **Examples**: Mixed Spring versions, missing SLF4J version
18+
19+
### 3. [Transitive Dependency Conflicts](issues/issue3-transitive-conflicts/)
20+
- Different libraries bringing conflicting versions
21+
- Runtime ClassNotFoundException errors
22+
- API breaking changes
23+
- **Examples**: SLF4J 1.7 vs 2.0, Guava version conflicts
24+
25+
### 5. [Duplicate Dependencies](issues/issue5-duplicate-deps/)
26+
- Same artifact with different versions
27+
- Multiple libraries doing same thing
28+
- Bloated classpath and JAR size
29+
- **Examples**: Multiple JSON libraries, logging implementations
30+
31+
### 6. [Plugin Version Management](issues/issue6-plugin-versions/)
32+
- Missing plugin versions (uses Maven defaults)
33+
- Outdated plugin versions
34+
- Inconsistent behavior across environments
35+
- **Examples**: Compiler plugin without version, old JAR plugin
36+
37+
### 8. [Circular Dependencies](issues/issue8-circular-deps/)
38+
- Modules depending on each other in circles
39+
- Build failures and compilation loops
40+
- Architecture problems
41+
- **Examples**: Module A ↔ Module B dependency cycle
42+
43+
### 11. [Parent POM Issues](issues/issue11-parent-pom/)
44+
- Direct dependencies forcing inheritance
45+
- Missing dependency/plugin management
46+
- Version conflicts between parent/children
47+
- **Examples**: Forced Spring inheritance, hardcoded Java 8
48+
49+
### 12. [Multi-module Coordination](issues/issue12-multi-module/)
50+
- Wrong module build order
51+
- Version drift between sibling modules
52+
- Hardcoded inter-module versions
53+
- **Examples**: Dependencies built before their dependencies
54+
55+
## How to Use
56+
57+
Each issue directory contains:
58+
- `pom.xml` - Problematic configuration
59+
- `pom-fixed.xml` or `fixed/` - Correct solution
60+
- `README.md` - Detailed explanation and commands
61+
62+
### Running Examples
63+
64+
```bash
65+
# Try to build the problematic examples (they will fail/conflict)
66+
cd issues/issue1-wrong-scopes && mvn clean compile
67+
68+
# Build the fixed versions (they work correctly)
69+
cd issues/issue1-wrong-scopes/fixed && mvn clean compile
70+
```
71+
72+
### Useful Maven Commands for Analysis
73+
74+
```bash
75+
# Show dependency tree
76+
mvn dependency:tree
77+
78+
# Find conflicts
79+
mvn dependency:analyze
80+
81+
# Check for updates
82+
mvn versions:display-dependency-updates
83+
mvn versions:display-plugin-updates
84+
85+
# Validate configuration
86+
mvn validate
87+
88+
# Show effective POM
89+
mvn help:effective-pom
90+
```
91+
92+
## Key Takeaways
93+
94+
1. **Use Dependency Management**: Central version control without forced inheritance
95+
2. **Correct Scopes**: test, provided, runtime, compile appropriately
96+
3. **Plugin Versions**: Always specify plugin versions explicitly
97+
4. **BOM Imports**: Use framework BOMs for consistent versions
98+
5. **Multi-module Order**: Build foundation modules first
99+
6. **Avoid Circles**: Design clear dependency hierarchies
100+
7. **Version Consistency**: Use properties and `${project.version}`
101+
102+
## Common Patterns for Fixes
103+
104+
- Replace `<dependencies>` with `<dependencyManagement>` in parent POMs
105+
- Add `<exclusions>` to resolve transitive conflicts
106+
- Use `${project.version}` for inter-module dependencies
107+
- Import BOMs with `<type>pom</type><scope>import</scope>`
108+
- Add `<pluginManagement>` for consistent plugin versions
109+
- Extract common interfaces to break circular dependencies

issue1-wrong-scopes/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Issue 1: Wrong Dependency Scopes
2+
3+
## Problem
4+
Dependencies have incorrect scopes, causing:
5+
- Test libraries included in production JAR (bloating size)
6+
- Server-provided libraries included (conflicts)
7+
- Runtime-only dependencies available at compile time
8+
9+
## Examples in pom.xml
10+
11+
### Wrong Scopes:
12+
1. **JUnit** - Missing scope (defaults to `compile`) → Should be `test`
13+
2. **Servlet API** - Missing scope → Should be `provided`
14+
3. **Mockito** - `compile` scope → Should be `test`
15+
4. **MySQL Driver** - `compile` scope → Should be `runtime`
16+
17+
## Fix
18+
```xml
19+
<!-- Correct scopes -->
20+
<dependency>
21+
<groupId>org.junit.jupiter</groupId>
22+
<artifactId>junit-jupiter</artifactId>
23+
<version>5.8.2</version>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>javax.servlet</groupId>
29+
<artifactId>javax.servlet-api</artifactId>
30+
<version>4.0.1</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
```
34+
35+
## Impact
36+
- **Bloated JARs**: Test libraries in production
37+
- **ClassPath conflicts**: Server vs bundled versions
38+
- **Security**: Unnecessary libraries exposed

issue1-wrong-scopes/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>wrong-scopes-demo</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<maven.compiler.source>11</maven.compiler.source>
15+
<maven.compiler.target>11</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
<!-- WRONG: JUnit should be test scope, not compile -->
20+
<dependency>
21+
<groupId>org.junit.jupiter</groupId>
22+
<artifactId>junit-jupiter</artifactId>
23+
<version>5.8.2</version>
24+
<!-- Missing scope - defaults to compile, wrong for test library -->
25+
</dependency>
26+
27+
<!-- WRONG: Servlet API should be provided scope (server provides it) -->
28+
<dependency>
29+
<groupId>javax.servlet</groupId>
30+
<artifactId>javax.servlet-api</artifactId>
31+
<version>4.0.1</version>
32+
<!-- Missing scope - defaults to compile, should be provided -->
33+
</dependency>
34+
35+
<!-- WRONG: Mockito should be test scope -->
36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<version>4.6.1</version>
40+
<scope>compile</scope> <!-- Explicitly wrong scope -->
41+
</dependency>
42+
43+
<!-- WRONG: JDBC driver should be runtime scope -->
44+
<dependency>
45+
<groupId>mysql</groupId>
46+
<artifactId>mysql-connector-java</artifactId>
47+
<version>8.0.29</version>
48+
<scope>compile</scope> <!-- Should be runtime -->
49+
</dependency>
50+
51+
<!-- CORRECT: Business logic dependency -->
52+
<dependency>
53+
<groupId>org.apache.commons</groupId>
54+
<artifactId>commons-lang3</artifactId>
55+
<version>3.12.0</version>
56+
<scope>compile</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>

issue11-parent-pom/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Issue 11: Parent POM Issues
2+
3+
## Problem
4+
Poorly designed parent POMs causing:
5+
- Forced inheritance of unwanted dependencies
6+
- Version conflicts between parent and children
7+
- Inflexible plugin configurations
8+
- Inconsistent project structure
9+
10+
## Issues in Parent POM
11+
12+
### Parent POM Problems:
13+
1. **Direct dependencies**: Forces ALL children to inherit Spring
14+
2. **Missing properties**: No encoding, Java version standardization
15+
3. **No dependency management**: Versions scattered across children
16+
4. **Direct plugins**: Hardcoded Java 8, inflexible configuration
17+
5. **Missing distribution management**: Where to deploy artifacts?
18+
19+
### Child POM Problems:
20+
1. **Missing relativePath**: Can't find parent reliably
21+
2. **Version inconsistencies**: child1 uses parent 1.0.0, child2 uses 1.0.1
22+
3. **Unmanaged dependencies**: commons-lang3 version in child, not parent
23+
4. **Dependency conflicts**: Spring 5.3.21 vs 5.2.0.RELEASE
24+
5. **Multiple test frameworks**: JUnit 4 + JUnit 5
25+
26+
### Build Issues:
27+
```bash
28+
# Children can't find parent
29+
mvn clean compile
30+
# [ERROR] Non-resolvable parent POM
31+
32+
# Version conflicts
33+
mvn dependency:tree
34+
# Shows conflicting Spring versions
35+
```
36+
37+
## Solution (see fixed/ directory)
38+
39+
### Proper Parent POM Structure:
40+
41+
#### 1. **Use dependencyManagement** (not dependencies):
42+
```xml
43+
<dependencyManagement>
44+
<dependencies>
45+
<!-- Children choose what they need -->
46+
<dependency>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-framework-bom</artifactId>
49+
<version>${spring.version}</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
```
56+
57+
#### 2. **Use pluginManagement** (not plugins):
58+
```xml
59+
<build>
60+
<pluginManagement>
61+
<plugins>
62+
<!-- Children can override if needed -->
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<version>${maven-compiler-plugin.version}</version>
67+
</plugin>
68+
</plugins>
69+
</pluginManagement>
70+
</build>
71+
```
72+
73+
#### 3. **Essential properties**:
74+
```xml
75+
<properties>
76+
<maven.compiler.source>11</maven.compiler.source>
77+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
78+
<spring.version>5.3.23</spring.version>
79+
</properties>
80+
```
81+
82+
#### 4. **Multi-module structure**:
83+
```xml
84+
<modules>
85+
<module>../child1</module>
86+
<module>../child2</module>
87+
</modules>
88+
```
89+
90+
### Benefits of Fixed Version:
91+
- **Optional inheritance**: Children choose what they need
92+
- **Consistent versions**: BOM imports prevent conflicts
93+
- **Flexibility**: Children can override when necessary
94+
- **Proper build order**: Multi-module builds work correctly
95+
- **Version management**: Centralized but not forced

issue11-parent-pom/child1/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<!-- WRONG: Incorrect parent reference -->
9+
<parent>
10+
<groupId>com.example.parent</groupId>
11+
<artifactId>parent-pom</artifactId>
12+
<version>1.0.0</version>
13+
<!-- MISSING: relativePath should point to ../parent/pom.xml -->
14+
</parent>
15+
16+
<artifactId>child1</artifactId>
17+
<!-- WRONG: Inherits parent version but should it? -->
18+
<!-- MISSING: Own version number -->
19+
20+
<!-- PROBLEM: Forced to inherit Spring dependency it doesn't need -->
21+
<!-- PROBLEM: Forced to use Java 8 when it needs Java 11 -->
22+
23+
<dependencies>
24+
<!-- Child needs different dependencies but gets parent ones too -->
25+
<dependency>
26+
<groupId>org.apache.commons</groupId>
27+
<artifactId>commons-lang3</artifactId>
28+
<version>3.12.0</version> <!-- Should be managed by parent -->
29+
</dependency>
30+
31+
<!-- WRONG: Duplicating parent's test dependency -->
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.13.2</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<!-- Child cannot override parent's compiler plugin easily -->
41+
</project>

issue11-parent-pom/child2/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<!-- WRONG: Different parent version! -->
9+
<parent>
10+
<groupId>com.example.parent</groupId>
11+
<artifactId>parent-pom</artifactId>
12+
<version>1.0.1</version> <!-- Different version than child1! -->
13+
<relativePath>../parent/pom.xml</relativePath>
14+
</parent>
15+
16+
<artifactId>child2</artifactId>
17+
<version>2.0.0</version> <!-- INCONSISTENT: Different versioning strategy -->
18+
19+
<dependencies>
20+
<!-- FORCED: Gets Spring dependency it doesn't want -->
21+
<!-- spring-core inherited from parent -->
22+
23+
<!-- Child2 needs web features but parent doesn't provide version management -->
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-web</artifactId>
27+
<version>5.2.0.RELEASE</version> <!-- CONFLICT: Different Spring version! -->
28+
</dependency>
29+
30+
<!-- REDUNDANT: Gets JUnit 4 from parent but needs JUnit 5 -->
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter</artifactId>
34+
<version>5.8.2</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<!-- Now has both JUnit 4 and 5! -->
38+
</dependencies>
39+
40+
<!-- PROBLEM: Stuck with Java 8 compiler from parent -->
41+
<!-- But this module needs newer Java features -->
42+
</project>

0 commit comments

Comments
 (0)