在使用Maven编译过程中,有时可能需要执行脚本(如打包压缩JS),然而不同的操作系统执行脚本的命令不同,如何根据不同的操作系统来调用不同的脚本命令呢?
基本思路:
(1)使用ant实现,ant 具有强大的功能,解决:使用maven-antrun-plugin插件;(2)有能根据不同操作系统执行不同脚本的语句,解决:用ant的<if>.标签;(2)有能获取和判断操作系统的语句,解决:用ant 的<contains>标签。
下面一个示例可供参考:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>one-project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>one project</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
<version>3.1</version>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>packagejs</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"
classpathref="maven.runtime.classpath" />
<if>
<contains string="${os.name}" substring="Windows"/>
<then>
<echo>Operation System:${os.name}</echo>
<exec dir="F:\\temp\\1" executable="C:\\Windows\\System32\\cmd.exe" output="dir.txt">
<arg line="/c dir"/>
</exec>
</then>
<else>
<echo> Linux</echo>
<echo>Operation System:${os.name}</echo>
<exec dir="/home/builder/logs" executable="/bin/bash" output="dir.txt">
<arg line="-c ls"/>
</exec>
</else>
</if>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
说明:
(1)需要引入ant-contrib 包;
(2)在tasks 节点需要引入加入:
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.runtime.classpath" />
另一个可供参考的例子(写法更简单,不需要引入ant-contrib包):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>one-project</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>one project</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
<version>3.1</version>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>packagejs</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<condition property="osFamily" value="windows">
<os family="windows" />
</condition>
<condition property="osFamily" value="unix">
<os family="unix" />
</condition>
<exec dir="F:\\temp\\1" executable="C:\\Windows\\System32\\cmd.exe" osfamily="windows" output="dir.txt">
<arg line="/c dir"/>
</exec>
<exec dir="/home/builder/logs" executable="/bin/bash" osfamily="unix" output="dir.txt">
<arg line="-c ls"/>
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
参考:
1、https://www.ibm.com/developerworks/cn/java/j-lo-antautofind/
2、https://blog.csdn.net/lhltolerate/article/details/50825341?utm_source=blogxgwz1