1.java1.8 2.maven3.3+
1.创建空文件夹 2.创建pom文件
<?xml version="1.0" encoding="UTF-8"?> <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.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Additional lines to be added here... --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>3.创建Java代码文件夹 4.编写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) throws Exception { SpringApplication.run(Example.class, args); } }5.启动SpringBoot 使用mvn spring-boot:run命令
C:\Users\Administrator\Desktop\MySpringBoot>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.1.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 C:\Users\Administrator\Desktop\MySpringBoot\src\main\resources [INFO] skip non existing resourceDirectory C:\Users\Administrator\Desktop\MySpringBoot\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ myproject --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\MySpringBoot\target\classes [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 C:\Users\Administrator\Desktop\MySpringBoot\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.1.RELEASE:run (default-cli) < test-compile @ myproject <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:run (default-cli) @ myproject --- . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.1.RELEASE) 2020-07-03 14:31:22.355 INFO 122092 --- [ main] Example : Starting Example on PC-20190103QOK with PID 122092 (C:\Users\Administrator\Desktop\MySpringBoot\target\classes started by Administrator in C:\Users\Administrator\Desktop\MySpringBoot) 2020-07-03 14:31:22.361 INFO 122092 --- [ main] Example : No active profile set, falling back to default profiles: default 2020-07-03 14:31:24.743 INFO 122092 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-07-03 14:31:24.772 INFO 122092 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-07-03 14:31:24.773 INFO 122092 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13 2020-07-03 14:31:24.788 INFO 122092 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Xshell\;C:\Program Files\gradle-4.10\bin;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\Java\jdk1.8.0_45\jre\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\SysWOW64;D:\Node\;D:\protoc\bin;C:\Program Files\gradle-4.10\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;D:\apache-maven-3.6.3\bin;%H2_HOME%\bin;D:\Cocos\Cocos2d-x\Cocos2d-x-3.10\templates;D:\Cocos\Cocos2d-x\Cocos2d-x-3.10\tools\cocos2d-console\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Programs\Microsoft VS Code\bin;D:\Node\node_global;C:\Users\Administrator\AppData\Local\BypassRuntm;C:\Users\Administrator\AppData\Roaming\npm;;.] 2020-07-03 14:31:24.936 INFO 122092 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-07-03 14:31:24.938 INFO 122092 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2533 ms 2020-07-03 14:31:25.246 INFO 122092 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-07-03 14:31:25.511 INFO 122092 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-07-03 14:31:25.518 INFO 122092 --- [ main] Example : Started Example in 4.196 seconds (JVM running for 9.577)6.访问测试 localhost:8080
7.项目打包 mvn package 这里打成本jar包 8.查看打包中的依赖
jar tvf target/myproject-0.0.1-SNAPSHOT.jar9.jar启动
java -jar target/myproject-0.0.1-SNAPSHOT.jar指定其他端口启动
java -jar target/myproject-0.0.1-SNAPSHOT.jar --server.port=80 C:\Users\Administrator\Desktop\MySpringBoot>java -jar target/myproject-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.1.RELEASE) 2020-07-03 14:37:06.986 INFO 116448 --- [ main] Example : Starting Example on PC-20190103QOK with PID 116448 (C:\Users\Administrator\Desktop\MySpringBoot\target\myproject-0.0.1-SNAPSHOT.jar started by Administrator in C:\Users\Administrator\Desktop\MySpringBoot) 2020-07-03 14:37:06.998 INFO 116448 --- [ main] Example : No active profile set, falling back to default profiles: default 2020-07-03 14:37:10.482 INFO 116448 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-07-03 14:37:10.511 INFO 116448 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-07-03 14:37:10.513 INFO 116448 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.13 2020-07-03 14:37:10.528 INFO 116448 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_45\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Xshell\;C:\Program Files\gradle-4.10\bin;C:\Program Files\Java\jdk1.8.0_45\bin;C:\Program Files\Java\jdk1.8.0_45\jre\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\SysWOW64;D:\Node\;D:\protoc\bin;C:\Program Files\gradle-4.10\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;D:\apache-maven-3.6.3\bin;%H2_HOME%\bin;D:\Cocos\Cocos2d-x\Cocos2d-x-3.10\templates;D:\Cocos\Cocos2d-x\Cocos2d-x-3.10\tools\cocos2d-console\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;D:\Programs\Microsoft VS Code\bin;D:\Node\node_global;C:\Users\Administrator\AppData\Local\BypassRuntm;C:\Users\Administrator\AppData\Roaming\npm;;.] 2020-07-03 14:37:10.762 INFO 116448 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-07-03 14:37:10.777 INFO 116448 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3715 ms 2020-07-03 14:37:11.190 INFO 116448 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-07-03 14:37:11.499 INFO 116448 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-07-03 14:37:11.506 INFO 116448 --- [ main] Example : Started Example in 5.101 seconds (JVM running for 5.63)搞定!!!