6

pom.xml の gmaven プラグインを介して外部の groovy スクリプトを実行しています。外部スクリプトは「myscript.groovy」と言います。

maven pom.xml [つまり、プラグイン 'gmaven-plugin' 実行内] を介して myscript.groovy にいくつかのパラメーター/引数を提供したい。しかし、そうすることができません..

で使ってみました; しかし、groovy スクリプトでその値を取得する方法がわかりません。properties.get を呼び出すだけでは、プロパティ値は得られません。

pom ファイルのスナップ:

<plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-resources-execute-groovyscript</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <properties>
                                    <property>
                                        <name>installation.dir</name>
                                        <value>${installation.dir}</value>
                                    </property>
                                </properties>
                                <source>${pom.basedir}/src/main/groovy/configure.groovy</source>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

「configure.groovy」スクリプトで「installation.dir」プロパティーの値を取得する方法がわかりません。

この点に関するヒントは役に立ちます..ありがとう

4

1 に答える 1

7

プロパティをバインドおよび取得するには、2 つの方法があります。1 つは、プラグイン固有のプロパティによるものです。

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-resources-execute-groovyscript</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <properties>
          <installation.dir>${installation.dir}</installation.dir>
        </properties>
        <source>${pom.basedir}/src/main/groovy/configure.groovy</source>
      </configuration>
    </execution>
  </executions>
</plugin>

これらは、 のようなスクリプトで取得されますproject.properties['installation.dir']

GMaven はもうメンテナンスされていません (私が最後のメンテナでした)。2.0.0 よりも新しいバージョンの Groovy を使用する場合は、GMavenPlusを参照してください。同等のPOMは次のとおりです。

<plugin>
  <groupId>org.codehaus.gmavenplus</groupId>
  <artifactId>gmavenplus-plugin</artifactId>
  <version>1.5</version>
  <executions>
    <execution>
      <goals>
        <goal>execute</goal>
      </goals>
      <phase>generate-resources</phase>
    </execution>
  </executions>
  <configuration>
    <bindPropertiesToSeparateVariables>false</bindPropertiesToSeparateVariables>
    <properties>
      <property>
        <name>installation.dir</name>
        <value>${installation.dir}</value>
      </property>
    </properties>
    <scripts>
      <script>file:///${pom.basedir}/src/main/groovy/configure.groovy</script>
    </scripts>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.3</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</plugin>

この場合の検索は のようになりますproperties['installation.dir']。私file:///は迷惑であることを知っています。次のリリースでは、その要件を削除しました。

GMaven または GMavenPlus の場合、プラグイン プロパティ アプローチを選択した場合は、プロジェクト POM で次のようなものを使用して、別の場所に値を設定する必要があります。

<properties>
  <installation.dir>C:\someDir</installation.dir>
</properties>

または、次のように呼び出しに含めますmvn -Dinstallation.dir=C:\someDir

もう 1 つのオプションは、プロジェクト レベルのプロパティに直接バインドすることです。上記のように、プロジェクト レベルのプロパティまたは呼び出しに配置<properties>し、プラグインには含めません<configuration>。このルートに進む場合は、スクリプトでproject.properties['installation.dir']GMaven または GMavenPlus のいずれかを使用してアクセスします (この場合、GMavenPlus も取り出します<bindPropertiesToSeparateVariables>)。

これがうまくいかない場合は、 のような名前に変更installation.dirしてみてくださいinstallationDir。生理が問題だったかどうかは、すぐには思い出せません。

于 2015-05-06T17:39:26.520 に答える