代码之家  ›  专栏  ›  技术社区  ›  Gábor Lipták

maven antrun插件中的父属性

  •  6
  • Gábor Lipták  · 技术社区  · 14 年前

    <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
    

    1 回复  |  直到 8 年前
        1
  •  11
  •   Pascal Thivent    14 年前

    我不知道 确切地 为什么 ${project.parent.basedir} 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
    ...
    

    但我得说你想做什么( )是一个 坏习惯 ,模块应该是自包含的,而不是紧密耦合的。