【问题标题】:executing shell script and ant script with named parameter using maven使用 maven 执行带有命名参数的 shell 脚本和 ant 脚本
【发布时间】:2014-04-01 19:03:29
【问题描述】:

我正在为 ant 构建实现 maven 包装器。而用于构建项目的ant命令如下

ant -v -f build.xml -Darch=linux-java7 -Dconfig=/work/build.config -Doutput=/work/bldout/

现在我必须通过 maven 执行上述命令。我尝试使用“I want to execute shell commands from maven's pom.xml”和“http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/”来实现这一点

我在 pom.xml 中尝试的示例代码如下:

<plugin>
      <artifactId>exec-maven-plugin</artifactId>
      <groupId>org.codehaus.mojo</groupId>
      <executions>
             <execution>
             <id>execute-shell</id>
              <phase>compile</phase>
              <goals>
                      <goal>exec</goal>
              </goals>
              <configuration>
                      <executable>test.sh</executable>
                      <arguments>
                            <argument>ARG1</argument>
                            <argument>ARG2</argument>
                      </arguments>
             </configuration>
             </execution>
     </executions>
</plugin>

但我无法弄清楚如何将命名参数(例如“-Darch=linux-java7”)作为参数传递给 build.xml

同样使用maven-antrun插件调用build.xml如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>

     <executions>
       <execution>
          <id>run-target</id>
          <phase>install</phase>
              <configuration>
                 <target>
                    <ant antfile="build.xml" target="all" />
                 </target>
              </configuration>
          <goals>
             <goal>run</goal>
          </goals>
      </execution>

但在这里我也无法弄清楚如何将诸如“-Darch=linux-java7”之类的参数作为参数传递给 build.xml

我知道的是,我可以将命令放入 shell 脚本(在 .sh 文件中)并使用 maven-exec-plugin 调用 shell 脚本,但想知道是否可以不这样做。

【问题讨论】:

    标签: maven ant


    【解决方案1】:

    使用 antrun,它的外部依赖较少(如路径上的 ant 可执行文件等)

    由 antrun 插件执行的 Ant 任务继承在你的 pom 的 properties 部分中定义的所有属性。

    所以你只需要包含:

    <properties>
      <arch>linux-java7</arch>
      ...
    </properties>
    

    在你的 pom 里面让它工作。

    【讨论】:

    • 好的,将尝试将它们添加到属性标签中,您还知道如何通过 pom 传递 -v 和 -f ant 选项吗?通过 antrun 插件执行 ant 时,这些选项真的有必要吗?
    • -f build.xml 即使在从命令行调用时也是不必要的,因为默认值为build.xml-v 给出详细的输出。恕我直言,没有现成的选项可以使用 antrun 执行此操作(除了使用 -X 为整个 maven 作业打开详细信息,您真的需要 verbose 输出吗?详细的想法是记录额外(调试)信息以防万一出了点问题,这不应该是默认值。
    • 好吧,得和我的队友说一说,这是我第一次使用ant,最好还是先通过ant。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2011-04-24
    • 2013-09-01
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多