使用 maven 创建 SpringBoot 项目有几种方式, 使用 spring-boot-starter-parent 和使用 spring-boot-dependencies , 同理 springcloud 项目也可以使用两种方式创建, 推荐使用 dependencyManagement, 使用 IDEA 直接创建也是可以的!
一, 使用 spring-boot-starter-parent
- <!-- SpringBoot 应用的打包插件 -->
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.4.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
使用 spring-boot-starter-parent 虽然方便, 但是很多开发场景下需要用到自己公司的 parent, 如果这个时候还想进行项目依赖版本的统一管理, 就需要使用 spring-boot-dependencies 这种方式来实现了!
二, 使用 spring-boot-dependencies , 推荐使用此种方式
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-Web</artifactId> </dependency> </dependencies>
编写写一个主类和一个 Controller:
@SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } } //---------------------------------------------------------------- @RestController @ResponseBody public class HelloController { @RequestMapping("/hello") public String hello(){ return "Hello SpringBoot Application"; } }
在这里插入图片描述
三, 最省力的方式创建 SpringBoot 应用
在这里插入图片描述
Tip: 如何定制 banner?
http://www.network-science.de/ascii/ 在这个网站里定制你的启动文字, 然后新建一个 banner.txt 放在资源文件夹即可:
在这里插入图片描述
在这里插入图片描述
最后说一个小问题, 第二种配置方式打包成独立 Jar 包的方法:
<?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> <packaging>jar</packaging> <groupId>edu.xpu</groupId> <artifactId>springboot-03-hello</artifactId> <version>1.0</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-Web</artifactId> </dependency> </dependencies> </project>
来源: http://www.jianshu.com/p/63853ce319d1