7

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.

harun
  • 129
  • 3
  • 11

2 Answers2

17

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.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
3

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
Kristof Neirynck
  • 3,934
  • 1
  • 33
  • 47
  • Actually my project is a large project and I simplified to represent here. In real `child1` is a google app engine application and it has app engine maven plugin so I am able to upload it via running this plugin. But it requires `child2` to be located in local reository to build and update. I do not want to package or install parent project each time I run app engine update plugin. – harun Jan 22 '15 at 19:58
  • In that case go in the child2 directory and run `mvn clean install`. Then you can build child1 because child2 is in the local repository. Or if that is not what you want ... just `mvn package` the parent and let maven decide what needs to be compiled. – Kristof Neirynck Jan 22 '15 at 20:02