WebApp中的Java Maven参考文件 [英] Java Maven reference files in WebApp
问题描述
我正在尝试引用Java应用程序中的一些文件,这些文件将被复制到WAR中.
I'm trying to reference some files in a Java application that will be copied into a WAR.
我是Maven的新手,但基本上文件是
I'm new to Maven, but basically the files are
- 主要
- Java
- 包装
- 课程----从这里开始
- Main
- Java
- package
- Class ---- FROM HERE
- 资源
- 文件夹----到这里
- resources
- folder ---- TO HERE
我尝试了相对路径,例如
I've tried a relative paths such as
File source = new File("../../../webapp/resources/folder");
但是永远不能从类中访问该文件夹.
But can never access that folder from the class.
是否有替代方法?
推荐答案
始终记住,Maven的策略是
convention over configuration
.
话虽如此,在您使用Maven的情况下,您需要遵循 Maven标准目录结构.
创建一个像src/main/java
这样的目录结构,并将您的包放在java
文件夹中.
对于任何资源,创建一个类似于src/main/resources
的文件夹结构,然后将您的资源放在resources
文件夹中.
完成此操作后,假设在src/main/resources
目录中有一个名为readme.txt的文件,那么您可以使用以下命令简单地访问此文件:
File file = new File("readme.txt");
Maven将始终将src/main/resources
视为任何资源的根.
另一个示例是,假设您在src/main/resources/somefolder/hello.txt
中具有文件名hello.txt,然后可以使用以下文件进行访问:
File file = new File("somefolder/readme.txt");
Always remember that Maven's policy is
convention over configuration
.
That being said, in your case, while using Maven, you need to follow the Maven Standard Directory Structure.
Create a directory structure likesrc/main/java
and put your package(s) in thejava
folder.
For any resources, create a folder structure likesrc/main/resources
and put your resources in theresources
folder.
After you do this, lets say that there is a file named readme.txt insidesrc/main/resources
directory, then you can simple access this file using:
File file = new File("readme.txt");
Maven will always treatsrc/main/resources
as the root for any resources.
Another example is lets say you have a a file name hello.txt insrc/main/resources/somefolder/hello.txt
then you can access it using:
File file = new File("somefolder/readme.txt");
这篇关于WebApp中的Java Maven参考文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- package
- Java
- 包装
- Java