执行系统命令

依赖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 @ 2024-04-15 14:48  SpecialSpeculator  阅读(4)  评论(0编辑  收藏  举报