我正在使用spring boot并将我的静态内容(html,css等)存储为 src/main/webapp
. 我知道不建议这样做,因为它不会存储在jar文件中(应该在 /META-INF/resources/
, /resources/
, /static/
, /public/
) . 但我的问题不是关于在jar中存储静态内容 .
这是项目结构:
App
├──src
| └───main
| ├── java
| | └── ...
| ├── resources
| | └── (everything here will be in jar)
| └── webapp <- this won't be in jar
| └── ...
└ target
└───App.jar
当我尝试从 App
文件夹中运行jar作为 java -jar target/App.jar
时,spring boot找到静态内容并且工作正常 . 但是,从 target
文件夹运行它作为 java -jar App.jar
将找不到静态内容 .
如何从目标文件夹运行 App.jar
并让spring启动找到静态内容?它与classpath有关吗?我试图用 -cp
选项指定类路径,但它不起作用 .
2 回答
有's a point of view that this is a build problem. I don'吨意味着包装问题(我知道你不想把它放进 jar 里) . 但是您有另一种选择:您的构建系统(例如Maven)可以将静态文件复制到目标文件夹中 .
This answer演示了如何使用
maven-antrun-plugin
来调用Ant的copy
命令 . 这会将静态文件的副本放入target
文件夹中 .显然改变了执行阶段(测试阶段对于你的用例来说太具体/迟了) . 并且可能会查找是否有更多的Maveny方法(因为委托给Ant不是超级惯用的) .
它's searching relative to the working directory of the executable at runtime. Since it'使用相对路径来查找文件(例如
src/main/webapp/file.txt
),您的工作目录需要是/path/to/App
.You can change the working directory使用
-Duser.dir
.我建议试试这个:
或许它需要是一条绝对的道路 . 告诉我它是否有效;我没有得到这个项目的尝试 .