maven在Linux和Windows平台上调用外部脚本

oli*_*bre 23 shell batch-file pom.xml maven exec-maven-plugin

我需要在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>
    
    Run Code Online (Sandbox Code Playgroud)

我在MakefileLinux/MS-Windows这两个平台上使用相同的功能

我的剧本compile-jni.bat:

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

我的剧本compile-jni.sh:

#!/bin/sh
make
Run Code Online (Sandbox Code Playgroud)

更新:

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

  1. 使用一个变量script.extension 的变化<executable>./compile-jni${script.extension}</executable>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)
    
    Run Code Online (Sandbox Code Playgroud)

但在这两种解决方案中,Eclipse用户可能会被卡住......我仍然在寻找一种自动而优雅的解决方案......

oli*_*bre 37

最后,我混合了想法=> <profiles>用于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>
Run Code Online (Sandbox Code Playgroud)

然后我使用变量来完成脚本文件名:

<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>
Run Code Online (Sandbox Code Playgroud)


  ⚠   As noticed by Maksim for maven 3.5.4 move up the section <configuration> as shown below:  
 

<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>
Run Code Online (Sandbox Code Playgroud)

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

compile-jni.bat:

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

compile-jni.sh:

#!/bin/sh
cd src/main/cpp
make
Run Code Online (Sandbox Code Playgroud)


And*_*fat 5

运行sh脚本的示例.

这只是chmod为sh脚本做的.请记住,如果你有一个sh脚本,你应该chmod在执行其他操作之前做一些事情,比如运行实际的脚本,所以以此为例,你可以<execution>按照下面的步骤进行操作,然后添加另一个<execution>来运行你的脚本.

对于批处理文件,只能<execution>运行一个脚本

        <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>
Run Code Online (Sandbox Code Playgroud)

你可能想要根据你的机器添加一个配置文件:

<profiles>
  <profile>
    <activation>
      <os>
        <family>!windows</family>
      </os>
    </activation>
    <plugin>
      <!-- add your exec-maven-plugin here -->
    </plugin>
    ...
  </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

希望这将是您需要的开始