Finding the root directory of a multi module maven reactor project

前端 未结 13 1514
庸人自扰
庸人自扰 2020-12-02 11:11

I want to use the maven-dependency-plugin to copy artifacts from all sub-modules of my multi-module project to a directory that is relative to the root directory of the enti

相关标签:
13条回答
  • 2020-12-02 11:35

    I'm not aware of a "nice" way to find the root of a multi-module project. But you can maybe improve a bit your current approach.

    A first alternative would be to create an additional module directly under the root project, to declare all EARs as dependencies in it and to use dependency:copy-dependencies to copy the dependencies of the module to the to-deploy directory (relatively). Yes the path would still be relative but since the dependency plugin configuration would be centralized, I don't find it that annoying.

    A second alternative would be to use the Maven Assembly Plugin instead of the Maven Dependency Plugin to create a distribution using the dir format (this will create a distribution in a directory). This is actually what I would do.

    0 讨论(0)
  • 2020-12-02 11:37

    As others have suggested, directory-maven-plugin is the way to go. However, I found it works best with the 'directory-of' goal, as described here: https://stackoverflow.com/a/37965143/6498617.

    I prefer that as using highest-basedir didn't work for me with a multi-module project, with nested multi-module poms. The directory-of goal lets you set a property to the path of any module in the whole project, including the root of course. It is also way better than ${session.executionRootDirectory}, because it always works, regardless of whether you build the root or a sub-module, and irrespective of the current working directory where you mvn from.

    0 讨论(0)
  • 2020-12-02 11:41

    You can go with something like this. Note, that you have to define two profiles - one which will be activated for the root directory and one for children. Obviously this assumes children will have the same structure but this can be easily adapted.

        <profiles>
            <profile>
                <id>root-dir</id>
                <activation>
                    <file>
                        <exists>${basedir}/lib</exists>
                    </file>
                </activation>
                <properties>
                    <project.libdir>${basedir}/lib</project.libdir>
                </properties>
            </profile>
            <profile>
                <id>child-dir</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <project.libdir>${basedir}/../../lib</project.libdir>
                </properties>
            </profile>
        </profiles>
    
    0 讨论(0)
  • 2020-12-02 11:45

    I encountered similar problem as i needed to copy files between projects. What Maven does is logical because it will keep the pom.xml installed in repository away from hard coded value.

    My solution was to put copied dir in a Maven artifact, then employ Ant to extract/copy

    0 讨论(0)
  • 2020-12-02 11:48

    Something which I have used in my projects is to override the property in the sub-module poms.

        root:           <myproject.root>${basedir}</myproject.root>
        moduleA:        <myproject.root>${basedir}/..</myproject.root>
        other/moduleX:  <myproject.root>${basedir}/../..</myproject.root>
    

    This way you still have the relative paths, but you can define a plugin once in the root module, and your modules will inherit it with the right substitution for myproject.root.

    0 讨论(0)
  • 2020-12-02 11:52

    Since Maven 3.3.1, you can use ${maven.multiModuleProjectDirectory} for this purpose. (thanks to https://stackoverflow.com/a/48879554/302789)

    edit: this seems to only work properly when you have a .mvn folder at the root of your project.

    0 讨论(0)
提交回复
热议问题