java工程快速打成可执行jar包的方案
经常需要快速打包一个jar,用来在各种环境进行某种测试,怎么快速打包呢?
下面的方法可以1分钟搞定
完整代码地址:
https://github.com/geektcp/thy-app
第一步:添加好配置文件
application.properties
server.port=10000 server.servlet.context-path=/api
第二部:配置好pom依赖
pom.xml
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <groupId>com.geektcp.app</groupId> <artifactId>thy-app</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.geektcp.common</groupId> <artifactId>thy-core</artifactId> <version>1.0.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> <fork>true</fork> <verbose>true</verbose> <encoding>UTF-8</encoding> <showWarnings>false</showWarnings> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第三步:写两简单的类
package com.geektcp.app; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; /** * site: https://github.com/geektcp * @author geektcp@163.com on 2023/3/14 15:44. */ @Slf4j @EnableScheduling @EnableAsync @ComponentScan(lazyInit = true) @SpringBootApplication @ComponentScan({"com.*"}) public class ThyApp { public static void main(String[] args) { SpringApplication.run(ThyApp.class, args); } }
package com.geektcp.app.controller; import com.geektcp.common.core.generator.IdGenerator; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * site: https://github.com/geektcp * @author geektcp@163.com on 2023/3/15 10:00. */ @Slf4j @RestController @RequestMapping(value = "/test") public class ApiController { @GetMapping(path = "/id") public String getId() { String id = IdGenerator.getId("id_"); log.info("generator id: {}", id); return id; } }
第四步:直接打包
mvn clean package
第五步:运行测试
这是最后一步。
执行启动命令:
java -jar target/thy-app-1.0-SNAPSHOT.jar
测试方法(两种环境任选一种):
linux环境:
curl http://localhost:10000/api/test/id
每次返回的结果是不一样的。
windows环境:
直接在浏览器打开地址:
http://localhost:10000/api/test/id
然后刷新即可
日志如下:
$ java -jar target/thy-app-1.0-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE) 2023-03-15 11:04:42.157 INFO 18528 --- [ main] com.geektcp.app.VarnishApp : Starting VarnishApp v1.0-SNAPSHOT on DESKTOP-IO4R0Q5 with PID 18528
(D:\_github\thy-app\target\thy-app-1.0-SNAPSHOT.jar started by Administrator in D:\_github\t hy-app) 2023-03-15 11:04:42.173 INFO 18528 --- [ main] com.geektcp.app.VarnishApp : No active profile set, falling back to default profiles: default 2023-03-15 11:04:43.850 INFO 18528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 10000 (http) 2023-03-15 11:04:43.882 INFO 18528 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-03-15 11:04:43.883 INFO 18528 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16] 2023-03-15 11:04:43.895 INFO 18528 --- [ 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 (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;F:\Git\mingw64\bin;F:\Git\usr\bin;C:\Users\Administrator\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;
C:\Program Files (x86)\Common Files\NetSarang;C :\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;F:\Java\jdk1.8.0_231\bin;F:\Java\apache-maven-3.6.1\bin;F:\node-v14.21.1;
C:\Program Files (x86)\gnupg\bin;F:\GnuPG;F:\Git\cmd;F:\scala- 2.11.12\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;.] 2023-03-15 11:04:43.999 INFO 18528 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/api] : Initializing Spring embedded WebApplicationContext 2023-03-15 11:04:44.000 INFO 18528 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1774 ms 2023-03-15 11:04:44.259 INFO 18528 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2023-03-15 11:04:44.405 INFO 18528 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler' 2023-03-15 11:04:44.553 INFO 18528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 10000 (http) with context path '/api' 2023-03-15 11:04:44.553 INFO 18528 --- [ main] com.geektcp.app.VarnishApp : Started VarnishApp in 2.715 seconds (JVM running for 3.079) 2023-03-15 11:05:17.648 INFO 18528 --- [ Thread-3] o.s.s.c.ThreadPoolTaskScheduler : Shutting down ExecutorService 'taskScheduler' 2023-03-15 11:05:17.649 INFO 18528 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
-------------------------
技术不在于多么高超先进巧妙,而在于要有现实价值!!!
技术不在于多么高超先进巧妙,而在于要有现实价值!!!