WebApp中的Java Maven参考文件 [英] Java Maven reference files in WebApp

查看:59
本文介绍了WebApp中的Java Maven参考文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试引用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 like src/main/java and put your package(s) in the java folder.
                  For any resources, create a folder structure like src/main/resources and put your resources in the resources folder.
                  After you do this, lets say that there is a file named readme.txt inside src/main/resources directory, then you can simple access this file using:
                  File file = new File("readme.txt");
                  Maven will always treat src/main/resources as the root for any resources.
                  Another example is lets say you have a a file name hello.txt in src/main/resources/somefolder/hello.txt then you can access it using:
                  File file = new File("somefolder/readme.txt");

                  这篇关于WebApp中的Java Maven参考文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆