seefa / spring-boot-java-starter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Building Spring Boot application with Gradle plus Groovy

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:
    1. plugins -> java, org.springframework.boot
    2. apply plugin -> io.spring.dependency-management
    3. Project properties: Group,Version,SourceCompatibility
    4. repositories
    5. 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.

About


Languages

Language:Java 100.0%