Spring Boot Reference Guide - Spring Boot Installation (Maven Installation, Gradle Installation)

Spring Boot installation

Spring Boot can be used with typical Java development tools or installed as a command-line tool. Either way, you will need Java SDK v1.6  or higher installed. Before starting, you need to check the currently installed Java version:

$ java -version

If you are new to Java, or you just want to experience Spring Boot, you may want to try [Spring Boot CLI](10.2. Installing the Spring Boot CLI.md) first, otherwise continue reading the classic installation guide.

Note : Although Spring Boot is compatible with Java 1.6, you should consider using the latest version of Java if possible

Installation Guide for Java Developers

You can use Spring Boot just like any other standard Java library, simply by including the correct spring-boot-*.jarfiles on your classpath. Spring Boot does not require any special tool integration, so you can use any IDE or text editor; there is nothing special about Spring Boot applications, so you can run and debug like any other Java program.

Although you can copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).

Maven installation

Spring Boot is compatible with Apache Maven 3.2 or later. If Maven is not installed, you can refer to the maven.apache.org guide.

Note : On many operating systems, you can install Maven through a package manager. If you're an OSX Homebrew user, give it a try brew install maven. Ubuntu users can run sudo apt-get install maven.

The groupId that Spring Boot depends on is org.springframework.boot. Usually your Maven POM file needs to inherit spring-boot-starter-parentand then declare one or more ["Starter POMs"](../III. Using Spring Boot/13.4. Starter POMs.md) dependencies. Spring Boot also provides a [Maven plugin](../VIII. Build tool plugins/58. Spring Boot Maven plugin.md) for creating executable jars.

The following is a typical pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>

Note : spring-boot-starter-parentis a nice way to use Spring Boot, but it is not always suitable. Sometimes you need to inherit from a different parent POM, or maybe you just don't like our default configuration. See [Section 13.1.2, “Using Spring Boot without the parent POM”](../III. Using Spring Boot/13.1.2. Using Spring Boot without the parent POM.md) for an alternative solution using import

Gradle installation

Spring Boot is compatible with Gradle 1.12 or higher. If Gradle is not installed, you can refer to the guide at www.gradle.org .

Spring Boot dependencies can be org.springframework.boot groupdeclared using Typically, your project will declare one or more ["Starter POMs"](../III. Using Spring Boot/13.4. Starter POMs.md ) dependencies. Spring Boot provides a useful [Gradle plugin](../VIII. Build tool plugins/59. Spring Boot Gradle plugin.md) for simplifying dependency declarations and creating executable jars.

Note : The Gradle Wrapper provides a nifty way of getting Gradle when you need to build a project. It's a small script and library that you commit along with your code to start the build process. Refer to Gradle Wrapper for details .

Here is a typical build.gradlefile:

buildscript {
    repositories {
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.BUILD-SNAPSHOT")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

jar {
    baseName = 'myproject'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Guess you like

Origin blog.csdn.net/2301_76484015/article/details/130485311