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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值