【问题标题】:How do I change the working directory while running Jetty with Maven?如何在使用 Maven 运行 Jetty 时更改工作目录?
【发布时间】:2017-02-15 17:31:52
【问题描述】:

我正在使用 Maven 通过执行 mvn jetty:run 目标来运行 Jetty 服务器。在我的 Java 代码中,我使用java.io.File 读取文件,但我现有的代码假定该进程在我的basedir 下的/war 目录中运行。

使用 Maven Jetty 插件时如何设置工作目录?

详情: 这些文件不是直接在baseDirectory 下,而是在子目录下:具体来说,是webapp 目录,它被复制到${project.artifactId}-${project.version} 下的目标文件夹中

我想读取文件,而不必每次定义文件时都添加目标路径。

我可以使用读取文件。

File file = new File("target/myProject-myVersion/fileToRead.txt")

我只想做:

File file = new File("fileToRead.txt")

以下是项目结构。 (这是典型的 Maven webapp 结构。)

MyDir/
  [pom.xml]
  src/main/
    java/
      com.example.mycode/
        MyCode.java
    webapp/
      [fileToRead.txt]
      [index.html]
      [jsp.jsp]
      WEB-INF/
        [web.xml]

pom.xml 我有:

             <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.7.v20160115</version>
                <configuration>
                    <webAppSourceDirectory>${project.basedir}/target/${project.artifactId}-${project.version}</webAppSourceDirectory>
                </configuration>
            </plugin>

【问题讨论】:

    标签: maven jetty maven-jetty-plugin


    【解决方案1】:

    使用ServletContext.getRealPath(String) API。

    File file = new File(servletContext.getRealPath("/fileToRead.txt"));
    

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 2018-09-23
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 2017-09-03
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多