Maven 编译后执行 Shell 脚本或 Ant 命令

在 Maven 编译后执行 Shell 脚本或 Ant 命令有多种方式。以下是几种常用的方法:

1. 使用 Maven Exec Plugin 执行 Shell 脚本

pom.xml 配置

xml 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>bash</executable>
                        <arguments>
                            <argument>deploy.sh</argument>
                        </arguments>
                        <workingDirectory>${project.basedir}</workingDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Windows 环境配置

xml 复制代码
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>cmd</executable>
                <arguments>
                    <argument>/c</argument>
                    <argument>deploy.bat</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

2. 使用 Maven Antrun Plugin 执行 Ant 任务

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <echo message="执行 Ant 任务..." />
                    <ant antfile="${project.basedir}/build.xml" target="deploy"/>
                </target>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.12</version>
        </dependency>
    </dependencies>
</plugin>

3. 使用 Maven Invoker Plugin 执行外部命令

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-invoker-plugin</artifactId>
    <version>3.5.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <scripts>
                    <script>deploy.sh</script>
                </scripts>
                <goals>
                    <goal>clean</goal>
                    <goal>package</goal>
                </goals>
            </configuration>
        </execution>
    </executions>
</plugin>

4. 完整的示例配置

执行 Shell 脚本的完整配置

xml 复制代码
<build>
    <plugins>
        <!-- 编译插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

        <!-- 执行 Shell 脚本 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>run-deploy-script</id>
                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>bash</executable>
                        <arguments>
                            <argument>scripts/deploy.sh</argument>
                            <argument>${project.version}</argument>
                            <argument>${project.build.directory}</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

对应的 deploy.sh 脚本示例

bash 复制代码
#!/bin/bash
echo "开始部署应用..."
echo "版本: $1"
echo "构建目录: $2"

# 执行部署操作
cp "$2/*.jar" /opt/app/
systemctl restart myapp.service
echo "部署完成!"

5. 多平台支持的配置

xml 复制代码
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>${script.executable}</executable>
                <arguments>
                    <argument>scripts/deploy.${script.extension}</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

profiles 中配置不同平台:

xml 复制代码
<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <properties>
            <script.executable>cmd</script.executable>
            <script.extension>bat</script.extension>
        </properties>
    </profile>
    <profile>
        <id>unix</id>
        <activation>
            <os>
                <family>unix</family>
            </os>
        </activation>
        <properties>
            <script.executable>bash</script.executable>
            <script.extension>sh</script.extension>
        </properties>
    </profile>
</profiles>

6. 常用 Maven 生命周期阶段

  • validate - 验证项目
  • compile - 编译源代码
  • test - 运行测试
  • package - 打包编译后的代码(常用)
  • verify - 运行集成测试
  • install - 安装到本地仓库
  • deploy - 部署到远程仓库

注意事项

  1. 权限问题 :确保脚本有执行权限 chmod +x script.sh
  2. 路径问题 :使用 ${project.basedir} 指定工作目录
  3. 跨平台:Windows 和 Unix 系统的脚本语法不同
  4. 依赖顺序:确保脚本在正确的生命周期阶段执行

选择哪种方式取决于您的具体需求:

  • 简单命令:使用 exec-maven-plugin
  • 复杂构建逻辑:使用 maven-antrun-plugin
  • 需要隔离环境:使用 maven-invoker-plugin