tags:

views:

35

answers:

3

I have a large Java Web Application project using Maven and I need to start a new project that will share most of the same code (so I don't have to repeat work), but not all of it. I'm going to copy the shared code into a new project (let's call it "root"). How do I make my original Project depend on root for source code? I can't just jar it because I want to change the source before compiling.

+1  A: 

Create a maven project which contains all your shared code. Keep packaging of this project (in the main pom.xml) as jar. This would help make this project kind of library for your usage.

In all the projects which access the shared code, add dependency for this project according to your needs. (compile, provided).

Now package and install the shared project before you build any of the dependent projects. This will add the shared project to your local repository which can be then used by your dependent projects.

Adding sample pom.xml for shared and dependent projects.

  1. Shared project pom.

<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"&gt;
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>com.myspace.test</artifactId>
<groupId>com.myspace</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.myspace</groupId>
<artifactId>shared</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shared-module</name>
<description>shared module which contains code shared by other modules.</description>
</project>

  • Dependent project's pom.

    <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"&gt;
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>com.myspace.test</artifactId>
    <groupId>com.myspace</groupId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.myspace</groupId>
    <artifactId>dependent-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dependent-module</name>
    <description>Dependent module.</description>
    <dependencies>
    <dependency>
    <groupId>com.myspace</groupId>
    <artifactId>shared</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <scope>provided</scope>
    </dependency>
    </dependencies>
    </project>

Parent project can be added optionally in case such organization is required. Hope this helps.

Tushar Tarkas
A: 

Install the shared code, using your current packaging setting of jar:

mvn install

Then, you can create a dependency in the child pom.xml (the code that is not shared) from whatever repository you are installing to.

This is good practice in general not only to avoid repeating work but also in case you want to change the implementation of the shared resources. You can change the logic in one place, install it to the repository, and other projects that depend on that code will use the new code the next time they are compiled.

JCD
A: 

You should refactor your projects.

  1. Identify the common code
  2. Extract that into its own maven module
    2.1. usually web-apps are multi module, so if you are going to share the common library across two web-apps, then separate the common library out into its own group-id
  3. Build and install the jar file into your repository
  4. change the poms of the web-apps to depend on your new library
crowne