Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
993 views
in Technique[技术] by (71.8m points)

shell - maven calls external script on both Linux and Windows platforms

I need to run an external script on both Linux and MS-Windows platforms.

  1. Do I use the right plugin exec-maven-plugin?
  2. Is there a more suitable plugin?
  3. What filename should I put in <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>
    

I use the same Makefile for both platforms Linux/MS-Windows

My script compile-jni.bat:

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

My script compile-jni.sh:

#!/bin/sh
make

UPDATE:

Two colleagues have suggested alternatives:

  1. Use a variable script.extension change <executable>./compile-jni${script.extension}</executable> in the pom.xml and append the variable within the command line mvn compile -Dscript.extention=.bat

  2. or set the Visual Studio environment variables before calling maven:

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

But on both solutions, Eclipse users may be stucked... I am still looking for an automatic and elegant solution...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.0VCvcvarsall.bat" x86
bash compile-jni.sh

compile-jni.sh:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...