【问题标题】:maven calls external script on both Linux and Windows platformsmaven 在 Linux 和 Windows 平台上调用外部脚本
【发布时间】:2013-01-26 10:10:56
【问题描述】:

我需要在 Linux 和 MS-Windows 平台上运行一个外部脚本。

  1. 我是否使用了正确的插件exec-maven-plugin
  2. 有没有更合适的插件?
  3. 我应该在<executable>....</executable> 中输入什么文件名?

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
        <executions>
            <execution>
                <id>compile-jni</id>
                <phase>compile</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>./compile-jni</executable>
                    <workingDirectory>${basedir}/src/main/cpp</workingDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

我对两个平台 Linux/MS-Windows 使用相同的 Makefile

我的脚本compile-jni.bat

call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
bash -c "make" 

我的脚本compile-jni.sh:

#!/bin/sh
make

更新:

两位同事提出了替代方案:

  1. 使用变量script.extension&lt;executable&gt;./compile-jni${script.extension}&lt;/executable&gt; 更改为pom.xml 并在命令行中附加变量mvn compile -Dscript.extention=.bat

  2. 或者在调用maven之前设置Visual Studio环境变量:

    call "C:\%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
    mvn compile #(the same script 'bash -c "make"' works on both platforms)
    

但是在这两种解决方案上,Eclipse 用户可能会被卡住……我仍在寻找一种自动且优雅的解决方案……

【问题讨论】:

    标签: shell maven batch-file pom.xml exec-maven-plugin


    【解决方案1】:

    最后,我混合了想法 => &lt;profiles&gt; 用于根据操作系统设置内部变量script.extension

    <profiles>
      <profile>
        <id>Windows</id>
        <activation>
          <os>
            <family>Windows</family>
          </os>
        </activation>
        <properties>
          <script.extension>.bat</script.extension>
        </properties>
      </profile>
      <profile>
        <id>unix</id>
        <activation>
          <os>
            <family>unix</family>
          </os>
        </activation>
        <properties>
          <script.extension>.sh</script.extension>
        </properties>
      </profile>
    </profiles>
    

    然后我用变量来补全脚本文件名:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <executions>
        <execution>
          <id>compile-jni</id>
          <phase>compile</phase>
          <goals>
            <goal>exec</goal>
          </goals>
          <configuration>
            <executable>./compile-jni${script.extension}</executable>
          </configuration>
        </execution>
      </executions>
    </plugin>
    


      ⚠   正如Maksim for maven 3.5.4 所注意到的,将&lt;configuration&gt; 部分向上移动,如下所示: 
     

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <configuration>
        <executable>./compile-jni${script.extension}</executable>
      </configuration>
      <version>1.2.1</version>
      <executions>
        <execution>
          <id>compile-jni</id>
          <phase>compile</phase>
          <goals>
            <goal>exec</goal>
         </goals>
        </execution>
      </executions>
    </plugin>
    

    我已将工作目录从 pom.xml 移至 shell 脚本。为了简化维护,在这个 shell 脚本中移动了常用的东西。因此,批处理文件使用这个 shell 脚本:

    compile-jni.bat

    call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
    bash compile-jni.sh
    

    compile-jni.sh

    #!/bin/sh
    cd src/main/cpp
    make
    

    【讨论】:

    • 您的配置有问题:将配置部分从执行标签移出到根插件声明。即//plugin/executions/execution/configuration -> //plugin/configuration 否则我会收到错误:The parameter 'executable' is missing or invalid on latest maven 3.5.4
    【解决方案2】:

    运行 sh 脚本的示例。

    这只是为 sh 脚本执行chmod。请记住,如果您有一个 sh 脚本,那么在执行其他操作(例如运行实际脚本)之前,您绝对应该执行一个 chmod,因此以这个为例,您可以如下执行第一个 &lt;execution&gt; 并添加另一个 @ 987654324@ 运行您的脚本。

    对于批处理文件,您只能有一个&lt;execution&gt; 来运行您的脚本

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${org.codehaus.mojo.version}</version>
                <executions>
                   <execution>
                        <id>script-chmod</id>
                        <phase>install</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>chmod</executable>
                            <arguments>
                                <argument>+x</argument>
                                <argument>yourscript.sh</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    您可能希望根据您的机器添加配置文件:

    <profiles>
      <profile>
        <activation>
          <os>
            <family>!windows</family>
          </os>
        </activation>
        <plugin>
          <!-- add your exec-maven-plugin here -->
        </plugin>
        ...
      </profile>
    </profiles>
    

    希望这将是您需要的开始

    【讨论】:

    • 嗯....我的 pom.xml 有 120 行长:我还将其他脚本称为 make clean。恐怕&lt;profile&gt; 的大小几乎会翻倍我的pom.xml,不是吗... :-(
    • :) 我有一个 1k 行长的 pom.xml,所以不用担心
    • 1k 行 :o 感谢您的帮助。我终于混合了所有的想法。我将在另一个答案中发布我的最终解决方案。但我验证你的回答。干杯
    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2016-03-29
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多