Maven multiproject: sharing common files between subprojects

Are you using multiproject and want to use common files in all your subprojects?

Then you can define a variable “rootdir” pointing to the dir where common files are stored, so they can be refered as ${rootdir}/filename, both in your properties (project.properties, build.properties) and project.xml files.

To do so you just need to create a maven.xml in the top level project (supposing you have a LICENSE.txt in the top level dir, you can use any other file). This var will point to the same dir from every subproject.

<project
   xmlns:j="jelly:core"
   xmlns:ant="jelly:ant"
   xmlns:util="jelly:util"
   xmlns:maven="jelly:maven">

    <!-- =========== Set root dir =========== -->

    <j:set var="rootdir">${basedir}</j:set>

    <j:while test="${true}">
        <util:available file="${rootdir}/LICENSE.txt">
            <j:break/>
        </util:available>
        <j:set var="rootdir">${rootdir}/..</j:set>
    </j:while>

    <ant:dirname property="rootdirname" file="${rootdir}/project.xml"/>
    <j:set var="rootdir">${rootdirname}</j:set>
    <echo>Using root dir: ${rootdir}</echo>

</project>

You’d like to set some properties using this new var

# License plugin settings
maven.license.licenseFile=${rootdir}/LICENSE.txt

# multiproject
maven.multiproject.basedir=${rootdir}

2 thoughts on “Maven multiproject: sharing common files between subprojects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s