├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── io └── slot ├── BootLauncher.java ├── BootSlotter.java ├── FileSlotter.java ├── IOKit.java ├── SlotTransformer.java └── Slotter.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | 11 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) 12 | !/.mvn/wrapper/maven-wrapper.jar 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slot [![](https://jitpack.io/v/core-lib/slot-maven-plugin.svg)](https://jitpack.io/#core-lib/slot-maven-plugin) 2 | 3 | Spring Boot 可插件化拓展改造器,让 Spring-Boot 应用支持加载外部 jar 包,实现插件化拓展。 4 | 5 | GitHub: https://github.com/core-lib/slot-maven-plugin 6 | 7 | #### Slot: 在计算机行业指的就是周边元件扩展插槽。 8 | 9 | ## 问题描述 10 | Spring-Boot 项目打包后是一个FatJar 即把所有依赖的第三方jar也打包进自身的jar中,运行时 classpath 包括 FatJar 中的 BOOT-INF/classes 目录和 BOOT-INF/lib 目录下的所有jar。 11 | 12 | 那么问题是要想加载外部化 jar 就只能打包期间把 jar 依赖进去,无法实现可插拔式插件化拓展。 13 | 14 | [Slot](https://github.com/core-lib/slot-maven-plugin) 就是一个可以将 Spring-Boot 项目升级为可支持加载外部 jar 的 Maven 插件。 15 | 16 | ## 原理说明 17 | 18 | 一个 Spring-Boot JAR 启动的流程可以分为以下几步: 19 | 1. 通过 java -jar spring-boot-app.jar args... 命令启动 20 | 2. JVM 读取该 jar 的 META-INF/MANIFEST.MF 文件中的 Main-Class,在 Spring-Boot JAR 中这个值通常为 org.springframework.boot.loader.JarLauncher 21 | 3. JVM 调用该类的 main 方法,传入参数即上述命令中参数 22 | 4. JarLauncher 构建 ClassLoader 并反射调用 META-INF/MANIFEST.MF 中的 Start-Class 类的 main 方法,通常为项目中的 Application 类 23 | 5. Application 类的 main 方法调用 SpringApplication.run(Application.class, args); 以最终启动应用 24 | 25 | [Slot](https://github.com/core-lib/slot-maven-plugin) 的核心原理是: 26 | 1. 拓展 org.springframework.boot.loader.JarLauncher 实现根据启动命令参数读取外部 jar 包并且加入至 classpath 中 27 | 2. 修改 META-INF/MANIFEST.MF 中的 Main-Class 为拓展的 JarLauncher 28 | 29 | ## 环境依赖 30 | 1. JDK 1.7 + 31 | 2. Spring-Boot 32 | 33 | ## 使用说明 34 | ```xml 35 | 36 | 37 | 38 | 39 | jitpack.io 40 | https://jitpack.io 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.github.core-lib 48 | slot-maven-plugin 49 | 1.0.2 50 | 51 | 52 | 53 | transform 54 | 55 | package 56 | 57 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | ``` 71 | 72 | ## 参数说明 73 | | 参数名称 | 命令参数名称 | 参数说明 | 参数类型 | 缺省值 | 示例值 | 74 | | :------ | :----------- | :------ | :------ | :----- | :----- | 75 | | sourceDir | -Dslot.sourceDir | 源jar所在目录 | File | ${project.build.directory} | 文件目录 | 76 | | sourceJar | -Dslot.sourceJar | 源jar名称 | String | ${project.build.finalName}.jar | 文件名称 | 77 | | targetDir | -Dslot.targetDir | 目标jar存放目录 | File | ${project.build.directory} | 文件目录 | 78 | | targetJar | -Dslot.targetJar | 目标jar名称 | String | ${project.build.finalName}.slot | 文件名称 | 79 | 80 | 插件的默认执行阶段是 package , 当然也可以通过使用以下命令来单独执行。 81 | ```text 82 | mvn slot:transform 83 | 84 | mvn slot:transform -Dslot.targetJar=your-spring-boot-app-slot.jar 85 | ``` 86 | 87 | 默认情况下,通过 slot 升级后的 jar 名称为 ${project.build.finalName}-slot.jar ,可以通过插件配置或命令参数修改。 88 | 89 | ## 注意事项 90 | ```xml 91 | 92 | org.springframework.boot 93 | spring-boot-maven-plugin 94 | 100 | 101 | ``` 102 | 103 | ## 启动应用 104 | 105 | Slot 支持使用两个参数来指定要加载的外部 jar 包: 106 | 1. --slot.root 即外部 jar 的根路径,缺省情况下为 Spring-Boot JAR 包的目录。 107 | 2. --slot.path 即外部 jar 的路径,支持设置多个,支持 ANT 表达式风格。 108 | 109 | ```text 110 | java -jar spring-boot-app-slot.jar --slot.root=/absolute/root/ --slot.path=foo.jar --slot.path=bar.jar 111 | 112 | java -jar spring-boot-app-slot.jar --slot.path=/relative/path/to/plugin.jar 113 | 114 | java -jar spring-boot-app-slot.jar --slot.path=/relative/path/to/**.jar 115 | ``` 116 | 117 | ANT 表达式通配符说明 118 | 119 | | 通配符 | 含义 | 示例 | 120 | | :----- | :--- | :--- | 121 | | ** | 任意个字符及目录 | /plugins/**.jar 即 /plugins 目录及子目录的所有 .jar 后缀的文件 | 122 | | * | 任意个字符 | /plugins/*.jar 即 /plugins 目录的所有 .jar 后缀的文件 | 123 | | ? | 单个字符 | ???.jar 即当前目录所有名称为三个任意字符及以 .jar 为后缀的文件 | 124 | 125 | 通配符可以随意组合使用! 例如 /plugins/\*\*/plugin-\*-v???.jar 126 | 127 | ## 使用技巧 128 | 由于通过 Slot 加载后的外部 jar 实际上和 Spring-Boot JAR 中的 jar 处于同一个 ClassLoader 所以外部插件和母体应用之间是一个平级的关系, 129 | 外部插件可以引用母体应用中的 class 同样母体应用也可以引用外部插件的 class。 130 | 131 | 由于外部插件项目或模块通常也会依赖另外的第三方jar,所以外部插件与母体应用集成运行时也需要把另外的第三方jar通过--slot.path参数加载进来。 132 | 推荐使用 maven-dependency-plugin 在打包时将需要用到的第三方jar拷贝到指定目录,最后通过ANT表达式方式一起加载运行。 133 | ```xml 134 | 135 | org.apache.maven.plugins 136 | maven-dependency-plugin 137 | 138 | 139 | package 140 | 141 | copy-dependencies 142 | 143 | 144 | runtime 145 | ${project.build.directory}/dependencies 146 | 147 | 148 | 149 | 150 | ``` 151 | 152 | 或者使用 maven-shade-plugin 插件把相关的第三方jar资源通通打包进一个。 153 | ```xml 154 | 155 | org.apache.maven.plugins 156 | maven-shade-plugin 157 | 3.1.1 158 | 159 | 160 | package 161 | 162 | shade 163 | 164 | 165 | ... 166 | 167 | 168 | 169 | 170 | ``` 171 | 另外需要注意的是,当母体应用和外部插件有相同的第三方依赖时,推荐让外部插件模块以 <scope>provided</scope> 的方式依赖之。 172 | 173 | 174 | 下面是作者想到的一些插件化拓展的方案: 175 | 176 | 1. IoC方式:母体应用声明接口,外部插件实现接口并且通过 @Component @Service 或其他注解让Spring 容器管理, 母体应用通过 @Resource @Autowired 来注入。 177 | 178 | 2. SPI方式:母体应用声明接口,外部插件实现接口并且配置于 META-INF/services/ 下,母体应用通过 ServiceLoader 加载接口的实现类。 179 | 180 | 3. AOP方式:外部插件通过 Spring Aspect 技术实现对母体应用的切面拦截。 181 | 182 | 183 | ## 版本记录 184 | * 1.0.2 升级[LoadKit](https://github.com/core-lib/loadkit)依赖版本解决ANT表达式无法正确匹配**/*通配符的问题 185 | 186 | * 1.0.1 bug 修复 187 | 188 | * 1.0.0 第一个正式版发布 189 | 190 | ## 协议声明 191 | [Apache-2.0](http://www.apache.org/licenses/LICENSE-2.0) 192 | 193 | ## 联系作者 194 | QQ 646742615 不会钓鱼的兔子 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.github.core-lib 8 | slot-maven-plugin 9 | 1.0.2 10 | maven-plugin 11 | slot-maven-plugin 12 | https://github.com/slot-maven-plugin 13 | 14 | 15 | UTF-8 16 | 1.7 17 | 1.7 18 | 19 | 20 | 21 | 22 | jitpack.io 23 | https://jitpack.io 24 | 25 | 26 | 27 | 28 | 29 | org.apache.commons 30 | commons-compress 31 | 1.18 32 | 33 | 34 | com.github.core-lib 35 | loadkit 36 | v1.0.1 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-loader 41 | 2.0.1.RELEASE 42 | provided 43 | 44 | 45 | org.apache.maven 46 | maven-plugin-api 47 | 3.5.0 48 | 49 | 50 | org.apache.maven.plugin-tools 51 | maven-plugin-annotations 52 | 3.5 53 | provided 54 | 55 | 56 | org.apache.maven 57 | maven-core 58 | 3.1.1 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.apache.maven.plugins 66 | maven-compiler-plugin 67 | 3.5.1 68 | 69 | 1.7 70 | 1.7 71 | true 72 | true 73 | UTF-8 74 | 75 | 76 | ${project.basedir}/src/main/java 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-source-plugin 84 | 3.0.1 85 | 86 | true 87 | 88 | 89 | 90 | compile 91 | 92 | jar 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /src/main/java/io/slot/BootLauncher.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import io.loadkit.Loaders; 4 | import io.loadkit.Resource; 5 | import org.springframework.boot.loader.JarLauncher; 6 | 7 | import java.io.File; 8 | import java.net.URL; 9 | import java.util.*; 10 | 11 | /** 12 | * Spring Boot JAR 启动器 13 | * 14 | * @author Payne 646742615@qq.com 15 | * 2019/2/16 10:49 16 | */ 17 | public class BootLauncher extends JarLauncher { 18 | private static final String SLOT_ROOT = "--slot.root="; 19 | private static final String SLOT_PATH = "--slot.path="; 20 | 21 | private final String root; 22 | private final List paths; 23 | 24 | public BootLauncher(String root, List paths) { 25 | if (root == null) { 26 | throw new NullPointerException("root must not be null"); 27 | } 28 | if (paths == null) { 29 | paths = Collections.emptyList(); 30 | } 31 | this.root = root; 32 | this.paths = paths; 33 | } 34 | 35 | public static void main(String[] args) throws Exception { 36 | String root = System.getProperty("user.dir"); 37 | List paths = new ArrayList<>(); 38 | List arguments = new ArrayList<>(); 39 | for (String arg : args) { 40 | if (arg.startsWith(SLOT_ROOT)) { 41 | root = arg.substring(SLOT_ROOT.length()); 42 | } else if (arg.startsWith(SLOT_PATH)) { 43 | String path = arg.substring(SLOT_PATH.length()); 44 | paths.add(path); 45 | } else { 46 | arguments.add(arg); 47 | } 48 | } 49 | new BootLauncher(root, paths).launch(arguments.toArray(new String[0])); 50 | } 51 | 52 | @Override 53 | protected ClassLoader createClassLoader(URL[] urls) throws Exception { 54 | Set slots = new LinkedHashSet<>(Arrays.asList(urls)); 55 | for (String path : paths) { 56 | Enumeration resources = Loaders.ant(Loaders.file(new File(root))).load(path); 57 | while (resources.hasMoreElements()) { 58 | Resource resource = resources.nextElement(); 59 | slots.add(resource.getUrl()); 60 | } 61 | } 62 | return super.createClassLoader(slots.toArray(new URL[0])); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/io/slot/BootSlotter.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import org.apache.commons.compress.archivers.jar.JarArchiveEntry; 4 | import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; 5 | import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; 6 | 7 | import java.io.*; 8 | import java.util.jar.Attributes; 9 | import java.util.jar.Manifest; 10 | import java.util.zip.CRC32; 11 | import java.util.zip.CheckedOutputStream; 12 | 13 | /** 14 | * Spring Boot JAR 开槽机 15 | * 16 | * @author Payne 646742615@qq.com 17 | * 2019/2/16 10:46 18 | */ 19 | public class BootSlotter extends FileSlotter implements Slotter { 20 | 21 | @Override 22 | public void slot(InputStream in, OutputStream out) throws IOException { 23 | JarArchiveInputStream zis = null; 24 | JarArchiveOutputStream zos = null; 25 | try { 26 | zis = new JarArchiveInputStream(in); 27 | zos = new JarArchiveOutputStream(out); 28 | 29 | JarArchiveEntry entry; 30 | while ((entry = zis.getNextJarEntry()) != null) { 31 | if (entry.isDirectory()) { 32 | JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); 33 | jarArchiveEntry.setTime(entry.getTime()); 34 | zos.putArchiveEntry(jarArchiveEntry); 35 | } else if (entry.getName().endsWith(".jar")) { 36 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 37 | CheckedOutputStream cos = new CheckedOutputStream(bos, new CRC32()); 38 | IOKit.transfer(zis, cos); 39 | JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); 40 | jarArchiveEntry.setMethod(JarArchiveEntry.STORED); 41 | jarArchiveEntry.setSize(bos.size()); 42 | jarArchiveEntry.setTime(entry.getTime()); 43 | jarArchiveEntry.setCrc(cos.getChecksum().getValue()); 44 | zos.putArchiveEntry(jarArchiveEntry); 45 | ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 46 | IOKit.transfer(bis, zos); 47 | } else if (entry.getName().equals("META-INF/MANIFEST.MF")) { 48 | Manifest manifest = new Manifest(zis); 49 | Attributes attributes = manifest.getMainAttributes(); 50 | attributes.putValue("Main-Class", "io.slot.BootLauncher"); 51 | JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); 52 | jarArchiveEntry.setTime(entry.getTime()); 53 | zos.putArchiveEntry(jarArchiveEntry); 54 | manifest.write(zos); 55 | } else { 56 | JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); 57 | jarArchiveEntry.setTime(entry.getTime()); 58 | zos.putArchiveEntry(jarArchiveEntry); 59 | IOKit.transfer(zis, zos); 60 | } 61 | zos.closeArchiveEntry(); 62 | } 63 | 64 | IOKit.embed("io/slot/**", zos); 65 | IOKit.embed("io/loadkit/**", zos); 66 | 67 | zos.finish(); 68 | } finally { 69 | IOKit.close(zis); 70 | IOKit.close(zos); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/io/slot/FileSlotter.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import java.io.*; 4 | 5 | /** 6 | * 文件开槽机 7 | * 8 | * @author Payne 646742615@qq.com 9 | * 2019/2/16 10:43 10 | */ 11 | public abstract class FileSlotter implements Slotter { 12 | 13 | @Override 14 | public void slot(String src, String dest) throws IOException { 15 | slot(new File(src), new File(dest)); 16 | } 17 | 18 | @Override 19 | public void slot(File src, File dest) throws IOException { 20 | try ( 21 | InputStream in = new FileInputStream(src); 22 | OutputStream out = new FileOutputStream(dest) 23 | ) { 24 | slot(in, out); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/slot/IOKit.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import io.loadkit.Loaders; 4 | import io.loadkit.Resource; 5 | import org.apache.commons.compress.archivers.jar.JarArchiveEntry; 6 | import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; 7 | 8 | import java.io.*; 9 | import java.util.Collections; 10 | import java.util.Enumeration; 11 | import java.util.HashSet; 12 | import java.util.Set; 13 | 14 | /** 15 | * I/O 工具 16 | * 17 | * @author Payne 646742615@qq.com 18 | * 2019/2/15 10:53 19 | */ 20 | public class IOKit { 21 | 22 | /** 23 | * 往JAR包中嵌入框架的classes 24 | * 25 | * @param zos jar包输出流 26 | * @throws IOException I/O 异常 27 | */ 28 | public static void embed(String ant, JarArchiveOutputStream zos) throws IOException { 29 | Set directories = new HashSet<>(); 30 | Enumeration resources = Loaders.ant().load(ant); 31 | while (resources.hasMoreElements()) { 32 | Resource resource = resources.nextElement(); 33 | String name = resource.getName(); 34 | String directory = name.substring(0, name.lastIndexOf('/') + 1); 35 | if (directories.add(directory)) { 36 | JarArchiveEntry xDirEntry = new JarArchiveEntry(directory); 37 | xDirEntry.setTime(System.currentTimeMillis()); 38 | zos.putArchiveEntry(xDirEntry); 39 | zos.closeArchiveEntry(); 40 | } 41 | JarArchiveEntry xJarEntry = new JarArchiveEntry(name); 42 | xJarEntry.setTime(System.currentTimeMillis()); 43 | zos.putArchiveEntry(xJarEntry); 44 | try (InputStream ris = resource.getInputStream()) { 45 | transfer(ris, zos); 46 | } 47 | zos.closeArchiveEntry(); 48 | } 49 | } 50 | 51 | /** 52 | * 从输入流中读取一行字节码 53 | * 54 | * @param in 输入流 55 | * @return 最前面的一行字节码 56 | * @throws IOException I/O 异常 57 | */ 58 | public static byte[] readln(InputStream in) throws IOException { 59 | int b = in.read(); 60 | if (b == -1) { 61 | return null; 62 | } 63 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 64 | while (b != -1) { 65 | switch (b) { 66 | case '\r': 67 | break; 68 | case '\n': 69 | return bos.toByteArray(); 70 | default: 71 | bos.write(b); 72 | break; 73 | } 74 | b = in.read(); 75 | } 76 | return bos.toByteArray(); 77 | } 78 | 79 | /** 80 | * 往输出流中写入一行字节码 81 | * 82 | * @param out 输出流 83 | * @param line 一行字节码 84 | * @throws IOException I/O 异常 85 | */ 86 | public static void writeln(OutputStream out, byte[] line) throws IOException { 87 | if (line == null) { 88 | return; 89 | } 90 | out.write(line); 91 | out.write('\r'); 92 | out.write('\n'); 93 | } 94 | 95 | /** 96 | * 关闭资源,等效于XKit.close(closeable, true); 97 | * 98 | * @param closeable 资源 99 | */ 100 | public static void close(Closeable closeable) { 101 | try { 102 | close(closeable, true); 103 | } catch (IOException e) { 104 | throw new RuntimeException(e); 105 | } 106 | } 107 | 108 | /** 109 | * 关闭资源 110 | * 111 | * @param closeable 资源 112 | * @param quietly 是否安静关闭,即捕获到关闭异常时是否忽略 113 | * @throws IOException 当quietly == false, 时捕获到的I/O异常将会往外抛 114 | */ 115 | public static void close(Closeable closeable, boolean quietly) throws IOException { 116 | if (closeable == null) return; 117 | try { 118 | closeable.close(); 119 | } catch (IOException e) { 120 | if (!quietly) throw e; 121 | } 122 | } 123 | 124 | /** 125 | * 输入流传输到输出流 126 | * 127 | * @param in 输入流 128 | * @param out 输出流 129 | * @return 传输长度 130 | * @throws IOException I/O 异常 131 | */ 132 | public static long transfer(InputStream in, OutputStream out) throws IOException { 133 | long total = 0; 134 | byte[] buffer = new byte[4096]; 135 | int length; 136 | while ((length = in.read(buffer)) != -1) { 137 | out.write(buffer, 0, length); 138 | total += length; 139 | } 140 | out.flush(); 141 | return total; 142 | } 143 | 144 | /** 145 | * reader传输到writer 146 | * 147 | * @param reader reader 148 | * @param writer writer 149 | * @return 传输长度 150 | * @throws IOException I/O 异常 151 | */ 152 | public static long transfer(Reader reader, Writer writer) throws IOException { 153 | long total = 0; 154 | char[] buffer = new char[4096]; 155 | int length; 156 | while ((length = reader.read(buffer)) != -1) { 157 | writer.write(buffer, 0, length); 158 | total += length; 159 | } 160 | writer.flush(); 161 | return total; 162 | } 163 | 164 | /** 165 | * 输入流传输到文件 166 | * 167 | * @param in 输入流 168 | * @param file 文件 169 | * @return 传输长度 170 | * @throws IOException I/O 异常 171 | */ 172 | public static long transfer(InputStream in, File file) throws IOException { 173 | OutputStream out = null; 174 | try { 175 | out = new FileOutputStream(file); 176 | return transfer(in, out); 177 | } finally { 178 | close(out); 179 | } 180 | } 181 | 182 | /** 183 | * reader传输到文件 184 | * 185 | * @param reader reader 186 | * @param file 文件 187 | * @return 传输长度 188 | * @throws IOException I/O 异常 189 | */ 190 | public static long transfer(Reader reader, File file) throws IOException { 191 | OutputStream out = null; 192 | Writer writer = null; 193 | try { 194 | out = new FileOutputStream(file); 195 | writer = new OutputStreamWriter(out); 196 | return transfer(reader, writer); 197 | } finally { 198 | close(writer); 199 | close(out); 200 | } 201 | } 202 | 203 | /** 204 | * 删除文件,如果是目录将不递归删除子文件或目录,等效于delete(file, false); 205 | * 206 | * @param file 文件/目录 207 | * @return 是否删除成功 208 | */ 209 | public static boolean delete(File file) { 210 | return delete(file, false); 211 | } 212 | 213 | /** 214 | * 删除文件,如果是目录将递归删除子文件或目录 215 | * 216 | * @param file 文件/目录 217 | * @return 是否删除成功 218 | */ 219 | public static boolean delete(File file, boolean recursively) { 220 | if (file.isDirectory() && recursively) { 221 | boolean deleted = true; 222 | File[] files = file.listFiles(); 223 | for (int i = 0; files != null && i < files.length; i++) { 224 | deleted &= delete(files[i], true); 225 | } 226 | return deleted && file.delete(); 227 | } else { 228 | return file.delete(); 229 | } 230 | } 231 | 232 | public static boolean isRelative(String path) { 233 | return !isAbsolute(path); 234 | } 235 | 236 | public static boolean isAbsolute(String path) { 237 | if (path.startsWith("/")) { 238 | return true; 239 | } 240 | Set roots = new HashSet<>(); 241 | Collections.addAll(roots, File.listRoots()); 242 | File root = new File(path); 243 | while (root.getParentFile() != null) { 244 | root = root.getParentFile(); 245 | } 246 | return roots.contains(root); 247 | } 248 | 249 | public static String absolutize(String path) { 250 | return normalize(isAbsolute(path) ? path : System.getProperty("user.dir") + File.separator + path); 251 | } 252 | 253 | public static String normalize(String path) { 254 | return path.replaceAll("[/\\\\]+", "/"); 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /src/main/java/io/slot/SlotTransformer.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import org.apache.maven.model.Build; 4 | import org.apache.maven.model.Plugin; 5 | import org.apache.maven.plugin.AbstractMojo; 6 | import org.apache.maven.plugin.MojoExecutionException; 7 | import org.apache.maven.plugin.MojoFailureException; 8 | import org.apache.maven.plugin.logging.Log; 9 | import org.apache.maven.plugins.annotations.LifecyclePhase; 10 | import org.apache.maven.plugins.annotations.Mojo; 11 | import org.apache.maven.plugins.annotations.Parameter; 12 | import org.apache.maven.project.MavenProject; 13 | import org.codehaus.plexus.util.xml.Xpp3Dom; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.util.Map; 18 | 19 | /** 20 | * 插槽建造器 21 | * 22 | * @author Payne 646742615@qq.com 23 | * 2019/2/16 11:25 24 | */ 25 | @Mojo(name = "transform", defaultPhase = LifecyclePhase.PACKAGE) 26 | public class SlotTransformer extends AbstractMojo { 27 | /** 28 | * 当前Maven工程 29 | */ 30 | @Parameter(defaultValue = "${project}", readonly = true, required = true) 31 | private MavenProject project; 32 | 33 | /** 34 | * 原本JAR所在文件夹 35 | */ 36 | @Parameter(property = "slot.sourceDir", required = true, defaultValue = "${project.build.directory}") 37 | private File sourceDir; 38 | 39 | /** 40 | * 原本JAR名称 41 | */ 42 | @Parameter(property = "slot.sourceJar", required = true, defaultValue = "${project.build.finalName}.jar") 43 | private String sourceJar; 44 | 45 | /** 46 | * 生成JAR所在文件夹 47 | */ 48 | @Parameter(property = "slot.targetDir", required = true, defaultValue = "${project.build.directory}") 49 | private File targetDir; 50 | 51 | /** 52 | * 生成JAR名称 53 | */ 54 | @Parameter(property = "slot.targetJar", required = true, defaultValue = "${project.build.finalName}-slot.jar") 55 | private String targetJar; 56 | 57 | @Override 58 | public void execute() throws MojoExecutionException, MojoFailureException { 59 | Log log = getLog(); 60 | String packaging = project.getPackaging(); 61 | if (!"jar".equalsIgnoreCase(packaging)) { 62 | throw new MojoExecutionException("Could not transform project with packaging: " + packaging); 63 | } 64 | 65 | Build build = project.getBuild(); 66 | Map plugins = build.getPluginsAsMap(); 67 | Plugin plugin = plugins.get("org.springframework.boot:spring-boot-maven-plugin"); 68 | // 非Spring-Boot项目/模块 69 | if (plugin == null) { 70 | throw new MojoFailureException("Could not transform non-spring-boot project"); 71 | } 72 | // Spring-Boot项目/模块 73 | else { 74 | Object configuration = plugin.getConfiguration(); 75 | // 不允许开启 true 76 | if (configuration instanceof Xpp3Dom) { 77 | Xpp3Dom dom = (Xpp3Dom) configuration; 78 | { 79 | Xpp3Dom child = dom.getChild("executable"); 80 | String executable = child != null ? child.getValue() : null; 81 | if ("true".equalsIgnoreCase(executable)) { 82 | String msg = "Unsupported to transform true spring boot JAR file, "; 83 | msg += "maybe you should upgrade slot-maven-plugin dependency if it have been supported in the later versions,"; 84 | msg += "if not, delete true or set executable as false for the configuration of spring-boot-maven-plugin."; 85 | throw new MojoFailureException(msg); 86 | } 87 | } 88 | { 89 | Xpp3Dom child = dom.getChild("embeddedLaunchScript"); 90 | String embeddedLaunchScript = child != null ? child.getValue() : null; 91 | if (embeddedLaunchScript != null) { 92 | String msg = "Unsupported to transform ... spring boot JAR file, "; 93 | msg += "maybe you should upgrade slot-maven-plugin dependency if it have been supported in the later versions,"; 94 | msg += "if not, delete ... for the configuration of spring-boot-maven-plugin."; 95 | throw new MojoFailureException(msg); 96 | } 97 | } 98 | } 99 | } 100 | 101 | try { 102 | File src = new File(sourceDir, sourceJar); 103 | File dest = new File(targetDir, targetJar); 104 | File folder = dest.getParentFile(); 105 | if (!folder.exists() && !folder.mkdirs() && !folder.exists()) { 106 | throw new IOException("could not make directory: " + folder); 107 | } 108 | 109 | log.info("Transforming " + src + " to " + dest); 110 | 111 | Slotter slotter = new BootSlotter(); 112 | slotter.slot(src, dest); 113 | } catch (IOException e) { 114 | throw new MojoFailureException(e.getMessage(), e); 115 | } 116 | } 117 | 118 | public MavenProject getProject() { 119 | return project; 120 | } 121 | 122 | public void setProject(MavenProject project) { 123 | this.project = project; 124 | } 125 | 126 | public File getSourceDir() { 127 | return sourceDir; 128 | } 129 | 130 | public void setSourceDir(File sourceDir) { 131 | this.sourceDir = sourceDir; 132 | } 133 | 134 | public String getSourceJar() { 135 | return sourceJar; 136 | } 137 | 138 | public void setSourceJar(String sourceJar) { 139 | this.sourceJar = sourceJar; 140 | } 141 | 142 | public File getTargetDir() { 143 | return targetDir; 144 | } 145 | 146 | public void setTargetDir(File targetDir) { 147 | this.targetDir = targetDir; 148 | } 149 | 150 | public String getTargetJar() { 151 | return targetJar; 152 | } 153 | 154 | public void setTargetJar(String targetJar) { 155 | this.targetJar = targetJar; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/main/java/io/slot/Slotter.java: -------------------------------------------------------------------------------- 1 | package io.slot; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.OutputStream; 7 | 8 | /** 9 | * 开槽机 10 | * 11 | * @author Payne 646742615@qq.com 12 | * 2019/2/16 10:41 13 | */ 14 | public interface Slotter { 15 | 16 | /** 17 | * 开槽,将目标文件开槽输出至目标文件。 18 | * 19 | * @param src 源文件 20 | * @param dest 目标文件 21 | * @throws IOException I/O 异常 22 | */ 23 | void slot(String src, String dest) throws IOException; 24 | 25 | /** 26 | * 开槽,将目标文件开槽输出至目标文件。 27 | * 28 | * @param src 源文件 29 | * @param dest 目标文件 30 | * @throws IOException I/O 异常 31 | */ 32 | void slot(File src, File dest) throws IOException; 33 | 34 | /** 35 | * 开槽,将输入流开槽输出至输出流。 36 | * 37 | * @param in 输入流 38 | * @param out 输出流 39 | * @throws IOException I/O 异常 40 | */ 41 | void slot(InputStream in, OutputStream out) throws IOException; 42 | 43 | } 44 | --------------------------------------------------------------------------------