Search code examples
mavendependenciesmaven-3maven-assembly-plugin

Maven multiple module dependency build


I am trying setup this Maven build for a project and want some help to get the dependency working.

Project
   |--pom.xml
   |--environments
        |--pom.xml
   |--configuration
        |--pom.xml
        |--idm
               |--pom.xml
   |--other
        |--pom.xml
        |--iam-scripts
               |--pom.xml

pom.xml for idm module is below:

<?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>

    <parent>
        <groupId>saas</groupId>
        <artifactId>environments</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../../environments</relativePath> 
    </parent>   

    <groupId>configuration</groupId>
    <artifactId>idm</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>${basedir}/modules/iam3/files</directory>
                            <includes>
                                <include>iam-scripts.tgz</include>
                            </includes>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-ext-artifacts</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>other</groupId>
                                    <artifactId>iam-scripts</artifactId>
                                    <version>1.0.0-SNAPSHOT</version>
                                    <type>tar.gz</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${basedir}/modules/iam3/files</outputDirectory>
                                    <destFileName>iam-scripts.tgz</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${basedir}/modules</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.conf</include>
                    <include>**/*.yaml</include>
                    <include>**/*.varfile</include>
                    <include>**/*.sh</include>
                    <include>**/*.html</include>
                    <include>**/*.tmpl</include>
                    <include>**/*.tmpl.html</include>
                    <include>**/*.groovy</include>
                    <include>**/*.erb</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/modules</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.sql</include>
                    <include>**/*.service</include>
                    <include>**/*.war</include>
                    <include>**/*.groovy</include>
                    <include>**/*.gitkeep</include>
                    <include>**/*.zip</include>
                    <include>**/*.tar.gz</include>
                    <include>**/*.tgz</include>
                    <include>**/*.jar</include>
                    <include>**/*.pp</include>
                    <include>**/*.so</include>
                    <include>**/*.rpm</include>
                    <include>**/*.erb</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

pom.xml for iam-scripts module is below:

<?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>saas</groupId>
        <artifactId>environments</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../../environments</relativePath>
    </parent>

    <groupId>other</groupId>
    <artifactId>iam-scripts</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>
                            src/assembly/conf.xml
                        </descriptor>
                    </descriptors>
                    <finalName>${project.artifactId}</finalName>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${basedir}/src/conf</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                    <include>**/*.conf</include>
                    <include>**/*.yaml</include>
                    <include>**/*.varfile</include>
                    <include>**/*.sh</include>
                    <include>**/*.html</include>
                    <include>**/*.tmpl</include>
                    <include>**/*.tmpl.html</include>
                    <include>**/*.groovy*</include>
                    <include>**/*.rptdesign</include>
                    <include>**/*.cps</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/conf</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.sql</include>
                    <include>**/*.service</include>
                    <include>**/*.war</include>
                    <include>**/*.gitkeep</include>
                    <include>**/*.zip</include>
                    <include>**/*.tar.gz</include>
                    <include>**/*.tgz</include>
                    <include>**/*.jar</include>
                    <include>**/*.pp</include>
                    <include>**/*.so</include>
                    <include>**/*.rpm</include>
                    <include>**/*.erb</include>
                    <include>**/*.md</include>
                    <include>**/*.js</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

So what I need to do is from idm module I need to call idm-scripts module build. Currently (as shown in the pom.xml) all it does is copying the iam-scripts.tgz from the repository. But say I change something within iam-scripts, instead of doing a manual clean build of iam-scripts I want to do the build from within idm module.

So steps I do now are:

  • within iam-scripts, i do mvn clean install -Denv=xxx
  • and then within idm, i do mvn clean install -Denv=xxx

How do I get idm build to call a clean build of iam-scripts?


Solution

  • Let me ask a question about your POM structure first:

    What are ...

    • configuration/pom.xml and
    • other/pom.xml

    ... good for when both ...

    • idm/pom.xml and
    • iam-scripts/pom.xml

    ... have environments/pom.xml as their parent? Such leaving them out when building idm or iam-scripts anyway. (BTW, I personally consider cross referencing projects via relativePath across multiple levels of directories unclear and I'm avoiding such.)

    In my opinion calling a project from within another doesn't fit into the declarative nature of POMs. Though there might be a plugin I'm not aware of that achieves that. (Oh yes, I am aware of one: GMaven[Plus]. Though I'd not recommend it here.)

    To build idm and iam-scripts at once I'd use a common pom packaged parent POM for them (maybe your Project can be used for that) and add them as <module>s therein.

    See