初學springboot, 如何快速使用maven搭建springboot項目呢
阿新 • • 發佈:2017-06-18
apache 提示框 code 搭建 目錄結構 oot dem plugin prop
搭建Springboot 推薦使用maven, 但也可以不使用maven,用正常的java工程。
- 在MyEclipse上新建一個maven工程
- 點擊Next,選擇create a simple project
- 點擊Next,輸入下圖所示
- 點擊finish,稍等一下會,項目就構建完成。目錄結構如下所示:
- 上圖中的pom.xml提示錯誤,點開pom.xml,切換至:Dependencies視圖,點擊紅色錯誤,彈出提示框,點擊+號。完成後pom.xml將不會報錯。
- pom.xml增加繼承父包
<!-- 繼承父包--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent>
- pom.xml增加依賴關系
<dependencies> <!-- spring-boot的web啟動的jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- pom.xml 增加熱部署,可選
<!-- maven 插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </dependencies> </plugin>
- 完整的pom.xml文件內容
<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>org.springboot</groupId> <artifactId>SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 繼承父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot的web啟動的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 配置java版本 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <!-- 配置war版本 --> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <version>3.1</version> </configuration> </plugin> <!-- maven 插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build> <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>
- 如果你希望他是web項目的話,選擇項目-右鍵-Properties-MyEclipse-Project fcets
- 點擊上圖的Further configuration available,輸入路徑src/main/webapp
- 點擊OK,在點擊apply,點擊OK,目錄發生變化,如下圖所示
- 下面接著,如何將jar包應用雨lib中呢? 選擇項目右鍵-properties-MyEclipse-Deployment Assembly
- 點擊add按鈕,選擇java build path Entries
- 點擊next,選擇Maven Dependencies,點擊finsh
- 點擊apply,點擊ok,完成jar包的應用,此時srping boot 的web工程基本已部署完畢,下面來簡單寫一個列子
- 新建一個包含main方法的類,此地方起名用Application
@SpringBootApplication public class Application { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(Demo.class, args); } }
- 再創建一個不需要main方法的demo類
@RestController @EnableAutoConfiguration public class Demo { @RequestMapping("/") String helloWord() { return "helloWorld"; } }
- 此時小列子已經寫完了,由於spring boot 內置了servlet容器,所以,我們可以直接運行application類,選擇項目-run as -- java application,我們將開到下圖信息,這樣表示已經完成。
- 上圖表示已經成功運行,下面我們來訪問一下,打開localhost:8080,將會出現hello word信息。
- 到此,spring boot 通過maven搭建已經完成。
初學springboot, 如何快速使用maven搭建springboot項目呢