如何在使用 Maven 运行 Jetty 时更改工作目录?
How do I change the working directory while running Jetty with Maven?
我是 运行 通过执行 mvn jetty:run
目标使用 Maven 的 Jetty 服务器。在我的 Java 代码中,我使用 java.io.File
读取文件,但我现有的代码假设进程是 basedir
下的 /war
目录中的 运行。 =23=]
使用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>
使用 ServletContext.getRealPath(String)
API.
File file = new File(servletContext.getRealPath("/fileToRead.txt"));
我是 运行 通过执行 mvn jetty:run
目标使用 Maven 的 Jetty 服务器。在我的 Java 代码中,我使用 java.io.File
读取文件,但我现有的代码假设进程是 basedir
下的 /war
目录中的 运行。 =23=]
使用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>
使用 ServletContext.getRealPath(String)
API.
File file = new File(servletContext.getRealPath("/fileToRead.txt"));