8000 Get current project root directory · allwaysoft/Java-Coding-Problems@66bc05a · GitHub
[go: up one dir, main page]

Skip to content

Commit 66bc05a

Browse files
committed
Get current project root directory
1 parent 0f4eb9f commit 66bc05a

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Converting `Date` to `YearMonth`
2+
Write a program that converts an `Date` to `YearMonth` and vice-versa.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.app</groupId>
5+
<artifactId>BONUS_1_GetCurrentProjectRootDirectory</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>13</maven.compiler.source>
11+
<maven.compiler.target>13</maven.compiler.target>
12+
</properties>
13+
<name>BONUS_1_GetCurrentProjectRootDirectory</name>
14+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package modern.challenge;
2+
3+
public class MainApplication {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("The root directory of this project is:\n"
8+
+ Roots.getCurrentProjectRootDirectory());
9+
}
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package modern.challenge;
2+
3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
5+
6+
public class Roots {
7+
8+
private Roots() {
9+
throw new AssertionError("Cannot be instantiatied");
10+
}
11+
12+
public static String getCurrentProjectRootDirectory() {
13+
14+
String userDirectory = System.getProperty("user.dir");
15+
Path rootDirectory = Paths.get(".").normalize().toAbsolutePath();
16+
17+
if (rootDirectory.startsWith(userDirectory)) {
18+
return rootDirectory.toString();
19+
} else {
20+
throw new RuntimeException("Cannot find the current project root directory");
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)
0