| 知乎专栏 |
目录
curl https://start.spring.io/starter.tgz \ -d artifactId=creds-example-server \ -d dependencies=security,web \ -d language=java \ -d type=maven-project \ -d baseDir=example-server \ | tar -xzvf -
<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>api.netkiller.cn</groupId> <artifactId>api.netkiller.cn</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Skyline</name> <description>skylinechencf@gmail.com</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source /> <target /> </configuration> </plugin> </plugins> </build> </project>
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
测试
curl http://127.0.0.1:8080/
spring-boot-maven-plugin 插件
将 resource 添加应用程序
<build>
<resources>
<resource>
<directory>src/main/java/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>*.jks</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
$ mvn spring-boot:run $ mvn -P prod spring-boot:run
-P 指定 Maven 的 profile,如果指定 Springboot 的 profiles 请使用 -Drun.profiles=prod
$ mvn spring-boot:run -Drun.profiles=prod
打包后,使用jar包运行
$ mvn verify $ mvn package $ java -jar target/api.netkiller.cn-0.0.1-SNAPSHOT.jar
Spring Boot 构建 Docker 镜像,你不需要写 Dockerfile,plugin 帮你完成。
只需要简单的执行:
mvn spring-boot:build-image
执行完成后会看到成功提示信息:
[INFO] Successfully buit image 'docker.io/library/demo:0.0.1-SNAPSHOT'
运行容器测试:
docker run -p 8000:8080 -t demo:0.0.1-SNAPSHOT
注意:这里映射的本机端口是8000。
curl http://localhost:8000/