maven calls external script on both Linux and Windows platforms

扶醉桌前 提交于 2019-11-28 16:53:29

Finally, I mixed the ideas => the <profiles> are used to set an internal variable script.extension depending on the operating system:

<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>

Then I use the variable to complete the script filename:

<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>


  ⚠   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>

I have moved the working directory from the pom.xml to the shell script. In order to simplify maintenance, the common stuff is moved within this shell scrip. Therefore, the batch file use this shell script:

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

An example of running sh script.

This just does a chmod for the sh script. Keep in mind if you have a sh script, you should definitely do a chmod before performing other operations such as running the actual script, so having this as an example, you can do the first <execution> as below and add another <execution> to run your script.

For the batch file, you can have only one <execution> to run your script

        <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>

and you would probably want to add a profile depending on which machine you are:

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

Hope this will be a start for what you need

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!