Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use Maven to share source code for two projects?

Tags:

maven-2

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.

like image 374
Adam L Davis Avatar asked Aug 02 '10 17:08

Adam L Davis


People also ask

How do I share resources between projects in Maven?

There are a few ways to share resources across multiple projects or modules: Cut and paste them. Use Assembly and Dependency plugins. Use the maven-remote-resources-plugin.

Does Maven support multi project build?

Maven's Multi-Module Project The submodules are regular Maven projects, and they can be built separately or through the aggregator POM. By building the project through the aggregator POM, each project that has a packaging type different from pom will result in a built archive file.

How do I merge two Maven projects?

let's assume that you have two maven projects A and B. Now create a new project C in your intllij-idea. In project C, create a new module A1. Copy the dependencies and build, properties (if present) from pom.


2 Answers

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
like image 187
crowne Avatar answered Oct 02 '22 05:10

crowne


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.

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">  
  <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">  
  <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.

like image 36
Tushar Tarkas Avatar answered Oct 02 '22 06:10

Tushar Tarkas