首页 > 解决方案 > 如何将 env var arg 传递给 maven 到 shell 脚本?

问题描述

我正在使用 exec 插件从 maven 调用 shell 脚本,并希望将 arg 传递给我的 maven 命令,该命令将被转发到 shell 脚本。所以如果我这样做

mvn exec:exec compile -Dfoo=bar

我希望能够在我的 shell 脚本中使用 foo 访问$foo. 我尝试使用${env.foo}and将它作为参数从 pom.xml 传递给 shell 脚本${foo},但我总是在 shell 脚本中得到那些确切的文字而不是“bar”,它 foo 也应该扩展。

我的 pom.xml 就像

<build>
  <plugins>
    <plugin>
      <artifactId>exec-maven-plugin</artifactId>
      <groupId>asdf</groupId>
      <version>1</version>
      <executions>
        <execution>
          <id>asdf</id>
          <phase>compile</phase>
          <goals>
            <goal>exec</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <executable>bash</executable>
        <commandlineArgs>myscript.sh ${env.foo}</commandlineArgs>
      </configuration>
     </plugin>
    </plugins>
  </build>

标签: maven

解决方案


您可以使用argumentsorcommandlineArgs配置。

关键是您必须为以下foo任一项提供值:

  • 在命令行上:-Dfoo=bar
  • 通过定义一个属性:<properties><foo>bar</foo></properties>

例如:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <configuration>
        <executable>bash</executable>
        <arguments>
            <argument>myscript.sh</argument> 
            <argument>${foo}</argument>
        </arguments>
    </configuration>
</plugin>

然后跑...

mvn exec:exec -Dfoo=bar

... 将导致myscript.sh使用一个参数运行:foo.

注意:您问题中的插件配置看起来不正确。具体来说; ( groupId"asdf") 和version(1)。


推荐阅读