Spring Boot 學習 (1): 初始化工程

spring boot 項目初始化,介紹三種方式:IntelliJ 建立、Spring CLI 建立以及手動建立,工程使用 gradle 構建工具。java

IntelliJ建立

  1. 選擇 spring initializr
    intellj_create_new1
  2. 填寫本身想要的配置信息
    intellj_create_new2
  3. 選擇依賴包:
    intellj_create_new3
  4. 配置工程名和工程所在目錄:
    intellj_create_new4
    進入到工程,以下圖所示:

intellj_create_new5

建立完成。web

Spring CLI建立

示例:spring

spring init -dweb,data-jpa,h2,thymeleaf --build gradle initbycli
複製代碼

運行命令後會顯示:vim

Using service at https://start.spring.io
Project extracted to '<current_path>/initbycli'
複製代碼

執行完成會看到當前目錄下會多出 initbycli 目錄.tomcat

.
└── initbycli
    ├── HELP.md
    ├── build.gradle
    ├── gradle
    │   └── wrapper
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── gradlew
    ├── gradlew.bat
    ├── settings.gradle
    └── src
        ├── main
        │   ├── java
        │   │   └── com
        │   │       └── example
        │   │           └── initbycli
        │   │               └── DemoApplication.java
        │   └── resources
        │       ├── application.properties
        │       ├── static
        │       └── templates
        └── test
            └── java
                └── com
                    └── example
                        └── initbycli
                            └── DemoApplicationTests.java
複製代碼

能夠看到基本工程目錄文件已經建立好了。bash

再看一下 Spring CLI 的說明:app

$ spring help init
spring init - Initialize a new project using Spring Initializr (start.spring.io)

usage: spring init [options] [location]

Option                       Description
------                       -----------
-a, --artifactId <String>    Project coordinates; infer archive name (for
                               example 'test')
-b, --boot-version <String>  Spring Boot version (for example '1.2.0.RELEASE')
--build <String>             Build system to use (for example 'maven' or
                               'gradle') (default: maven)
-d, --dependencies <String>  Comma-separated list of dependency identifiers to
                               include in the generated project
--description <String>       Project description
-f, --force                  Force overwrite of existing files
--format <String>            Format of the generated content (for example
                               'build' for a build file, 'project' for a
                               project archive) (default: project)
-g, --groupId <String>       Project coordinates (for example 'org.test')
-j, --java-version <String>  Language level (for example '1.8')
-l, --language <String>      Programming language  (for example 'java')
--list                       List the capabilities of the service. Use it to
                               discover the dependencies and the types that are
                               available
-n, --name <String>          Project name; infer application name
-p, --packaging <String>     Project packaging (for example 'jar')
--package-name <String>      Package name
-t, --type <String>          Project type. Not normally needed if you use --
                               build and/or --format. Check the capabilities of
                               the service (--list) for more details
--target <String>            URL of the service to use (default: https://start.
                               spring.io)
-v, --version <String>       Project version (for example '0.0.1-SNAPSHOT')
-x, --extract                Extract the project archive. Inferred if a
                               location is specified without an extension
複製代碼

說明:maven

  • 依賴: 使用 -d, --dependencies <String>, , 號分割.
  • 項目構建類型: --build <String>, 默認爲 maven, 另外一選項是 gradle.
  • 初始化類型: --format <String>, 默認爲 project, 會初始化整個工程的目錄結構。可選項是 build, 只會生成工程所須要的 build.gradle 文件.
  • 查看有哪些能夠配置的:--list, 命令輸出之後,內容包括可選的依賴包,工程類型和構建屬性( java 版本等).

手動建立

  1. 創建工程目錄: mkdir initbyselfide

  2. 在工程目錄下創建 build.gradle 文件,spring-boot

    cd initbyself
    vim build.gradle
    複製代碼

    build.gradle 內容爲:

    plugins {
    		id 'org.springframework.boot' version '2.1.3.RELEASE'
    		id 'java'
    	}
    	
    	apply plugin: 'io.spring.dependency-management'
    	
    	group = 'com.example'
    	version = '0.0.1-SNAPSHOT'
    	sourceCompatibility = '1.8'
    	
    	repositories {
    		mavenCentral()
    	}
    	
    	dependencies {
    		implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    		implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    		implementation 'org.springframework.boot:spring-boot-starter-web'
    		runtimeOnly 'com.h2database:h2'
    		testImplementation 'org.springframework.boot:spring-boot-starter-test'
    	}
    複製代碼
  3. 建立 setting.gradle 文件

pluginManagement {
		repositories {
			gradlePluginPortal()
		}
	}
	rootProject.name = 'initbyself'

複製代碼
  1. 建立java代碼目錄
    mkdir -p src/main/java/com/example/initbyself
    mkdir -p src/main/java/resources
    複製代碼
  2. 建立 application
  • 切換到代碼目錄 cd src/main/java/com/example/initbyself
  • 編寫源碼文件 DemoApplication.java
    package com.example.initbyself;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    }
    
    複製代碼
  1. 運行 gradle build, 結果爲:
    > Task :build
    Skipping task ':build' as it has no actions.
    :build (Thread[Daemon worker Thread 2,5,main]) completed. Took 0.0 secs.
    
    BUILD SUCCESSFUL in 3s
    2 actionable tasks: 2 executed
    複製代碼
  2. 運行項目:
java -jar build/libs/initbyself-0.0.1-SNAPSHOT.jar

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE) . . . . . 2019-03-07 00:17:36.996 INFO 11848 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-03-07 00:17:36.999 INFO 11848 --- [ main] com.example.initbyself.DemoApplication : Started DemoApplication in 19.497 seconds (JVM running for 19.992) 複製代碼

此時,已說明工程初始化成功了。

小結

以上是 Spring Boot 項目的初始化的三種方式,不必定全。第一種和第二種都依賴 https://start.spring.io 這個地址,這是官方提供的快速初始化方式,第三種是咱們全手動一步一步初始化,須要對構建工具十分熟悉。

相關文章
相關標籤/搜索