Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.yes.tools.build.handle.BuildHandle Maven / Gradle / Ivy
package org.yes.tools.build.handle;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.maven.plugin.logging.Log;
import org.yes.tools.build.bo.DeployBo;
import org.yes.tools.build.utils.CommonUtils;
import org.yes.tools.build.utils.TemplateUtils;
import java.io.*;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author Co.
* @name BuildHandle
* @date 2023/6/19 11:03
*/
public class BuildHandle {
public static void generateDockerFile(List properties, String image, Log log, DeployBo.Deploy deploy) throws IOException, TemplateException {
List propertiesList = CommonUtils.getJarPath(properties);
String targetPathStr = System.getProperty("user.dir") + "/release/";
for (Properties proper : propertiesList) {
String path = proper.get("jarPath").toString();
String rootDirectory = targetPathStr + proper.get("project").toString() + "/";
File tatgatFile = new File(rootDirectory);
if (!tatgatFile.exists()) {
tatgatFile.mkdirs();
}
// 复制jar包
List files = copyJar(path, tatgatFile);
for (File file : files) {
//新增dockerfile
addDockerfile(rootDirectory, image, file.getName(), proper.get("project").toString(), proper.get("port").toString());
//新增sh
addBuildSh(rootDirectory, proper);
}
}
generateDockerSh(targetPathStr, deploy, log);
generateDockerFile(targetPathStr, deploy);
generateJarSh(targetPathStr, deploy, log);
}
private static void generateJarSh(String targetPathStr, DeployBo.Deploy deploy, Log log) throws IOException, TemplateException {
String filePathUrl = targetPathStr + deploy.project.getArtifactId();
//生成sh
log.info("------------------------------正在生成SH文件-----------------------------------");
File targetFile = new File(filePathUrl + "/jar-start.sh");
Template template = TemplateUtils.getTemplate("jar.sh.ftl");
targetFile.getParentFile().mkdirs();
FileOutputStream file = new FileOutputStream(targetFile);
OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
Map params = new HashMap<>();
params.put("cloudImage", deploy.cloudImage);
params.put("project", deploy.project);
template.process(params, out);
out.close();
downloadJdk(filePathUrl);
}
private static void generateDockerSh(String targetPathStr, DeployBo.Deploy deploy, Log log) throws IOException, TemplateException {
String shPathUrl = targetPathStr + deploy.project.getArtifactId() + "/run-service.sh";
//生成sh到目录的release目录中
log.info("------------------------------正在生成SH文件-----------------------------------");
File targetFile = new File(shPathUrl);
Template template = TemplateUtils.getTemplate("service.sh.ftl");
targetFile.getParentFile().mkdirs();
FileOutputStream file = new FileOutputStream(targetFile);
OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
Map params = new HashMap<>();
params.put("cloudImage", deploy.cloudImage);
params.put("project", deploy.project);
template.process(params, out);
out.close();
}
private static void generateDockerFile(String targetPathStr, DeployBo.Deploy deploy) throws IOException {
String filePathUrl = targetPathStr + deploy.project.getArtifactId();
FileWriter fw = null;
BufferedWriter bufw = null;
File file = new File(filePathUrl + "/build.sh");
try {
//创建输出流
//true代表追加写入内容至文件末尾
fw = new FileWriter(file, true);
bufw = new BufferedWriter(fw);
//输出
String temp = "docker run -d -p %s --network=host --name=%s-%s -e NACOS_ACTIVE=%s -e ACTIVE=%s %s -e TZ=Asia/Shanghai -e JAVA_OPTS=\"%s\" %s:%s\n" +
"echo \"---------------------------------------启动完成--------------------------------\"";
bufw.write(String.format(temp, deploy.project.getPort().get(0) + ":" + deploy.project.getPort().get(0),
deploy.project.getArtifactId(),
deploy.project.getVersion(),
deploy.project.getActive(),
deploy.project.getActive(),
deploy.project.getDockerParameter(),
deploy.project.getJvmParameter(),
deploy.project.getArtifactId(),
deploy.project.getVersion()));
bufw.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
//出现异常后在finally中关闭输出流
if (bufw != null) {
bufw.close();
}
if (fw != null) {
fw.close();
}
}
}
/**
* 新增sh
*
* @param rootDirectory
* @param proper
* @throws IOException
* @throws TemplateException
*/
private static void addBuildSh(String rootDirectory, Properties proper) throws IOException, TemplateException {
File targetFile = new File(rootDirectory + "build.sh");
Template template = TemplateUtils.getTemplate("build.sh.ftl");
targetFile.getParentFile().mkdirs();
FileOutputStream file = new FileOutputStream(targetFile);
OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
Map params = new HashMap<>();
params.put("version", proper.get("version").toString());
params.put("projectName", proper.get("project").toString());
params.put("port", proper.get("port").toString());
template.process(params, out);
out.close();
}
/**
* 新增dockerfile
*
* @param rootDirectory
* @param image
* @param jarName
* @param serviceName
* @param port
* @throws IOException
* @throws TemplateException
*/
private static void addDockerfile(String rootDirectory, String image, String jarName, String serviceName, String port) throws IOException, TemplateException {
File targetFile = new File(rootDirectory + "Dockerfile");
Template template = TemplateUtils.getTemplate("Dockerfile.ftl");
targetFile.getParentFile().mkdirs();
FileOutputStream file = new FileOutputStream(targetFile);
OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
Map params = new HashMap<>();
params.put("image", image);
params.put("jarName", jarName);
params.put("serviceName", serviceName);
params.put("port", port);
template.process(params, out);
out.close();
}
/**
* 复制jar包
*
* @param path
* @param tatgatFile
* @return
* @throws IOException
*/
private static List copyJar(String path, File tatgatFile) throws IOException {
File file = new File(System.getProperty("user.dir") + "/" + path + "/target");
List jarFiles = CommonUtils.getJarFile(file);
for (File jarFile : jarFiles) {
InputStream inputStream = new FileInputStream(jarFile);
if (inputStream != null) {
String outPath = tatgatFile.getPath() + "/" + jarFile.getName();
java.nio.file.Files.copy(inputStream, new File(outPath).toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
return jarFiles;
}
/**
* 下载jdk
*
* @param filePathUrl
* @throws IOException
* @throws TemplateException
*/
static void downloadJdk(String filePathUrl) throws IOException, TemplateException {
File targetFile = new File(filePathUrl + "/downloadJdk.sh");
Template template = TemplateUtils.getTemplate("downloadJdk.sh.ftl");
targetFile.getParentFile().mkdirs();
FileOutputStream file = new FileOutputStream(targetFile);
OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
Map params = new HashMap<>();
template.process(params, out);
out.close();
}
}