This extends Part 1: Creating an empty multi module maven project structure.
Dependency Management allows you to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children. The dependencyManagement control the versions of artifacts used in transitive dependencies.
So, add this to the parent pom.xml file under simple-app-parent/pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simple-app-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>simple-app-parent</name> <modules> <module>simple-app-service</module> <module>simple-app-endpoint</module> </modules> <properties> <commons-lang.version>3.1</commons-lang.version> <commons-collections.version>3.2.1</commons-collections.version> </properties> <dependencyManagement> <dependencies> <!-- Commons Dependencies --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>${commons-collections.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- common to all child modules --> <!-- child modules can override --> <dependencies> <!-- Commons Dependencies --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project> |
In your parent pom, in addition “dependencyManagement/dependencies” still you need to add “dependencies” as “dependencyManagement” is there to share the versions across child modules.
Now, if you build the war project with “mvn clean package” or “mvn clean install” you will have
Now let’s do 2 things in child pom “simple-app-endpoint/pom.xml”:
1) override “commons-collections” artifact version. The parent version is “3.2.1” and the child version is “3.2.1”.
2) let’s give the built war file a version like “simple-app-endpoint-1.0-SNAPSHOT.war” by modifying the “buld/finalName” tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.mytutorial</groupId> <artifactId>simple-app-endpoint</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>simple-app-endpoint Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mytutorial</groupId> <artifactId>simple-app-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- overriding parent version --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.1</version> </dependency> </dependencies> <build> <finalName>simple-app-endpoint-${project.version}</finalName> </build> </project> |