YUHEE1984 / CatsOneday

1 stars 0 forks source link

02 - It's a Real Project Now! #2

Closed TheBeege closed 4 months ago

TheBeege commented 5 months ago

Goal

Set up a real project tool chain. This will be the second important, non-Java thing for you to learn. (The first was Git. Good job!)

You're going to set up Gradle, a very common build tool in the Java ecosystem. Gradle is used to download libraries to help you code and to set up tasks like automatically building your code or testing your code. Gradle and it's competitor, Maven, are the main build tools for Java. Every decent engineer knows these tools at least a little bit.

Steps

From now on, we'll always assume you're in the CatsOneday project directory.

Setting Up the Gradle Project

  1. Install Gradle with brew install gradle.
  2. Run gradle init --type java-application --dsl groovy to turn this project into a Gradle project.
    1. You'll be prompted to enter your target Java version. You have Java version 19 right now (try java -version in another terminal to check), so type 19 and press enter.
    2. It will ask you to choose an application structure. We're just writing a single application, not some software library that we'll put on the internet for other people to use. So type 1 and hit enter.
    3. For testing framework, just hit enter to choose the default (JUnit Jupiter).
    4. For using new APIs and behavior, just hit enter to choose the default (no).
  3. Look at all the new files! Gradle has set up a complete Java project for you. Don't panic! You don't need to know what every little piece does yet.
  4. Check out the app folder. You'll see it's defined a folder structure app/src/main/java/org/example. The org/example part is the package name. You may remember this from your reading. Check out the Java source file for org.example.App.
  5. Delete Main.java. We'll use the App.java instead.

Using Gradle

  1. Type ./gradlew run in your terminal and hit enter. This will download any dependencies, automatically build your Java files, and run your main Java file (App.java).
  2. In your org.example.App class, try changing "Hello World!" to "Hello cats!".
  3. Run ./gradlew run again. You'll see Hello cats! print for the :app:run task.
    • "But Beege! How does Gradle know to run org.example.App?!" Check out the build.gradle file. This is where most of your configuration of Gradle will happen. There's a mainClass definition for the path to your main class file. See the current value? Later, you'll need to learn more about Gradle, but you can mostly ignore it for now.

Making the Package Yours

Our project runs, but our organization org isn't "example." This is Yuhee's project! It's Yuhee's CatsOneday project! Let's rename our package to match.

  1. Rename the example folder to yuhee. (In terminal, you can use mv.)
  2. Create a new folder under yuhee called catsoneday. (In terminal, you can use mkdir.)
  3. Move App.java to the catsoneday folder. (In termina, you can use mv.)
  4. In App.java, change org.example to org.yuhee.catsonday
  5. Update build.gradle's mainClass value to org.yuhee.catsoneday.App.
  6. Run ./gradlew run to make sure everything still works.
  7. In app/src/test/java, make similar changes to ensure everything matches.

Congratulations! You've set up your first Gradle project. As a junior engineer, you won't mess with Gradle too much except for adding dependencies, which we'll cover later. Read about it if you're curious, but don't go too deep, yet!

Don't forget to commit and push your work! See issue #1 if you need a reminder on how to do it. Also, a tip: if you include something like Closes #2 in the commit message, it will automatically close this issue. Try it after you're sure your code works.

YUHEE1984 commented 1 month ago

Reviewed it.