Monday, November 5, 2012

How to use Maven Module, Example from Spring Batch


I have tried to use maven module for related project set up in once. but, I was not able to find good sample or answer for me. So I opened up well known open source project source files who uses maven. I found one from Spring Batch.

Benefit for using maven module :
I can manage related dependencies version number in once
Several projects can be setup once in your IDE.
Your IDE (ex: eclipse ) will look the module project which applies your latest changes between the projects.
My team member will get all these benefit. Also I can save time to help team member.

It is little long to follow, so, I will explain as simple as possible for easy understanding.

Directory Structure

/juno_workspace/SpringSource-spring-batch-326da17$ find ./ -name 'pom.xml'
./pom.xml
./spring-batch-parent/pom.xml
./spring-batch-core/pom.xml
./spring-batch-infrastructure/pom.xml


./pom.xml file


The point from this file is, "packaging" is set to "pom" and defines "modules"
<?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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework.batch</groupId>
 <artifactId>spring-batch</artifactId>
 <name>Spring Batch</name>
 <description>Spring Batch provides tools for enterprise batch or bulk processing. It can be used to wire up jobs, and track
 their execution, or simply as an optimization for repetitive processing in a transactional environment. Spring Batch is part of the Spring Portfolio.</description>
 <version>2.2.0.BUILD-SNAPSHOT</version>
 <packaging>pom</packaging>
 <modules>
  <module>spring-batch-parent</module>
  <module>spring-batch-infrastructure</module>
  <module>spring-batch-core</module>
  <module>spring-batch-test</module>
 </modules>


./spring-batch-parent/pom.xml file


The point from this file is, "packaging" is set to "pom" and defines "dependencyManagement"
<?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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.springframework.batch</groupId>
 <artifactId>spring-batch-parent</artifactId>
 <version>2.2.0.BUILD-SNAPSHOT</version>
 <name>Spring Batch Parent</name>
 <description>Spring Batch parent project.  Defines dependencies and common configuration for the build process.</description>
 <url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
 <packaging>pom</packaging>
...

 <dependencyManagement>
  <dependencies>
...
   <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.5.4</version>
   </dependency>
...



./spring-batch-core/pom.xml file


The point from this file is, "packaging" is set to "jar" and defines parent
<?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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <artifactId>spring-batch-infrastructure</artifactId>
 <packaging>jar</packaging>
 <name>Infrastructure</name>
 <description><![CDATA[The Spring Batch Infrastructure is a set of 
 low-level components, interfaces and tools for batch processing 
 applications and optimisations.]]>
 </description>
 <url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
 <parent>
  <groupId>org.springframework.batch</groupId>
  <artifactId>spring-batch-parent</artifactId>
  <version>2.2.0.BUILD-SNAPSHOT</version>
  <relativePath>../spring-batch-parent</relativePath>
 </parent>
...
 <dependencies>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <optional>true</optional>
  </dependency>
...


./spring-batch-infrastructure/pom.xml file


The point from this file is, "packaging" is set to "jar" and defines parent
<?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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <artifactId>spring-batch-core</artifactId>
 <packaging>jar</packaging>
 <name>Core</name>
 <description>Core domain for batch processing, expressing a domain of Jobs, Steps, Chunks, etc.</description>
 <url>http://static.springframework.org/spring-batch/${project.artifactId}</url>
 <parent>
  <groupId>org.springframework.batch</groupId>
  <artifactId>spring-batch-parent</artifactId>
  <version>2.2.0.BUILD-SNAPSHOT</version>
  <relativePath>../spring-batch-parent</relativePath>
 </parent>
...
 <dependencies>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <optional>true</optional>
  </dependency>
...


Additionally,


You can see, there is no version number described for "aspectjweaver" on "./spring-batch-core/pom.xml" and "./spring-batch-infrastructure/pom.xml" file. because the version number is described on "./spring-batch-parent/pom.xml" file in "dependencyManagement". That you can manage version number in once. It will be great to manage version number like Junit or common libraries

you can run mvn eclipse:eclipse and import the project on your eclipse. I will post detail how to import project for the next.

No comments:

Post a Comment