Java – compile a multi module Maven project without installing it into a local repository
There is a compilation (and packaging) problem in my multi module Maven project There was a problem compiling one of the two projects, which are sibling projects and contained in the parent project, and the other project depends on the other project For simplicity, I sketch the project folder structure and POM XML content
Folder structure
-parent | |-child1 | |-src | | |-main | | |-java | | |-Child1Class.java | |-pom.xml | |-child2 | |-src | | |-main | | |-java | | |-Child2Class.java | |-pom.xml | |-pom.xml
Parents' POM xml
<?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 of Child1 xml
<?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 of child2 xml
<?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 shown in the figure, child2 is a dependency of Child1 Child1 and child2 are developed in parallel Therefore, after any source code changes in child2, Child1 should be compiled and deployed without any other operations But I can't successfully compile Child1 This error message appears after running the MVN compile command on Child1:
[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've seen, Maven tries to search the local repository for child2 If I run the MVN install command on the parent project, both projects will be added to the local repository Then any attempt to compile Child1 is successful This seems like a solution, but it's inconvenient What I want is to compile Child1 and write Child1 and child2 at the same time without installing into the local repository
Solution
Try MVN compile - pl: Child1 - am, where - PL is the project you want to build, and - am tells Maven to build the dependencies of these projects if they are part of a multi module project
