简体   繁体   English

如何在Maven Java Web应用程序中设置文件夹的相对路径?

[英]How to set relative path for a folder in Maven java web application?

I am creating a Maven web application which stores many policy files in policy folder. 我正在创建一个Maven Web应用程序,该应用程序将许多策略文件存储在policy文件夹中。 My source folder layout looks like : 我的源文件夹布局如下: 在此处输入图片说明 I tried to set the folder path as policyLocation as shown below: 我试图将文件夹路径设置为policyLocation,如下所示:

try {
    // InputStream aa =
    // Accesscontrol.class.getClass().getResource(aa);
    String AbsolutePath = new File("/").getAbsolutePath();
    String policyLocation = (new File("."))
            .getCanonicalPath() + File.separator + "policy";
    System.setProperty(
            FileBasedPolicyFinderModule.POLICY_DIR_PROPERTY,
            policyLocation);
} catch (IOException e) {
    System.err.println("Can not locate policy repository");
}

But this is returning my Eclipse folder. 但这将返回我的Eclipse文件夹。 Is there anyway I can get the policy folder that is not inside Resource folder (cause it can't be included inside Resource) in java web application? 无论如何,我可以在Java Web应用程序中获得不在Resource文件夹内的策略文件夹(因为它不能包含在Resource内)吗?

Please keep in mind that your application has more than 1 way to run: 请记住,您的应用程序有多种运行方式:

  • from within IDE (Eclipse) where certain things are done automatically for you (like compilation on the fly) 从IDE(Eclipse)内部自动完成某些事情(例如即时编译)
  • when you build your web application using maven , do you build it as jar , war , or perhaps ear ? 当您使用Maven构建Web应用程序时,您将其构建为jarwar还是ear Once built, how do you deploy and run it? 构建完成后,如何部署和运行它?

Firstly you have to ensure that your build process (maven) includes the policy files in the distributable (eg in the war file) in a known location which you can reference. 首先,您必须确保构建过程(Maven)在可引用的已知位置中将策略文件包含在可分发文件中(例如, war文件中)。 Then you can modify the classpath within the jar or war and to ensure that your policy files are on the classpath. 然后,您可以在jarwar中修改类路径,并确保策略文件位于类路径中。

With such setup all you have to do is to address it as a resource as per this answer 通过这种设置,您所要做的就是按照此答案将其作为资源来解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM