执行系统命令

依赖hutool工具类

// 定义要执行的shell命令
String command = StrUtil.format("source /etc/profile;source ~/.bash_profile;cd {};$MAVEN_HOME/bin/mvn --settings $MAVEN_HOME/conf/settings.xml archetype:generate -B " +
                        "    -DarchetypeGroupId={}" +
                        "    -DarchetypeArtifactId={}" +
                        "    -DarchetypeVersion={}" +
                        "    -DgroupId={}" +
                        "    -DartifactId={}" +
                        "    -Dversion={} &>{}",
                codeDest,
                archetype.getArchetypeGroupId(),
                archetype.getArchetypeArtifactId(),
                archetype.getArchetypeVersion(),
                archetype.getGroupId(),
                archetype.getArtifactId(),
                archetype.getVersion(),
                logLocation);

// 拼接命令行数组,这个是必须的,因为要用到shell 的特性
String[] cmd = {
                "/bin/bash", "-c",
                command
        };
// 执行命令
Process process = RuntimeUtil.exec(cmd);
// 等待命令执行完毕
int exitCode = process.waitFor();

  if (exitCode != 0) {
      throw new BusinessException("调用本地系统命令异常,命令如下:" + command + ",结果异常,详情见日志文件" + logLocation);
  }

// 获取命令返回结果
String result = RuntimeUtil.getResult(process);
posted @   SpecialSpeculator  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2022-04-15 day06 prometheus
2021-04-15 第一章:第1课 经济学世界观(上)
点击右上角即可分享
微信分享提示