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.
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.
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.
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.
You should refactor your projects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With