Simplify project configuration. Separate java source code from WAR
to JAR
. After this you can simply add this JAR
to another WAR
as dependency. Merging java code in this case is not good idea.
For merging resources from WAR
use maven overlays.
Sample solution
* test-parent.pom
* test-core (test-core.jar) - shared code
* test-web-parent (test-web-parent.pom) - first project
* core (test-web-core.jar)
* web (test-web.war)
* test-web-2-parent (test-web-2-parent.pom) - second project
* core (test-web-2-core.jar)
* web (test-web-2.war)
test-web.war
have dependencies:
- test-web-core.jar
- test-core.jar
test-web-2.war
have dependencies:
- test-web-2-core.jar
- test-core.jar
Configuration
test-parent.pom
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-parent</groupId>
<artifactId>test-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>test-core</module>
<module>test-web-parent</module>
<module>test-web-2-parent</module>
</modules>
</project>
test-core.jar
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-core</artifactId>
</project>
test-web-parent.pom
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web-parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>web</module>
</modules>
</project>
test-web-core.jar
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-web-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web-core</artifactId>
<dependencies>
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
test-web.war
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-web-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-web-core</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
test-web-2-parent.pom
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web-2-parent</artifactId>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>web</module>
</modules>
</project>
test-web-2-core.jar
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-web-2-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web-2-core</artifactId>
<dependencies>
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
test-web-2.war
<?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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>test-parent</groupId>
<artifactId>test-web-2-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-web-2</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-web-2-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--first war-->
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-web</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>runtime</scope>
<type>war</type>
</dependency>
<dependency>
<groupId>test-parent</groupId>
<artifactId>test-web-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>