SpringBoot项目jar发布获取jar包所在目录路径

发布于:2023-01-19 ⋅ 阅读:(474) ⋅ 点赞:(0)


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;


public class SpringBootUtils {

    private static final Logger logger = LoggerFactory.getLogger(SpringBootUtils.class);

    public static List<File> getRunBootDirFiles(String startsWith, String suffix) {
        ApplicationHome home = new ApplicationHome(SpringBootUtils.class);
        File jarFile = home.getSource();
        File homeFile = new File(jarFile.getParentFile().toString() + File.separator);
        logger.info("读取文件:{}", homeFile.getAbsoluteFile());
        File[] files = homeFile.listFiles((dir, name) -> StringUtils.startsWith(name, startsWith)
                && StringUtils.endsWith(name, suffix));
        if (Objects.nonNull(files)) {
            logger.info("{}:共读取到文件:{}", startsWith, files);
            return Arrays.stream(files).sorted(Comparator.comparing(File::getName)).collect(Collectors.toList());
        } else {
            return Collections.emptyList();
        }
    }

}


网站公告

今日签到

点亮在社区的每一天
去签到