Parent properties inside maven antrun plugin
Asked Answered
F

2

7

There is a multi-module project. Inside the child I need to do some complicated stuff (integration test with deploying to application server and so on). So there is an integrationtest child, and from this module I need the root of the parent to reach other modules. I do not want to use "..". There is a property in integrationtest POM:

<properties>
 <main.basedir>${project.parent.basedir}</main.basedir>
    ...
</properties>

And there is an antrun plugin with the following content:

<plugins>
 <plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
   <execution>
    <id>render-parameter-sql</id>
    <phase>validate</phase>
    <goals>
     <goal>run</goal>
    </goals>
    <configuration>
     <tasks>
      <echoproperties/>
     </tasks>
    </configuration>
   </execution>
  </executions>
 </plugin>
</plugins>

In the output, the main.basedir is not resolved:

main:
[echoproperties] #Ant properties
[echoproperties] #Thu Oct 28 09:32:13 CEST 2010
[echoproperties] ant.core.lib=C\:\\Users\\gaborl\\.m2\\repository\\org\\apache\\ant\\ant\\1.8.1\\ant-1.8.1.jar
...
[echoproperties] main.basedir=${project.parent.basedir}
[echoproperties] maven.dependency.antlr.antlr.jar.path=C\:\\Users\\gaborl\\.m2\\repository\\antlr\\antlr\\2.7.6\\antlr-2.7.6.jar

After becoming really angry I decided to ask you how to get around this...

Fatal answered 28/10, 2010 at 7:41 Comment(0)
U
11

I don't know exactly why the ${project.parent.basedir} is not "available" from AntRun, maybe it's just not supported (see http://jira.codehaus.org/browse/MNG-3597).

Here is an horrible workaround using gmaven:

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>set-custom-property</id>
      <phase>validate</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          project.properties.setProperty('main.basedir', project.parent.basedir.toString())
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>render-parameter-sql</id>
      <phase>validate</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <target>
          <echo>project.artifactId        : ${project.artifactId}</echo>
          <echo>project.parent.basedir    : ${project.parent.basedir}</echo>
          <echo>main.basedir              : ${main.basedir}</echo>
          <echo>project.basedir           : ${project.basedir}</echo>
          <echo>project.build.directory   : ${project.build.directory}</echo>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

I'm not proud of it, but it kinda "works" (if a string representation of the path to the parent basedir is ok for you):

$ mvn validate
[INFO] Scanning for projects...
...
[INFO] --- maven-antrun-plugin:1.6:run (render-parameter-sql) @ Q4040778 ---
[INFO] Executing tasks

main:
     [echo] project.artifactId        : Q4040778
     [echo] project.parent.basedir    : ${project.parent.basedir}
     [echo] main.basedir              : /home/pascal/Projects/stackoverflow
     [echo] project.basedir           : /home/pascal/Projects/stackoverflow/Q4040778
     [echo] project.build.directory   : /home/pascal/Projects/stackoverflow/Q4040778/target
[INFO] Executed tasks
...

But I need to say that what you want to do (from this module I need the root of the parent to reach other modules) is a bad practice, modules should be self contained and not tightly coupled.

I do not recommend using what I posted :)

Utu answered 28/10, 2010 at 22:58 Comment(0)
G
0

Have you thought about using the maven-resources-plugin? I had to copy jacoco generated files in a multi-module project from the submodule (which aggregated the jacoco file from all other submodules) to the parent target folder and following worked nicely while adding it to the submodules pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
      <outputDirectory>${project.parent.basedir}/target/site/jacoco-aggregate</outputDirectory>
      <resources>
        <resource>
          <directory>${project.basedir}/target/site/jacoco-aggregate</directory>
        </resource>
      </resources>
    </configuration>
    <executions>
      <execution>
        <id>copy-resources</id>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <phase>install</phase>
      </execution>
    </executions>
  </plugin>
General answered 14/12, 2023 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.