|
17 | 17 |
|
18 | 18 | import java.io.File; |
19 | 19 | import java.io.Serializable; |
20 | | -import java.net.URL; |
21 | 20 | import java.nio.file.Path; |
22 | 21 | import java.nio.file.Paths; |
23 | 22 |
|
24 | | -import org.slf4j.LoggerFactory; |
25 | | - |
26 | 23 | import com.vaadin.flow.internal.hilla.EndpointRequestUtil; |
| 24 | +import com.vaadin.flow.server.frontend.FileIOUtils; |
27 | 25 | import com.vaadin.flow.server.frontend.FrontendUtils; |
28 | 26 |
|
29 | 27 | import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_DISABLE_XSRF_PROTECTION; |
@@ -190,48 +188,36 @@ default File getProjectFolder() { |
190 | 188 | } |
191 | 189 |
|
192 | 190 | String folder = getStringProperty(FrontendUtils.PROJECT_BASEDIR, null); |
193 | | - if (folder == null) { |
194 | | - /* Try determining the project folder from the classpath. */ |
195 | | - try { |
196 | | - URL url = getClass().getClassLoader().getResource("."); |
197 | | - if (url != null && url.getProtocol().equals("file")) { |
198 | | - // URI decodes the path so that e.g. " " works correctly |
199 | | - String path = url.toURI().getPath(); |
200 | | - if (path.endsWith("/target/classes/")) { |
201 | | - folder = path.replaceFirst("/target/classes/$", ""); |
202 | | - } |
203 | | - } |
204 | | - } catch (Exception e) { |
205 | | - LoggerFactory.getLogger(getClass()).warn( |
206 | | - "Unable to determine project folder using classpath", |
207 | | - e); |
208 | | - } |
| 191 | + if (folder != null) { |
| 192 | + return new File(folder); |
| 193 | + } |
| 194 | + |
| 195 | + File projectFolder = FileIOUtils.getProjectFolderFromClasspath(); |
| 196 | + if (projectFolder != null) { |
| 197 | + return projectFolder; |
209 | 198 | } |
210 | 199 |
|
211 | | - if (folder == null) { |
212 | | - /* |
213 | | - * Accept user.dir or cwd as a fallback only if the directory seems |
214 | | - * to be a Maven or Gradle project. Check to avoid cluttering server |
215 | | - * directories (see tickets #8249, #8403). |
216 | | - */ |
217 | | - String baseDirCandidate = System.getProperty("user.dir", "."); |
218 | | - Path path = Paths.get(baseDirCandidate); |
219 | | - if (path.toFile().isDirectory() |
220 | | - && (path.resolve("pom.xml").toFile().exists() || path |
221 | | - .resolve("build.gradle").toFile().exists())) { |
222 | | - return path.toAbsolutePath().toFile(); |
223 | | - } else { |
224 | | - throw new IllegalStateException(String.format( |
225 | | - "Failed to determine project directory for dev mode. " |
226 | | - + "Directory '%s' does not look like a Maven or " |
227 | | - + "Gradle project. Ensure that you have run the " |
228 | | - + "prepare-frontend Maven goal, which generates " |
229 | | - + "'flow-build-info.json', prior to deploying your " |
230 | | - + "application", |
231 | | - path.toString())); |
232 | | - } |
| 200 | + /* |
| 201 | + * Accept user.dir or cwd as a fallback only if the directory seems to |
| 202 | + * be a Maven or Gradle project. Check to avoid cluttering server |
| 203 | + * directories (see tickets #8249, #8403). |
| 204 | + */ |
| 205 | + String baseDirCandidate = System.getProperty("user.dir", "."); |
| 206 | + Path path = Paths.get(baseDirCandidate); |
| 207 | + if (path.toFile().isDirectory() |
| 208 | + && (path.resolve("pom.xml").toFile().exists() |
| 209 | + || path.resolve("build.gradle").toFile().exists())) { |
| 210 | + return path.toAbsolutePath().toFile(); |
| 211 | + } else { |
| 212 | + throw new IllegalStateException(String.format( |
| 213 | + "Failed to determine project directory for dev mode. " |
| 214 | + + "Directory '%s' does not look like a Maven or " |
| 215 | + + "Gradle project. Ensure that you have run the " |
| 216 | + + "prepare-frontend Maven goal, which generates " |
| 217 | + + "'flow-build-info.json', prior to deploying your " |
| 218 | + + "application", |
| 219 | + path.toString())); |
233 | 220 | } |
234 | | - return new File(folder); |
235 | 221 | } |
236 | 222 |
|
237 | 223 | /** |
|
0 commit comments