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
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.
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.
You should refactor your projects.