java下一种获取jar包内文件的方法,适用于win与linux,另附外置配置文件的配置方式

背景

接到一个需求,需要项目启动时读取excel,将数据缓存,然后程序运行时使用

WINDOWS与LINUX均可用

方式与步骤

文件放到config下

在这里插入图片描述

项目初始化时读取文件

步骤1
@Component
@Slf4j
public class Device2LocationRunnerImpl implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

        List<DeviceLocation> locations = FileUtil.readDirectory("config/位置表.xls");
        log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(locations));
        if(StringUtils.isNotEmpty(locations)){
            locations.forEach(location -> {
                DeviceLocationConstant.nickname2LocMap.put(location.getNickname(),location.getLocation());
            });
        }
    }

}
步骤2

在这里插入图片描述

/**
 * directory 传入需要读取文件
 * 
 */
public static List<DeviceLocation> readDirectory(String directory) throws Exception {

    ExcelUtil<DeviceLocation> util = new ExcelUtil<DeviceLocation>(DeviceLocation.class);
    List<DeviceLocation> locations = Lists.newArrayList();


    //取出配置路径下的配置文件
    try {
        InputStream resourceAsStream = FileUtil.class.getClassLoader().getResourceAsStream(directory);
        List<DeviceLocation> locations1 = util.importExcel(resourceAsStream);

        log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(locations1));
        locations.addAll(locations1);

    } catch (Exception e) {
        log.error("解析:{}文件出现异常:{}", directory, e);
    }

    return locations;
}

外置配置文件可用的读取方式

待读取文件

在这里插入图片描述

方法展示

在这里插入图片描述

/**
 * 读取文件夹下所有xml文件
 * 传入config即获取xxxxx\target\classes\config目录
 *
 */
public static List<Config> readDirectory(String directory) throws Exception {
    List<Config> configs = Lists.newArrayList();
    Resource resource = new ClassPathResource(directory);
    Arrays.stream(Objects.requireNonNull(resource.getFile().listFiles())).filter(File::isDirectory).distinct().forEach(file ->{

            //取出配置路径下的配置文件
            Arrays.stream(file.listFiles()).filter(File::isFile).distinct().forEach(xml ->{
                try {
                    InputStream inputStreamCheck = FileUtils.openInputStream(xml);
                    //只解析xml文件
                    if (!FileUtil.checkFileType(inputStreamCheck, FileType.XML)) {
                        log.info("config下文件:{}不是xml类型,不符合解析条件,本次跳过", xml.getName());
                        return;
                    }
                    InputStream inputStreamUse = FileUtils.openInputStream(xml);
                    String xmlStr = new String(readFileStream(inputStreamUse), Constants.GBK);
                    Config config = (Config) XsteamUtil.xmltoBean(Config.class, xmlStr);
                    log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(config));
                    configs.add(config);

                }catch (IOException e){
                    log.error("解析:{}文件出现异常:{}" ,xml.getName(),e);
                }
            });

    });
    return configs;
}

外置配置文件的方式

    <plugins>
        <!--打包jar-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <!--不打包资源文件-->
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.yml</exclude>
                </excludes>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--jar包不包含唯一版本标识-->
                        <useUniqueVersions>false</useUniqueVersions>
                        <!--指定入口类-->
                        <mainClass>com.xxx.xxxApplication</mainClass>
                    </manifest>
                    <manifestEntries>
                        <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                        <Class-Path>./resources/</Class-Path>
                    </manifestEntries>
                </archive>
                <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
        </plugin>
        <!--拷贝依赖 copy-dependencies-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib/
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--拷贝资源文件 copy-resources-->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/resources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                <includes>
                    <include>
                        <groupId>com.xxxx</groupId>
                        <artifactId>xxxxx-xxxx</artifactId>
                    </include>
                </includes>
                <layout>ZIP</layout>
                <!--使用外部配置文件,jar包里没有资源文件-->
                <addResources>true</addResources>
                <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                        <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                        <!--<classifier>run</classifier>-->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值