Maven Antrun插件中的父属性 [英] Parent properties inside maven antrun plugin

查看:144
本文介绍了Maven Antrun插件中的父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个多模块项目.在孩子里面,我需要做一些复杂的事情(与部署到应用程序服务器的集成测试等等).因此,有一个Integrationtest子级,从这个模块中,我需要父级的根才能访问其他模块.我不想使用"..". Integrationtest POM中有一个属性:

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

还有一个具有以下内容的antrun插件:

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

在输出中,main.basedir无法解析:

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

在变得非常生气之后,我决定问你如何解决这个问题...

解决方案

我完全不知道 为什么${project.parent.basedir}不能从AntRun中获得",也许它不受支持(请参见 http://jira.codehaus.org/browse/MNG-3597 ).). >

这是使用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>

我不为此感到骄傲,但是它有点奏效"(如果您可以使用基于父对象的路径的字符串表示形式):

$ 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
...

但是我要说的是,您要做的事情(该模块需要父级的根才能到达其他模块)是一种不好的做法,模块应该是自给自足的,而不是紧密耦合的.

我不建议使用我发布的内容:)

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

解决方案

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 :)

这篇关于Maven Antrun插件中的父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆