Spring Boot打Jar包,读取resource

打jar包

首先在pom.xml中添加如下的插件

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.xxx.admin.Main</mainClass>

<!--                    <excludes>-->
<!--                        <groupId>org.springframework.boot</groupId>-->
<!--                        <artifactId>spring-boot-configuration-processor</artifactId>-->
<!--                    </excludes>-->
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

然后运行下面的命令打包

mvn clean package -Dmaven.test.skip=true

打包完成,进入Target目录,使用java -jar命令,运行应用。
然后就可以在浏览器愉快的访问了。

读取资源

接下来是读取jar包中的资源问题,我的需求是能够读取到jar包内的资源文件夹中的多个文件,经过测试发现无法读取,所以只能读取target中的资源。写法如下:

private static String GetTableDir() {
    String directory = "";
    try {
        directory = ResourceUtils.getURL("classpath:").getPath() + "static/table";
        // 莫名奇妙的感叹号是怎么回事,好吧,把它干掉
        if (directory.contains("!")) {
            directory = directory.replaceAll("!", "");
            // 不用jar包的resource, 用target中的resource,这里的名字根据项目名称自己改,也可以用查询的方式更健壮
            directory = directory.replace("/01helloworld-1.0-SNAPSHOT.jar/BOOT-INF", "");
            // 最开始的这个东西也干掉
            directory = directory.replace("file:", "");
        }

    } catch (Exception e) {
        e.printStackTrace();
        log.error(e.getMessage());
    }
    return directory;
}

spring boot 打包时排除特定资源

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
	<resources>
		<resource>
			<filtering>true</filtering>
			<directory>src/main/resources</directory>
			<excludes>
				<exclude>webapp/**</exclude>
			</excludes>
		</resource>
	</resources>
</build>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在将Spring Boot项目打成jar包后,如果工具类无法读取模板文件,可能是因为模板文件没有被正确地打包进jar包中。这可以通过以下方法解决: 1. 确认模板文件的路径和位置。在Spring Boot项目中,模板文件通常位于/resources/templates目录下。确保文件名和相对路径都正确。 2. 使用Maven插件来打包项目。例如,在pom.xml文件中添加以下配置: ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build> ``` 3. 将模板文件的位置和路径添加到ResourceLoader中。例如,可以在工具类中使用以下代码来获取模板文件: ``` ResourceLoader resourceLoader = new FileSystemResourceLoader(); Resource resource = resourceLoader.getResource("classpath:/templates/template.html"); ``` 其中,classpath:/templates/template.html是模板文件的相对路径。 4. 如果仍然无法读取模板文件,可以尝试将模板文件打包到jar包的根目录下。在pom.xml文件中添加以下配置: ``` <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> <filtering>true</filtering> </resource> </resources> </build> ``` 这将在打包时将src/main/resources目录下的所有文件打包到根目录下。 综上所述,将Spring Boot项目打包成jar包后,如果工具类无法读取模板文件,需要注意模板文件的路径和位置,并使用Maven插件来打包项目。此外,还可以将模板文件打包到jar包的根目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值