Maven快速使用教程(二) spring boot 項目構建


1. 使用Maven來構建Spring boot項目

訪問spring boot的官網。http://projects.spring.io/spring-boot/ 選擇當前最新穩定版本,當前的最新穩定版本是1.4.0 。找到Quick Start部分,如下圖所示:

wKiom1evsZWilY_cAAEJl6EEYps655.png-wh_50

2. 新建一個pro-springboot的Maven項目:

修改pom.xml,添加官網quick start建議添加的內容:

1
2
3
4
5
6
7
8
<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>

需要介紹2個標籤,分別是<parent>和<exclusion>。

<parent>標籤,表示要引用另外一個項目的pom.xml。表示這個項目依賴於parent標籤內的項目。

下面修改pom文件,並開發測試程序。

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<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.vip</groupId>
  <artifactId>pro-springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
 
  <name>pro-springboot</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
   
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
   
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
   
   
</project>

SampleController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.vip.pro_springboot;
 
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);
    }
}

直接右鍵SampleController.java,“run as Java Application”即可,控制檯出現下面的畫面:

wKioL1ewC-qRjJQfAADxlZkWC9I283.png-wh_50

在瀏覽器輸入http://localhost:8080/ 出現下面結果:

wKioL1ewDA6CzcZIAAAeqdAfIho260.png-wh_50


3. 遇到的問題:

在上面的開發過程中,曾經遇到問題,Maven項目前有個“紅色感嘆號”,Mave在下載相關依賴時實際上有問題,到配置的m2的repository目錄刪除相關文件夾,再右鍵項目,Maven --> Update Project...重新下載就行了。

在定位錯誤時,參看Eclipse的Problems標籤視圖,如下圖所示:

wKiom1ewlRfi-JzVAAC4lryEWfo548.png-wh_50

如果發現某個Maven下載的jar包有問題,根據errors裏的提示,到相應的maven本地repository下,刪除對應的目錄。


4. 嘗試修改應用的端口:

上邊的應用,已經能夠跑通了。但是,如果我們的8080端口已經被佔用了的話,需要修改spring mvc佔用的端口,該如何修改?

1.增加一個配置文件:

首先,在src/main下添加一個文件夾,叫resources,並把該文件夾添加到類路徑。

2. 新建一個文件(名爲resources):

在這個文件夾中新建文件application.properties,並在裏邊輸入:

1
server.port=8089

重新啓動後,端口號就改成8089了。只能通過該IP訪問:http://localhost:8089/。


5. Maven常用標籤總結:

1.<scope>標籤:

scope是作用域,依賴的作用域。可以在測試、編譯、運行、打包等等情況下依賴進來。默認是compile作用域。

2.<exclusion>標籤:

排除依賴。

3.不可以循環依賴。


6. maven的配置:

在搭建自己的maven的環境過程中,對2個地方進行了特殊設置,一個是maven默認使用的jdk的版本,一個是本地倉庫的位置(從中央倉庫將jar包等下載到哪個地方),這2個地方都是在$M2_HOME/conf/setting.xml中配置的。

1.本地倉庫位置:

1
<localRepository>D:\java_workdir\.m2\repository</localRepository>

2.默認使用的JDK的版本,在<profiles>標籤下配置:

1
2
3
4
5
6
7
8
9
10
11
12
<profile>  
    <id>jdk18</id>  
     <activation>  
          <activeByDefault>true</activeByDefault>  
          <jdk>1.8</jdk>  
     </activation>  
     <properties>  
          <maven.compiler.source>1.8</maven.compiler.source>  
          <maven.compiler.target>1.8</maven.compiler.target>  
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
     </properties>   
</profile>



本文出自 “雪花” 博客:http://6216083.blog.51cto.com/6206083/1836347



發佈了77 篇原創文章 · 獲贊 215 · 訪問量 82萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章