I don't know what number it is, but for learning, I created a program that displays "Hello world." Using Java and Gradle.
We have confirmed the operation in the above environment. However, it's only a simple matter, so you should be able to read the slight differences in the versions as appropriate.
To create a Gradle project, first create a suitable folder. The command looks like this:
mkdir hello-gradle
cd hello-gradle
I named the folder hello-gradle
here.
Next, create a project with the gradle command.
Make the folder where you want to create the project the current directory (that is, cd hello-gradle
and inside the folder) and execute the following command.
gradle init --type java-application
Then you will be asked some questions.
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2]
Select whether to write the script that sets the build in Groovy or Kotlin. For Java projects, you would normally use Groovy, so type "1" and press Enter.
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Then select a test framework. This time it's just Hello world, so you can test anything, but for now, type "4" and press Enter.
Project name (default: hello-gradle):
Source package (default: hello.gradle):
Finally, you will be asked for "Project name" and "Source package" respectively. Here, we will proceed by default, and enter without entering anything.
This should generate various files in the hello-gradle
folder.
All the source code needed to "Hello world." Is already in place.
The main method that outputs the most important "Hello world." As standard is in src / main / java / hello / gradle / App.java
.
In addition, I did gradle init --type java-application
at first, but if you do not specify --type
and just gradle init
alone, you should get the same result with only a few more questions.
However, if you want to create a Java command line application, it is faster to specify --type java-application
.
The types that can be specified for --type
are described in Supported Gradle build types. For example, it seems that you can create a Kotlin project by specifying kotlin-application
.
In the hello-gradle
folder, at the command prompt,
gradlew.bat run
Execute the command of. Then the character string Hello world.
should be output. This completes Hello world with Java + Gradle.
Since it is a program that only outputs Hello world as standard, there is no point in testing it, but since the test code is prepared, I will also run the test.
At the command prompt, just like when checking the operation
gradlew.bat test
To execute.
The test will then succeed and you will see BUILD SUCCESS FUL
.
The test code is in src / test / java / hello / gradle / AppTest.java
.
The ʻappHasAGreetingmethod of this class is the test method. This test only verifies that the
getGreeting method of the ʻApp
class, which has the main method, does not return null. This method returns the string Hello world.
, so of course the test passes.
For example, this getGreeting
method
package hello.gradle;
public class App {
public String getGreeting() {
// return "Hello world.";
return null;
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
If I change it to return null and run the test, then the test fails, with the words > Task: test FAILED
and the failed test method.
Recommended Posts