'Compiling multi module maven projects without installing to local repository
I have a compiling (and packaging) problem in my multi-module maven project. Problem occurs when compiling one of two projects which are siblings and included in a parent project and also one is dependent on the other. For simplicity I am sketching project folder structure and pom.xml
contents here.
folder structure
-parent
|
|-child1
| |-src
| | |-main
| | |-java
| | |-Child1Class.java
| |-pom.xml
|
|-child2
| |-src
| | |-main
| | |-java
| | |-Child2Class.java
| |-pom.xml
|
|-pom.xml
pom.xml of parent
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.project</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
</project>
pom.xml of child1
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.project</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example.project</groupId>
<artifactId>child1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>child1</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>child2</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
pom.xml of child2
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example.project</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example.project</groupId>
<artifactId>child2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>child2</name>
<dependencies>
</dependencies>
</project>
As seen, child2
is a dependency of child1
. child1
and child2
are developed in parallel. So after any source code changes in child2
, child1
should be compiled and deployed without any additional operations. But I can't succeed to compile child1
. After running mvn compile
command on child1
this error message is occured:
[ERROR] Failed to execute goal on project child1: Could not resolve dependencies for project com.example.project:child1: jar:1.0-SNAPSHOT: Could not find artifact com.example.project:child2:jar:1.0-SNAPSHOT -> [Help 1]
As I see, maven tries to search local repository for child2
. If I run mvn install
command on parent
project, both projects are added to local repository. Then any attempts to compile child1
is successful. This seems a solution but it is not convenient. What I want is to compile child1
while coding child1
and child2
without any installation to local repository.
Solution 1:[1]
try mvn compile -pl :child1 -am
, where -pl
are the projects you'd like to build and -am
tells maven to build the dependencies of these projects as well if they are part of the multimodule project.
Solution 2:[2]
Your mistake is to think maven will build the parent when building the child. It does not. Try building the parent and maven will automatically guess the build order.
C:\parent\child1> mvn clean package
[INFO] BUILD FAILURE
C:\parent\child1> cd ..
C:\parent> mvn clean package
[INFO] parent ............................................. SUCCESS [ 0.148 s]
[INFO] child2 ............................................. SUCCESS [ 1.209 s]
[INFO] child1 ............................................. SUCCESS [ 0.073 s]
[INFO] BUILD SUCCESS
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Robert Scholte |
Solution 2 | Kristof Neirynck |