1. 安装Maven
去http://maven.apache.org/download.cgi
下载安装包,选择Link里已经打包好的Binary压缩包,选择Source的话需要自己build。
解压好下载的压缩文件后,将其放在希望保存的地址。
2. 配置Maven环境变量
Windows
右键此电脑 -> 属性 -> 高级系统设置 -> 环境变量
新建系统变量 -> 添加刚才解压后的文件的地址为MAVEN_HOME
编辑系统变量PATH -> 在末尾添加 %MAVEN_HOME%\bin
确定保存环境变量后,打开cmd或者powershell输入mvn -v
验证安装。
Unix/Linux
3. Eclipse里配置Maven环境
打开 Eclipse -> Windows -> Preferences -> Maven -> Installation -> 右侧Add
在弹框中的 Installation home 选中 MAVEN_HOME 的地址,Finish 并 Apply。
再转到 Eclipse -> Windows -> Preferences -> Maven -> User Settings
查看一下Global Settings 和 User Settings 的地址是否存在settings.xml,如果不存在的话这里复制一份Maven settings.xml过去。
4. 设置Maven镜像,不然Build会不成功或者非~常~慢~
打开刚才的settings.xml,添加阿里镜像到mirrors部分。
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
5. 添加本地仓库
本地仓库可以很好的帮我们管理当前机器所用的jar包。当我们从maven中获取jar包的时候,maven首先会在本地仓库中查找,如果本地仓库有则返回;如果没有则从远程仓库中获取包,并在本地库中保存。此外,我们在maven项目中运行mvn install,项目将会自动打包并安装到本地仓库中。
在 MAVEN_HOME里新建一个叫local-maven-repository
的文件夹。
更改settings.xml里的localRepository为刚才创建的文件夹。
<localRepository>C:\Program Files\Java\apache-maven-3.6.3\local-maven-repository</localRepository>
回到 Eclipse -> Preferences -> Maven -> User Settings 会发现Local Repository低地址已经根据刚才的改变自动改变了。
6. 新建一个Project
File -> New -> Maven Project,并选择workspace
7.选择项目类型
4.填写Group Id和Artifact Id
8.在pom.xml中注入SpringBoot依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>www.pde.com</groupId>
<artifactId>pde-staff</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>pde-staff Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath></relativePath>
</parent>
<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>
<finalName>pde-staff</finalName>
</build>
</project>
9. 出现错误
cvc-complex-type.2.3: Element 'parent' cannot have character [children], because the type's content type is element
问题的原因是pom.xml编码错误,可能是复制粘贴中混有特殊字符
重新手打一遍错误的部分,或者复制粘贴进word,去掉特殊字符再重新复制到IDE
10. 出现错误
Java compiler level does not match the version of the installed Java project face
问题原因:project compiler和本机Java compiler不一致
查看本机JDK和compiler版本:Eclipse -> Preferences
查看Maven Project的Java版本: 右键当前Project总目录 -> 最下方Properties
让以上保证一致
如果还不行的话,通过Eclipse -> Preferences -> Maven -> User Settings打开settings.xml,添加以下到profiles (jdk和compiler版本根据自己的添加)
<profile>
<id>jdk-1.8</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>
11. 尝试编写Hello World
项目结构概览
在/src/main/java下创建自己的包
包内创建一个Controller类,将我们连向默认网址
package com.pde_staff.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StaffController {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
}
再创建一个Hello class作为程序入口
package com.pde_staff.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Hello {
public static void main(String[] args) {
SpringApplication.run(Hello.class, args);
}
}
因为我的8080端口已经被占用,我还在/src/main/resources里添加了application.properties文件改变端口
右键/src/main/resources -> New -> File -> 设置名称为application.properties才会默认启用
#---------------------------------------------
# WEB PROPERTIES
#---------------------------------------------
server.port=6050
右键当前Project -> Maven -> Update Project更新一下
然后右键Hello.java -> Run As -> Java Application
可以在Console得到Spring启动的日志
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2020-05-09 15:42:19.102 INFO 6237 --- [ main] com.pde_staff.controller.Hello : Starting Hello on Yuchens-MacBook-Pro.local with PID 6237 (/Users/lyudmila/Desktop/projects/Pennsylvania_Education/pde-staff/target/classes started by lyudmila in /Users/lyudmila/Desktop/projects/Pennsylvania_Education/pde-staff)
2020-05-09 15:42:19.105 INFO 6237 --- [ main] com.pde_staff.controller.Hello : No active profile set, falling back to default profiles: default
2020-05-09 15:42:19.167 INFO 6237 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b53f538: startup date [Sat May 09 15:42:19 CST 2020]; root of context hierarchy
2020-05-09 15:42:20.614 INFO 6237 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 6050 (http)
2020-05-09 15:42:20.629 INFO 6237 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2020-05-09 15:42:20.630 INFO 6237 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2020-05-09 15:42:20.719 INFO 6237 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-09 15:42:20.719 INFO 6237 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1556 ms
2020-05-09 15:42:20.841 INFO 6237 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2020-05-09 15:42:20.846 INFO 6237 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-05-09 15:42:20.847 INFO 6237 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-05-09 15:42:20.848 INFO 6237 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-05-09 15:42:20.848 INFO 6237 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2020-05-09 15:42:21.172 INFO 6237 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4b53f538: startup date [Sat May 09 15:42:19 CST 2020]; root of context hierarchy
2020-05-09 15:42:21.270 INFO 6237 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.pde_staff.controller.StaffController.home()
2020-05-09 15:42:21.273 INFO 6237 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-05-09 15:42:21.274 INFO 6237 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-05-09 15:42:21.311 INFO 6237 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-09 15:42:21.311 INFO 6237 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-09 15:42:21.357 INFO 6237 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-09 15:42:21.528 INFO 6237 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2020-05-09 15:42:21.600 INFO 6237 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 6050 (http)
2020-05-09 15:42:21.605 INFO 6237 --- [ main] com.pde_staff.controller.Hello : Started Hello in 2.885 seconds (JVM running for 3.313)
打开浏览器输入http://localhost:6050/
12. 参考文献
eclipse中创建springboot项目参考文献
Java compiler level does not match参考文献
maven下载安装及在Eclipse中配置步骤
Maven配置阿里镜像参考文献
Maven项目命名规范