how to get the super pom basedir in a child module pom?

五迷三道 提交于 2019-12-03 05:25:51
Ricardo Silva

In this case, at first you could try ${project.parent.basedir}.
As it seems it doesn't work, the simple(and native) way is use complete path (/root/...) or try relative path (../) instead of using ${basedir} variable.

But for me, a great solution would be externalize this configuration into a properties file.
You can use properties-maven-plugin ( http://mojo.codehaus.org/properties-maven-plugin/plugin-info.html ).

With this plugin, properties defined on the properties file can be read just like properties defined inside pom.xml.

From the plugin site:

If you have a properties file called teams.properties with this content:

toronto=raptors
miami=heat

Would be the same as declaring the following in your pom.xml:

<properties> 
  <toronto>raptors</toronto>
  <miami>heat</miami>
</properties>

${project.parent.basedir} should do the job.

Or you can set the basedir-path of the root in a property, so it will be inherited. Something like this in the Parent

<properties>
  <rootPath>${basedir}</rootPath>
</properties>

And in the Child

<repository>
  <id>my-local-repo</id>
  <url>file://${rootPath}/repository</url>
</repository>

I solved this many times with groovy plugin. Add a file called "basepath_marker" to your super pom's directory and add the following to your pom. You can access the property like this: ${base-path}. Read this blog post for more details.

Example:

 ...
 <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <executions>
                <!-- set absolute base path from super pom -->
                <execution>
                    <id>find-basepath</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
                            <![CDATA[
                                import java.io.File;
                                log.info('## define projects super pom absolute path through basepath_marker')
                                String p = "basepath_marker";
                                File f = null;
                                if( p != null ) {
                                    def _max_child_poms = 0
                                    while( _max_child_poms++ < 5 ) {
                                        f = new File( p );
                                        if( f.exists() ) {
                                            break;
                                        }   
                                        p = "../" + p;                                 
                                    }
                                }
                                if( f != null ) {
                                    String basePath = f.getCanonicalPath();
                                    basePath = basePath.substring( 0, basePath.lastIndexOf( File.separator ) ); 
                                    project.properties['base-path'] = basePath.replace( '\\' , '/');
                                    log.info(' - used base path = ' + project.properties['base-path'] );
                                } else {
                                    log.error( 'Could not find basepath_marker marker file!' );
                                    System.stop( 0 );
                                }
                            ]]>
                        </source>
                    </configuration>
                </execution>                    
            </executions>
        </plugin>
    </plugins>
</build>
 ...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!