maven笔记-3-springboot小案例

来源:互联网 发布:ae软件手机版中文 编辑:程序博客网 时间:2024/06/15 10:02

1.创建spring boot项目:
2.maven update后JRE System中的jre还原问题
3.将maven项目打成jar包


1.创建spring boot项目:
这里写图片描述
这里写图片描述

此时看到JRE System Library[这里的版本可能不是你系统当中的]
设置方法:http://blog.csdn.net/u011095110/article/details/56279020

配置pom.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>springboots</groupId>  <artifactId>springboottest</artifactId>  <version>0.0.1-SNAPSHOT</version>   <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.4.1.RELEASE</version>  </parent>  <dependencies>    <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-web</artifactId>    </dependency>  </dependencies></project>

此时查看可以看到
这里写图片描述
写入ExampleApplication类

package springbootpackage;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ExampleApplication {    //SpringApplication是springBoot框架中描述spring应用的类    //它的run()方法会创建一个spring应用上下文(ApplicationContext)    //另一个方面它会扫描当前路径上的依赖    //发现spring-webmvc  (由spring-boot-starter-web传递引入)在类路径中,那么    //springboot会判断这是一个web应用,并启动一个内嵌的servlet容器(默认的是tomcat)    public static void main(String[] args) {        SpringApplication.run(ExampleApplication.class, args);    }}

ExampleController类

package springbootpackage;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/Example")public class ExampleController {    @RequestMapping("/")     public String home(){         return "你好,同学";     }    //带参数的方法处理    @RequestMapping("/{myName}")    public String home(@PathVariable String myName){       return "你好,同学"+myName;       }}

springboot的启动方法(因为springboot自带tomcat,所以不需要引入tomcat)
1.执行ExampleApplication(直接run as–>java Application)
2.利用run as –> Maven build输入命令spring-boot:run执行
这里写图片描述
这里写图片描述
注:Maven build当该项目只设置了一条命令,便默认执行那条命令,如果有多条命令,选择执行。Maven build… 多条命令的设置方式

2.将项目打成jar包
点击maven build…设置命令clean package执行,如果看到下面错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project springboottest: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
设置eclipse中对应的为jdk路径
这里写图片描述

此时打成的jar中没有项目的jar文件,在pom.xml中配置

 <build>     <plugins>       <plugin>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-maven-plugin</artifactId>       </plugin>     </plugins>    </build>

再次打成jar包即可
到项目的jar文件对应的地方按住shift+鼠标右键,选择在此处打开命令窗口
运行java -jar 对应的jar包文件名.jar便可执行

原创粉丝点击