Eclipse 中构建 Maven 项目的完整过程 - SpringBoot 项目
进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件)
一、Maven项目的新建
1、鼠标右键---->New----->Other...
2、直接点击下一步
3、默认的是maven-archetype-quickstart,不用修改,直接点击下一步
4、 Group Id 中输入项目的基本包名。
Artifact Id 中输入项目名。
Version 中的值默认就行,不进行选择。
Package 中写的是默认生成的一个包名,不写也可以。
接着点击完成就可以了。
5、创建好项目后结构如下:
这就是个普通的maven项目,也就是单机版项目。
二、Maven项目的配置
首先我们配一下项目的jdk,检查下项目的输出位置,Java Complier 不用管,因为接下来我们会在pom.xml 中配置使用maven的编译插件。
这里一般还需要新建两个资源目录,src/main/resources和src/test/resources,并且添加相应的输出路径。具体可参考我的另一篇博客----Eclipse中构建Maven项目的完整过程---普通Web项目.
接下来我们配置项目的pom.xml ,根据需要进行配置,我这里提供一个基础版的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>com.sumavision.sums</groupId> <artifactId>wangbo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wangbo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <mybatis.spring.boot.version>1.2.0</mybatis.spring.boot.version> </properties> <parent> <!-- 从springboot中继承默认值 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <!-- 为web应用程序添加典型的依赖项 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mysql连接 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 缓存 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> </dependencies> <build> <plugins> <!-- maven编译打包插件 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- springboot编译,生成可执行jar包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置完pom.xml 后对项目进行刷新,右键项目----->Maven----->Update Project...
接下来写及几个类,测试一下服务。
目录结构:
App.java
package com.sumavision.wangbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * SpringBoot 项目启动类 * */ @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } }
UserController.java
package com.sumavision.wangbo.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping("/user/{name}") public String login(@PathVariable("name") String name){ return name+"登陆成功!"; } @RequestMapping("/xxx/{name}") public String loginout(@PathVariable("name") String name){ return name+"退出成功!"; } }
三、项目运行
可以选择在eclipse中直接右键运行App.java 类中的main方法,直接就可以在浏览器中访问了。这种比较简单就不说了。
说一下打jar包的运行方式:
项目右键----->Run As------->Maven build...,接着在Goals后面写上打包命令,一般是先清除,再打包,所以写的是clean package,接着点击Run就可以了。
控制台显示的打包日志
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building wangbo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ wangbo --- [INFO] Deleting H:\workspace\learn\wangbo\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wangbo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory H:\workspace\learn\wangbo\src\main\resources [INFO] skip non existing resourceDirectory H:\workspace\learn\wangbo\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ wangbo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to H:\workspace\learn\wangbo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wangbo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory H:\workspace\learn\wangbo\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ wangbo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to H:\workspace\learn\wangbo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ wangbo --- [INFO] Surefire report directory: H:\workspace\learn\wangbo\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.sumavision.wangbo.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in com.sumavision.wangbo.AppTest Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ wangbo --- [INFO] Building jar: H:\workspace\learn\wangbo\target\wangbo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:repackage (default) @ wangbo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.016 s [INFO] Finished at: 2017-08-19T17:17:24+08:00 [INFO] Final Memory: 30M/277M [INFO] ------------------------------------------------------------------------
显示打包成功,jar 包位于 H:\workspace\learn\wangbo\target\wangbo-0.0.1-SNAPSHOT.jar
接着可以直接在命令行中通过java -jar 命令启动该jar包,可以看到以下日志
C:\Users\Administrator>java -jar H:\workspace\learn\wangbo\target\wangbo-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.6.RELEASE) 2017-08-19 17:20:59.133 INFO 7180 --- [ main] com.sumavision.wangbo.App : Starting App v0.0.1-SNAPS HOT on wangbo with PID 7180 (H:\workspace\learn\wangbo\target\wangbo-0.0.1-SNAPSHOT.jar started by Administrator in C:\Users\ Administrator) 2017-08-19 17:20:59.138 INFO 7180 --- [ main] com.sumavision.wangbo.App : No active profile set, fa lling back to default profiles: default 2017-08-19 17:20:59.245 INFO 7180 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springfram ework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5ef04b5: startup date [Sat Aug 19 17:20:59 CST 2017 ]; root of context hierarchy 2017-08-19 17:21:01.058 INFO 7180 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with p ort(s): 8080 (http) 2017-08-19 17:21:01.075 INFO 7180 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-08-19 17:21:01.076 INFO 7180 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2017-08-19 17:21:01.199 INFO 7180 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embed ded WebApplicationContext 2017-08-19 17:21:01.199 INFO 7180 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContex t: initialization completed in 1957 ms 2017-08-19 17:21:01.375 INFO 7180 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatc herServlet' to [/] 2017-08-19 17:21:01.380 INFO 7180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characte rEncodingFilter' to: [/*] 2017-08-19 17:21:01.381 INFO 7180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHt tpMethodFilter' to: [/*] 2017-08-19 17:21:01.382 INFO 7180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutF ormContentFilter' to: [/*] 2017-08-19 17:21:01.385 INFO 7180 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestC ontextFilter' to: [/*] 2017-08-19 17:21:01.739 INFO 7180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAd vice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5ef04b5: startup date [Sat Aug 19 17:20:59 CST 2017]; root of context hierarchy 2017-08-19 17:21:01.837 INFO 7180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user/{name}]}" onto public java.lang.String com.sumavision.wangbo.controller.UserController.login(java.lang.String) 2017-08-19 17:21:01.839 INFO 7180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/xxx/{name}]}" onto public java.lang.String com.sumavision.wangbo.controller.UserController.loginout(java.lang.String) 2017-08-19 17:21:01.874 INFO 7180 --- [ 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.au toconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-08-19 17:21:01.876 INFO 7180 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produce s=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErro rController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-08-19 17:21:01.909 INFO 7180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars /**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-19 17:21:01.910 INFO 7180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] ont o handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-19 17:21:01.948 INFO 7180 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favi con.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-08-19 17:21:02.105 INFO 7180 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-08-19 17:21:02.187 INFO 7180 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s) : 8080 (http) 2017-08-19 17:21:02.196 INFO 7180 --- [ main] com.sumavision.wangbo.App : Started App in 3.426 seco nds (JVM running for 3.924)
由日志
Tomcat started on port(s): 8080 (http)
Started App in 3.426 seconds (JVM running for 3.924)
可以知道项目启动成功了。
四、项目访问
到此项目启动成功,可以在控制台Ctrl+C 结束项目运行
五、相关命令
Linux下启动应用:
springboot在linux下后台启动应用,首先cd到jar包所在目录,接着
java -jar sums-manage-0.0.1-SNAPSHOT.jar 属于前台启动,Ctrl+C 会关闭应用
java -jar sums-manage-0.0.1-SNAPSHOT.jar & 属于后台启动,Ctrl+C 不会关闭应用,但是关闭shell程序就会关闭应用
nohup java -jar sums-manage-0.0.1-SNAPSHOT.jar & 属于后台启动,应用不会被其他操作打断
Linux下关闭应用:
ps -ef|grep sums-manage-0.0.1-SNAPSHOT.jar 查看进程号
kill -9 xx 强制终止进程
Linux下查看应用日志:
tail -f nohup.out 查看线上日志,是通过nohup.out文件查看的
如果想查看日志记录文件,找到项目中配置的地方直接查看就行
一般maven用到的命令:
clean compile 清除再编译
clean package 清除再打包