6

I have project with several dependencies on other project.

<?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>group1</groupId>
<artifactId>artifact1<artifactId>
<name>RealtyRegistry</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>group1</groupId>
        <artifactId>artifact2</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>group1</groupId>
        <artifactId>artifact3</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

All of them developed by me simultaniously. I add edition to files of all of project and i need to build main project together with dependent ones. How to do that for projects without tree structure?

There can be 2 or more covering trees for projects hierachy, for example: A depends on B,C; D depends on C,E; A and D are independent.

rolve
  • 10,083
  • 4
  • 55
  • 75
Vyacheslav
  • 3,134
  • 8
  • 28
  • 42

1 Answers1

6

You can build multiple projects together using "Modules". Normally, you would do this by creating a "mother" project with <packaging>pom</packaging> and adding your real project as modules using the <modules> tag. Then, when you build the "mother" project, all modules are automatically built in the right order.

Here is an example from the Maven by Example book:

<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
    <module>simple-weather</module>
    <module>simple-webapp</module>
</modules>

Note that this requires you to have your modules in subfolders that are named accordingly. For example, you would have the "mother" pom in some folder:

/.../my-project/

and the modules in:

/.../my-project/simple-weather/
/.../my-project/simple-webapp/

For more information, read Chapter 6. A Multi-module Project of the book, it's freely available on the Sonatype website.

rolve
  • 10,083
  • 4
  • 55
  • 75
  • The thing is my maven projects does not form a tree to put them in sub folders. There can be 2 or more covering trees for projects hierachy, for example: A depends on B,C; D depends on C,E; A and D are independent. – Vyacheslav Nov 02 '12 at 14:04
  • specify these dependencies within the dependent's module pom. – Will Nov 02 '12 at 15:25
  • @Vyacheslav There's a misunderstanding here: The module system has nothing to do with dependencies. Your projects may have arbitrary dependencies between each other. The only new thing is that you declare them as modules of the main project and put them in subfolders next to each other. – rolve Nov 03 '12 at 08:36
  • Ok. but I need: A to build B,C; D to build C,E. So how to subfolder them then? – Vyacheslav Nov 05 '12 at 05:18
  • Look. Make a parent folder: `.../my-project/`. In there, place the parent pom file with a modules part like above. Also, make 5 subfolders: `.../my-project/A`, `.../my-project/B`, `.../my-project/C`, `.../my-project/D` & `.../my-project/E` and place your projects in there. Every project needs to have its dependencies declared like normal. You don't have to change anything if this already works. Now **all you have to do is to build the parent-project** and Maven will figure out the order in which to build them. – rolve Nov 05 '12 at 09:12
  • But I need to build A without building D and D without building A. – Vyacheslav Nov 06 '12 at 14:19
  • That's not what Maven is made for. Maven is (single-)project-based. In general, the only connection between two (depending) projects is the groupId-artifactId-version information. Modules are a convenient exception to this, but in principle you're still building a single project (the parent project). So either live with the fact that you have to build all of them together or write a small shell or batch script for Pete's sake. – rolve Nov 06 '12 at 15:55