Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java support #48

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiled_starters/java/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions compiled_starters/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_grep.jar
target/*
target/
.idea/
40 changes: 40 additions & 0 deletions compiled_starters/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for Java solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `src/main/java/Main.java`.
Study and uncomment the relevant code, and push your changes to pass the first
stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `java (21)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`src/main/java/Main.java`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions compiled_starters/java/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Java version used to run your code
# on Codecrafters.
#
# Available versions: java-21
language_pack: java-21
52 changes: 52 additions & 0 deletions compiled_starters/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-grep</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>java_grep</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
24 changes: 24 additions & 0 deletions compiled_starters/java/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args){
// You can use print statements as follows for debugging, they'll be visible when running tests.
System.out.println("Logs from your program will appear here!");

if(args.length < 2 || !args[0].equals("-E")) {
System.out.println("Expected first argument to be -E");
return;
}

final String pattern = args[1];
final Scanner input = new Scanner(System.in);
final String inputLine = input.nextLine();

// Uncomment this block to pass the first stage
// if (inputLine.contains(pattern)) {
// System.exit(0);
// } else {
// System.exit(1);
// }
}
}
10 changes: 10 additions & 0 deletions compiled_starters/java/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
mvn -B --quiet package -Ddir=/tmp/codecrafters-grep-target
exec java -jar /tmp/codecrafters-grep-target/java_grep.jar "$@"
2 changes: 2 additions & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ languages:
release_status: "alpha"
alpha_tester_usernames: ["rohitpaulk", "sreeram-venkitesh"]
- slug: "rust"
- slug: "java"
release_status: "beta"

marketing:
difficulty: medium
Expand Down
16 changes: 16 additions & 0 deletions dockerfiles/java-21.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM maven:3.9.5-eclipse-temurin-21-alpine

COPY pom.xml /app/pom.xml

WORKDIR /app

# Download the dependencies
RUN mvn -B package -Ddir=/tmp/codecrafters-grep-target

# Cache Dependencies
RUN mkdir -p /app-cached
RUN mv /app/target /app-cached # Is this needed?

# Pre-compile steps
RUN echo "cd \${CODECRAFTERS_SUBMISSION_DIR} && mvn -B package -Ddir=/tmp/codecrafters-grep-target && sed -i 's/^\(mvn .*\)/#\1/' ./your_grep.sh" > /codecrafters-precompile.sh
RUN chmod +x /codecrafters-precompile.sh
1 change: 1 addition & 0 deletions solutions/java/01-init/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions solutions/java/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_grep.jar
target/*
target/
.idea/
40 changes: 40 additions & 0 deletions solutions/java/01-init/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for Java solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `src/main/java/Main.java`.
Study and uncomment the relevant code, and push your changes to pass the first
stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `java (21)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`src/main/java/Main.java`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions solutions/java/01-init/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Java version used to run your code
# on Codecrafters.
#
# Available versions: java-21
language_pack: java-21
52 changes: 52 additions & 0 deletions solutions/java/01-init/code/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-grep</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>java_grep</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>Main</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
20 changes: 20 additions & 0 deletions solutions/java/01-init/code/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args){
if(args.length < 2 || !args[0].equals("-E")) {
System.out.println("Expected first argument to be -E");
return;
}

final String pattern = args[1];
final Scanner input = new Scanner(System.in);
final String inputLine = input.nextLine();

if (inputLine.contains(pattern)) {
System.exit(0);
} else {
System.exit(1);
}
}
}
10 changes: 10 additions & 0 deletions solutions/java/01-init/code/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
mvn -B --quiet package -Ddir=/tmp/codecrafters-grep-target
exec java -jar /tmp/codecrafters-grep-target/java_grep.jar "$@"
30 changes: 30 additions & 0 deletions solutions/java/01-init/diff/src/main/java/Main.java.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@@ -1,24 +1,20 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args){
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- System.out.println("Logs from your program will appear here!");
-
if(args.length < 2 || !args[0].equals("-E")) {
System.out.println("Expected first argument to be -E");
return;
}

final String pattern = args[1];
final Scanner input = new Scanner(System.in);
final String inputLine = input.nextLine();

- // Uncomment this block to pass the first stage
- // if (inputLine.contains(pattern)) {
- // System.exit(0);
- // } else {
- // System.exit(1);
- // }
+ if (inputLine.contains(pattern)) {
+ System.exit(0);
+ } else {
+ System.exit(1);
+ }
}
}
20 changes: 20 additions & 0 deletions starter-repository-definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@
template_attributes:
required_executable: "node (18)"
user_editable_file: "app/main.js"

- language: java
file_mappings:
- source: starter_templates/README.md
target: README.md
- source: starter_templates/codecrafters.yml
target: codecrafters.yml
- source: starter_templates/java/your_grep.sh
target: your_grep.sh
- source: starter_templates/java/.gitignore
target: .gitignore
- source: starter_templates/java/src/main/java/Main.java
target: src/main/java/Main.java
- source: starter_templates/java/pom.xml
target: pom.xml
- source: starter_templates/.gitattributes
target: .gitattributes
template_attributes:
required_executable: "java (21)"
user_editable_file: "src/main/java/Main.java"
4 changes: 2 additions & 2 deletions starter_templates/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ language_pack: elixir-1.10
language_pack: kotlin-1.4
{{/ language_is_kotlin }}
{{# language_is_java }}
# Available versions: java-1.8
language_pack: java-1.8
# Available versions: java-21
language_pack: java-21
{{/ language_is_java }}
{{# language_is_nim }}
# Available versions: nim-1.0
Expand Down
4 changes: 4 additions & 0 deletions starter_templates/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
java_grep.jar
target/*
target/
.idea/
Loading