1-Make a Gradle Project by default language syntax with IDE
2- Make Project package structure(ir.seefa.sample.project) in src/main/java and src/test/java
- Hint: Be careful about word spelling for packages because Test classes will not able to run SpringBoot Test Runner if Packages or Classes are not match names.
3- add below codes to build.gradle or build.gradle.kts to get Spring Boot dependencies and project features(Project name, Project version, Java plugin, dependencies, Add a task to making JAR file, Java configuration, so on)
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'ir.seefa.sample.project'
version = '1.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}
- Tips:
- plugins -> java, org.springframework.boot
- apply plugin -> io.spring.dependency-management
- Project properties: Group,Version,SourceCompatibility
- repositories
- dependencies implementation -> spring-boot-starter-web, testImplementation -> spring-boot-starter-test'
4- Add SpringBootStarterApplication class to has been made package from Step 2 with following code:
package ir.seefa.sample.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStarterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarterApplication.class, args);
}
}
5- Add application.properties file to src/main/resources path for apply application configuration before run such server port as follows:
server.port=9090
6- Add SpringBootStarterApplicationTests class to /src/test/java/[Package_Name] with following codes:
package ir.seefa.sample.project;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootStarterApplicationTests {
@Test
public void contextLoads() {
}
}
7- run command gradle in command line
8- run command gradle tasks in command line
9- Build your project with Gradle Wrapper => Run in command line(gradle wrapper --gradle-version 5.4.1)
10- After passing step No.8, we can only run this command to build project(./gradlew build)
11- Run this command will execute our Application(./gradlew run) or Run SpringBootStarterApplication directly.