最新的Maven3.6联合Spring boot构建Java Web项目已经非常方便了,只需下载Maven3.6即可开始进行构建。
首先,确保系统安装了JDK:
[supervisor@localhost ~]$ java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
下载Maven3.6.1:
[supervisor@localhost ~]$ wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
--2019-04-15 20:04:34-- http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
正在解析主机 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在连接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9136463 (8.7M) [application/x-gzip]
正在保存至: “apache-maven-3.6.1-bin.tar.gz”
100%[================================================================================================================================================================================================>] 9,136,463 391KB/s 用时 15s
2019-04-15 20:04:49 (604 KB/s) - 已保存 “apache-maven-3.6.1-bin.tar.gz” [9136463/9136463])
将Maven安装包下载到/usr/java/
[supervisor@localhost ~]$ sudo mv apache-maven-3.6.1-bin.tar.gz /usr/java/.
[supervisor@localhost java]$ cd /usr/java
[supervisor@localhost java]$ sudo tar zxvf apache-maven-3.6.1-bin.tar.gz
[supervisor@localhost java]$ sudo mv apache-maven-3.6.1 apache-maven #目录改为apache-maven
[supervisor@localhost java]$ sudo vim /etc/profile #修改环境变量
在/etc/profile文件最后添加如下语句
78 #JAVA Environment
79 export JAVA_HOME=/usr/java/jdk1.8.0
80 export M2_HOME=/usr/java/apache-maven
81 export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
82 export PATH=$PATH:${JAVA_HOME}/bin:${M2_HOME}/bin
83
[supervisor@localhost java]$ su root
[root@localhost java]$ source /etc/profile #使环境变量配置立即生效
[root@localhost java]$ exit
[supervisor@localhost java]$ mvn -v #maven安装成功,显示版本信息
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/java/apache-maven
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
在项目目录下新建一个项目,并建立项目各子目录:
[supervisor@localhost projects]$ mkdir myproject
[supervisor@localhost projects]$ cd myproject/
[supervisor@localhost myproject]$ mkdir -p src/main/java/Example
在myproject目录下新建pom.xml文件
4.0.0
com.Example
myproject
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
org.springframework.boot
spring-boot-starter-web
输入命令mvn dependency:tree
maven将下载相关依赖项,注意不要修改maven中央仓库至阿里云,有些依赖项在阿里云上找不到,项目Build将会失败。
Spring Boot引入了Starter的概念,本例中spring-boot-starter-web就是一个Starter,Spring Boot将会生成一个Web应用。
经过一段时间的下载,最终显示Build成功,列出依赖项的Tree:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.Example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ myproject ---
[INFO] com.Example:myproject:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.26:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile
[INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.23:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile
[INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.16.Final:compile
[INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.4.0:compile
[INFO] +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile
[INFO] | \- org.springframework:spring-beans:jar:5.1.6.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:5.1.6.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:5.1.6.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.270 s
[INFO] Finished at: 2019-04-15T20:42:09+08:00
[INFO] ------------------------------------------------------------------------
这个过程实际上maven将项目所需的spring,tomcat,log4j 和相关的jboss工具以及其他一些相关的依赖项都下载并安装配置好了。
接下来建立一个新文件src/main/java/Example/Example.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
在myproject目录下输入 mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.Example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) > test-compile @ myproject >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/main/resources
[INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /alidata/projects/myproject/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ myproject ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) < test-compile @ myproject <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) @ myproject ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
2019-04-15 20:57:38.187 INFO 438 --- [ main] Example : Starting Example with PID 438 (/alidata/projects/myproject/target/classes started by root in /alidata/projects/myproject)
2019-04-15 20:57:38.239 INFO 438 --- [ main] Example : No active profile set, falling back to default profiles: default
2019-04-15 20:57:44.743 INFO 438 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-15 20:57:45.021 INFO 438 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-15 20:57:45.022 INFO 438 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-15 20:57:46.409 INFO 438 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-15 20:57:46.410 INFO 438 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7838 ms
2019-04-15 20:57:47.900 INFO 438 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-15 20:57:50.623 INFO 438 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-15 20:57:50.682 INFO 438 --- [ main] Example : Started Example in 14.93 seconds (JVM running for 33.934)
2019-04-15 20:58:09.252 INFO 438 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-15 20:58:09.253 INFO 438 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-15 20:58:09.294 INFO 438 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 41 ms
用浏览器打开