idea使用maven项目本地打包时避免将未提交代码打包

方法1:使用Maven插件检查Git状态

pom.xml中配置exec-maven-plugin,在构建前检查未提交的代码:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>check-uncommitted-changes</id>
                    <phase>validate</phase> <!-- 在验证阶段执行 -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>git</executable>
                        <arguments>
                            <argument>diff</argument>
                            <argument>--quiet</argument>
                            <argument>HEAD</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

说明

  • git diff --quiet HEAD命令会检测工作区与最新提交的差异。若有未提交的修改,返回非零状态,导致Maven构建失败。

  • 仅检测已跟踪文件:若需包含未跟踪文件(新增未提交),改用git status --porcelain检查。

方法2:自定义脚本检查(跨平台方案)

  1. 创建检查脚本(如check-clean.sh):

    #!/bin/bash
    if [[ -n $(git status -s) ]]; then
        echo "存在未提交的更改,终止构建!";
        exit 1;
    fi
  2. 配置插件执行脚本

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
            <execution>
                <id>check-clean-workspace</id>
                <phase>validate</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>bash</executable>
                    <arguments>
                        <argument>check-clean.sh</argument>
                    </arguments>
                </configuration>
            </execution>
        </executions>
    </plugin>

    方法3:使用CI/CD策略(推荐生产环境)

    在持续集成(如GitHub Actions、GitLab CI)中配置流程,仅允许打包已提交到版本库的代码:

    GitHub Actions示例

    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v4
        - name: Check for uncommitted changes
          run: |
            if [[ -n $(git status --porcelain) ]]; then
              echo "错误:存在未提交的更改!";
              exit 1;
            fi
        - name: Build with Maven
          run: mvn package

    方法4:手动清理工作区

    打包前手动确保代码已提交:

    git stash -u       # 暂存所有修改(包括未跟踪文件)
    mvn clean package
    git stash pop      # 恢复修改

    构建失败自定义提示语

    <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <id>check-uncommitted-changes</id>
                            <phase>validate</phase> <!-- 在验证阶段执行  -->
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>powershell</executable>
                                <arguments>
                                    <argument>-Command</argument>
                                    <argument>
                                        if (git status -s) {
                                        Write-Host "========================>:[ERROR]: There are uncommitted changes! Please submit or stage all changes before packaging";
                                        exit 1;
                                        }
                                    </argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>


    注意事项

  3. Git可执行路径:确保构建环境中git命令可用。

  4. 忽略未跟踪文件:若只需检查已跟踪文件,将git status -s改为git status -s -uno

  5. 跨平台兼容:Windows环境需改用Batch/PowerShell脚本或使用Maven Profile区分系统。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值