简体   繁体   English

如何在Maven多模块应用程序中查找资源路径

[英]How to find resource path in maven multimodule application

I have a multimodule maven application that has the following structure: 我有一个具有以下结构的多模块Maven应用程序:

main-project
      ->submodule1
         ->src->main
            ->java
               ->MainClass.java
            ->resource
               ->php
                    index11.php
                    file12.php
                    file13.php
      ->submodule2
         ->src->main
            ->java
               MainClass.java
         ->resource
           ->php
                index21.php
                file22.php
                file23.php
     ->submodule3
        ->src->main
           ->java
               MainClass.java
           ->resource
              ->php
                  index31.php
                  file32.php
                  file33.php
     ->web-app
       ->src->main
            ->webapp  

Java classes from submodules should access the php files in their resource directories and execute it using Quercus Resin. 子模块中的Java类应访问其资源目录中的php文件,并使用Quercus Resin执行该文件。 However, when the project is packed in war, submodules are packed into jar file that are stored in web-app/WEB-INF/lib, which makes it impossible to execute php files. 但是,当项目处于战争状态时,子模块被打包到jar文件中,而jar文件存储在web-app / WEB-INF / lib中,这使得无法执行php文件。 As workaround for this problem, I found solution to copy all php files from the submodules into the web-app, so when it's extracted in Tomcat, it's not inside jar file and could be executed. 作为解决此问题的方法,我找到了将所有php文件从子模块复制到Web应用程序的解决方案,因此在Tomcat中提取它时,它不在jar文件中并且可以执行。 For that purpose, I'm using maven-remote-resources-plugin, and all php files are stored to web-app/src/main/webapp/php. 为此,我正在使用maven-remote-resources-plugin,并且所有php文件都存储在web-app / src / main / webapp / php中。

The problem I have now is how to properly provide path to these php files from java classes inside submodules.These java classes are inside jar files when application is deployed to Tomcat, but during development I'm using embedded Jetty server, so I need solution that would work in both cases. 我现在的问题是如何从子模块中的Java类正确提供这些php文件的路径。将应用程序部署到Tomcat时,这些Java类位于jar文件中,但是在开发过程中我使用的是嵌入式Jetty服务器,因此我需要解决方案在两种情况下都可以使用。

If I use class loader to get resource,eg getClass().getClassLoader().getResource("/php/index11.php").getPath() it returns absolute path to the submodule1.jar file. 如果我使用类加载器获取资源,例如getClass()。getClassLoader()。getResource(“ / php / index11.php”)。getPath(),它将返回submodule1.jar文件的绝对路径。

Any idea how to solve this issue? 任何想法如何解决这个问题?

I managed to solve this problem, so I will post solution here if it might help someone else. 我设法解决了这个问题,因此如果可以帮助其他人,我将在此处发布解决方案。

In each submodule I have a maven-remote-resources-plugin bundle to collect all resources I need 在每个子模块中,我都有一个maven-remote-resources-plugin捆绑包来收集我需要的所有资源

 <build>
    <plugins>
        <plugin>
            <artifactId>maven-remote-resources-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>bundle</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/*.php</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

Then, in web-app submodule I'm using maven-remote-resources-plugin process to copy these php files to resource directories called WEB-INF/php/submodule-name 然后,在Web应用子模块中,我正在使用maven-remote-resources-plugin进程将这些php文件复制到名为WEB-INF / php / submodule-name的资源目录中

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-remote-resources-plugin</artifactId>
      <version>1.5</version>
      <configuration>
          <resourceBundles>
              <resourceBundle>org.au.morph.offline:morph-sample:${project.version}</resourceBundle>
              <resourceBundle>org.au.morph.offline:morph-project:${project.version}</resourceBundle>
          </resourceBundles>
          <outputDirectory>src/main/webapp/WEB-INF</outputDirectory>
      </configuration>
      <executions>
          <execution>
              <goals>
                  <goal>process</goal>
              </goals>
          </execution>
      </executions>
  </plugin>

Finally, I created a utility method that resolves the correct path to this directory both when I run application from IDE or in the Tomcat: 最后,我创建了一个实用程序方法,当我从IDE或在Tomcat中运行应用程序时,该方法都会解析此目录的正确路径:

    public static String getWebContentPath(String contextPath) throws UnsupportedEncodingException {

    String path = PathUtils.class.getClassLoader().getResource("").getPath();
    String fullPath = URLDecoder.decode(path, "UTF-8");
    if(fullPath.contains("/WEB-INF/classes")){
        String pathArr[] = fullPath.split("/classes/");
        fullPath=pathArr[0];
    }
    String reponsePath = "";
    reponsePath = new File(fullPath).getPath() + File.separatorChar + "php"+File.separatorChar+contextPath;
    return reponsePath;
}

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

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