前言:
当初进入Java行业是不是因为他有高的薪资?不知道大家有没有听到过一种声音,“学会计专业不赚钱,还不如去学Java,”“学土木工程,不想去搬砖还是去学Java吧”‘学美术,投资方太挑了,还不如去学习Java’。学习Java的周期一般是在半年左右的,只要有目的性的学习,两个月也是可以完成的,很多的人在的两个月基本就能学习完成,并且找到好工作。
废话不多说了,我们今天来分享一下红包插件实现方法。
手动实现打包之后自动运行的插件:
实现思路:
1、将目标构件打包为可以执行jar包到target目录
maven中将构件打包为可以执行的jar的插件,maven已经帮我们提供了,如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>启动类完整路径</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
上面使用到了maven官方提供的一个打包的插件,可以将构件打包为可以直接运行的jar包。
2、自定义一个插件,然后执行上面打包好的插件
插件中需要通过java命令调用打包好的jar包,然后运行。
具体实现如下
创建自定义目标类
demo1-maven-plugin
中创建一个插件目标类,如下:
package com.javacode2018; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Execute; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 工作10年的前阿里P7分享Java、算法、数据库方面的技术干货!坚信用技术改变命运,让家人过上更体面的生活!喜欢的请关注公众号:路人甲Java */ @Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE) @Execute(phase = LifecyclePhase.PACKAGE) public class RunMojo extends AbstractMojo { /** * 打包好的构件的路径 */ @Parameter(defaultValue = "${project.build.directory}\\${project.artifactId}-${project.version}.jar") private String jarPath; @Override public void execute() throws MojoExecutionException, MojoFailureException { try { this.getLog().info("Started:" + this.jarPath); ProcessBuilder builder = new ProcessBuilder("java", "-jar", this.jarPath); final Process process = builder.start(); new Thread(() -> { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); try { String s; while ((s = reader.readLine()) != null) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } }).start(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { RunMojo.this.getLog().info("Destroying..."); process.destroy(); RunMojo.this.getLog().info("Shutdown hook finished."); } }); process.waitFor(); this.getLog().info("Finished."); } catch (Exception e) { this.getLog().warn(e); } } }
上面这个插件目标的名称为run
注意这个类上面多了一个注解@Execute
,这个注解可以配置这个目标执行之前可以先执行的生命周期的阶段
或者需要先执行的插件目标
。
上面配置的是phase = LifecyclePhase.PACKAGE
,也就是说当我们运行上面run
目标的时候,会先执行构件的package
阶段,也就是先执行项目的打包阶段,打包完成之后才会执行run
目标。
安装插件到本地仓库
maven-chat10/pom.xml
所在目录运行:
mvn clean install -pl :demo1-maven-plugin
创建测试模块`demo1-maven-plugin-run`
使用idea创建,过程和demo1-maven-plugin
过程类似,可以直接参考,创建好了,如下:
创建`com.javacode2018.Demo`类
在demo1-maven-plugin-run\src\main\java创建下面类:
package com.javacode2018; import java.util.Calendar; import java.util.concurrent.TimeUnit; /** * 工作10年的前阿里P7分享Java、算法、数据库方面的技术干货!坚信用技术改变命运,让家人过上更体面的生活!喜欢的请关注公众号:路人甲Java */ public class Demo1 { public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 1000; i++) { System.out.println(Calendar.getInstance().getTime() + ":" + i); TimeUnit.SECONDS.sleep(1); } } }
修改`demo1-maven-plugin-run/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> <groupId>com.javacode2018</groupId> <artifactId>demo1-maven-plugin-run</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 配置maven编译的时候采用的编译器版本 --> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> <!-- 指定源代码是什么版本的,如果源码和这个版本不符将报错,maven中执行编译的时候会用到这个配置,默认是1.5,这个相当于javac命令后面的-source参数 --> <maven.compiler.source>1.8</maven.compiler.source> <!-- 该命令用于指定生成的class文件将保证和哪个版本的虚拟机进行兼容,maven中执行编译的时候会用到这个配置,默认是1.5,这个相当于javac命令后面的-target参数 --> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.javacode2018.Demo1</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
验证效果见证奇迹的时刻
在maven-chat10/pom.xml
所在目录执行下面的命令:
mvn clean demo1:run -pl demo1-maven-plugin-run
输出如下:
D:\code\IdeaProjects\maven-chat10>mvn clean demo1:run -pl demo1-maven-plugin-run [INFO] Scanning for projects... [INFO] [INFO] --------------< com.javacode2018:demo1-maven-plugin-run >--------------- [INFO] Building demo1-maven-plugin-run 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo1-maven-plugin-run --- [INFO] Deleting D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target [INFO] [INFO] >>> demo1-maven-plugin:1.0-SNAPSHOT:run (default-cli) > package @ demo1-maven-plugin-run >>> [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo1-maven-plugin-run --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo1-maven-plugin-run --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo1-maven-plugin-run --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo1-maven-plugin-run --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo1-maven-plugin-run --- [INFO] No tests to run. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo1-maven-plugin-run --- [INFO] Building jar: D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target\demo1-maven-plugin-run-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:3.2.1:shade (default) @ demo1-maven-plugin-run --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target\demo1-maven-plugin-run-1.0-SNAPSHOT.jar with D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target\demo1-maven-plugin-run-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] <<< demo1-maven-plugin:1.0-SNAPSHOT:run (default-cli) < package @ demo1-maven-plugin-run <<< [INFO] [INFO] [INFO] --- demo1-maven-plugin:1.0-SNAPSHOT:run (default-cli) @ demo1-maven-plugin-run --- [INFO] Started:D:\code\IdeaProjects\maven-chat10\demo1-maven-plugin-run\target\demo1-maven-plugin-run-1.0-SNAPSHOT.jar Tue Nov 26 17:24:47 CST 2019:0 Tue Nov 26 17:24:48 CST 2019:1 Tue Nov 26 17:24:49 CST 2019:2 Tue Nov 26 17:24:50 CST 2019:3 Tue Nov 26 17:24:51 CST 2019:4 Tue Nov 26 17:24:52 CST 2019:5 Tue Nov 26 17:24:53 CST 2019:6 Tue Nov 26 17:24:54 CST 2019:7 Tue Nov 26 17:24:55 CST 2019:8 Tue Nov 26 17:24:56 CST 2019:9 Tue Nov 26 17:24:57 CST 2019:10 Tue Nov 26 17:24:58 CST 2019:11 Tue Nov 26 17:24:59 CST 2019:12 Tue Nov 26 17:25:00 CST 2019:13
是不是打包+运行很简单,一行命令就搞定了。
最后:
往事不回头,未来不将就,所有历练终将惊艳时光
作者为大家整理了很多的资料!包括java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书等等!