diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java index cc916434e5c8c2e91393d123bc53cceac4bed2ce..34b06d33ba6ada2cc2a8570745c77fe5cdfd992b 100644 --- a/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java +++ b/spring-brick-common/src/main/java/com/gitee/starblues/utils/FilesUtils.java @@ -20,7 +20,6 @@ import com.gitee.starblues.common.Constants; import java.io.File; import java.io.IOException; -import java.util.function.Supplier; /** * 文件工具类 diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java index fddd18d7b530ababaec3e2b72a9bc9c3ee478853..4631f5566c84a77a096a8897f266b64dc9652614 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/LoaderConstant.java @@ -28,6 +28,10 @@ public class LoaderConstant { public static final String PROD_CLASSES_URL_SIGN = "/classes!/"; public static final String PROD_LIB_PATH = "lib/"; + /** + * 相对路径符号标志 + */ + public final static String RELATIVE_SIGN = "~"; /** diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java index 9716ee00909d6937bd5098d489b341880d50ed6a..22249d6208c5dbb2e1a410da8c55c41bd97fb3ce 100644 --- a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/launcher/MainJarOuterProgramLauncher.java @@ -22,8 +22,8 @@ import com.gitee.starblues.loader.archive.JarFileArchive; import com.gitee.starblues.loader.classloader.GenericClassLoader; import com.gitee.starblues.loader.classloader.resource.loader.MainJarResourceLoader; import com.gitee.starblues.loader.launcher.runner.MethodRunner; +import com.gitee.starblues.loader.utils.FilesUtils; import com.gitee.starblues.loader.utils.ObjectUtils; -import com.gitee.starblues.loader.utils.ResourceUtils; import java.io.File; import java.io.FileFilter; @@ -90,10 +90,14 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ private void addLibResource(Archive archive, GenericClassLoader classLoader) throws Exception { Manifest manifest = archive.getManifest(); String libDir = manifest.getMainAttributes().getValue(MAIN_LIB_DIR); + String relativePath = rootJarFile.isDirectory() ? rootJarFile.getPath() : rootJarFile.getParent(); + libDir = FilesUtils.resolveRelativePath(relativePath, libDir); File libJarDir = new File(libDir); if(libJarDir.exists()){ List libIndexes = getLibIndexes(manifest); addLibJarFile(libJarDir, libIndexes, classLoader); + } else { + throw new IllegalStateException("主程序依赖目录不存在: " + libDir); } } @@ -110,6 +114,9 @@ public class MainJarOuterProgramLauncher extends MainProgramLauncher{ } indexes.add(index); } + if(indexes.isEmpty()){ + throw new IllegalStateException("主程序依赖包未发现!"); + } return indexes; } diff --git a/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..1b17a8b2434e8d85c9d02e926f613e5db6297da4 --- /dev/null +++ b/spring-brick-loader/src/main/java/com/gitee/starblues/loader/utils/FilesUtils.java @@ -0,0 +1,127 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.starblues.loader.utils; + +import com.gitee.starblues.loader.LoaderConstant; + +import java.io.File; +import java.io.IOException; + +/** + * 文件工具类 + * + * @author starBlues + * @version 3.0.2 + */ +public class FilesUtils { + + /** + * 获取存在的文件 + * + * @param pathStr 文件路径 + * @return File + */ + public static File getExistFile(String pathStr){ + File file = new File(pathStr); + if(file.exists()){ + return file; + } + return null; + } + + + /** + * 拼接file路径 + * + * @param paths 拼接的路径 + * @return 拼接的路径 + */ + public static String joiningFilePath(String ...paths){ + if(paths == null || paths.length == 0){ + return ""; + } + StringBuilder stringBuilder = new StringBuilder(); + int length = paths.length; + for (int i = 0; i < length; i++) { + String path = paths[i]; + if(ObjectUtils.isEmpty(path)) { + continue; + } + if(i > 0){ + if(path.startsWith(File.separator) || path.startsWith("/") || + path.startsWith("\\") || path.startsWith("//")){ + stringBuilder.append(path); + } else { + stringBuilder.append(File.separator).append(path); + } + } else { + stringBuilder.append(path); + } + } + + return stringBuilder.toString(); + } + + public static File createFile(String path) throws IOException { + try { + File file = new File(path); + File parentFile = file.getParentFile(); + if(!parentFile.exists()){ + if(!parentFile.mkdirs()){ + throw new IOException("Create " + parentFile + " dir error"); + } + } + if(file.createNewFile()){ + return file; + } + throw new IOException("Create " + path + " file error"); + } catch (Exception e){ + throw new IOException("Create " + path + " file error"); + } + } + + + /** + * 解决相对路径 + * @param rootPath 根路径 + * @param relativePath 以 ~ 开头的相对路径 + * @return 处理后的路径 + */ + public static String resolveRelativePath(String rootPath, String relativePath){ + if(ObjectUtils.isEmpty(relativePath)){ + return relativePath; + } + if(isRelativePath(relativePath)){ + return joiningFilePath(rootPath, relativePath.replaceFirst(LoaderConstant.RELATIVE_SIGN, "")); + } else { + return relativePath; + } + } + + /** + * 是否是相对路径 + * @param path 路径 + * @return true 为相对路径, false 未非相对路径 + */ + public static boolean isRelativePath(String path){ + if(ObjectUtils.isEmpty(path)){ + return false; + } + return path.startsWith(LoaderConstant.RELATIVE_SIGN); + } + +} diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java index f169726e23a54ab8a0ac626da84f0588b15f66e3..b24c52523d6a5463f6ec4ca63203420ed9d077d3 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/AbstractPackagerMojo.java @@ -54,6 +54,9 @@ public abstract class AbstractPackagerMojo extends AbstractDependencyFilterMojo{ @Parameter(property = "spring-brick-packager.loadMainResourcePattern", required = false) private LoadMainResourcePattern loadMainResourcePattern; + @Parameter(property = "spring-brick-packager.includeSystemScope", defaultValue = "true", required = false) + private Boolean includeSystemScope; + @Override public final void execute() throws MojoExecutionException, MojoFailureException { if(Constant.isPom(this.getProject().getPackaging())){ diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java index c0f1edde5fb4a71a228e5a798737a2e83e1916b3..bb1f79012dfec24951f3ea77c9c2bd8c19fa2ce6 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/BasicRepackager.java @@ -16,19 +16,16 @@ package com.gitee.starblues.plugin.pack; -import com.gitee.starblues.common.AbstractDependencyPlugin; -import com.gitee.starblues.common.ManifestKey; -import com.gitee.starblues.common.PackageStructure; -import com.gitee.starblues.common.PackageType; -import com.gitee.starblues.plugin.pack.dev.DevConfig; -import com.gitee.starblues.plugin.pack.utils.CommonUtils; +import com.gitee.starblues.common.*; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; import lombok.Getter; import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; import java.io.File; import java.io.FileOutputStream; @@ -355,7 +352,8 @@ public class BasicRepackager implements Repackager{ * @return 返回true表示被过滤掉 */ protected boolean filterArtifact(Artifact artifact){ - return Constant.scopeFilter(artifact.getScope()); + return Constant.filterMainTypeArtifact(artifact) || + Constant.filterArtifact(artifact, repackageMojo.getIncludeSystemScope()); } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java index 3358372d3de7bf558eac1c7f0e701f525b0d5593..a08d41bc78b9ac8b07b2c6c74e22b7ec1a76cb5c 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/Constant.java @@ -16,6 +16,8 @@ package com.gitee.starblues.plugin.pack; +import org.apache.maven.artifact.Artifact; + /** * 静态类 * @author starBlues @@ -26,8 +28,11 @@ public class Constant { public static final String PACKAGING_POM = "pom"; public static final String SCOPE_PROVIDED = "provided"; public static final String SCOPE_COMPILE = "compile"; + public static final String SCOPE_SYSTEM = "system"; public static final String SCOPE_TEST = "test"; + public static final String MAVEN_MAIN_TYPE = "main"; + public static final String MODE_MAIN = "main"; public static final String MODE_DEV = "dev"; public static final String MODE_PROD = "prod"; @@ -38,9 +43,29 @@ public class Constant { return PACKAGING_POM.equalsIgnoreCase(packageType); } + public static boolean filterArtifact(Artifact artifact, Boolean includeSystemScope){ + boolean scopeFilter = Constant.scopeFilter(artifact.getScope()); + if(scopeFilter){ + return true; + } + if(Constant.isSystemScope(artifact.getScope())){ + return includeSystemScope == null || !includeSystemScope; + } + return false; + } + + public static boolean filterMainTypeArtifact(Artifact artifact){ + // 配置了为main的依赖, 则对其过滤 + return MAVEN_MAIN_TYPE.equalsIgnoreCase(artifact.getType()); + } + public static boolean scopeFilter(String scope){ return SCOPE_PROVIDED.equalsIgnoreCase(scope) || SCOPE_TEST.equalsIgnoreCase(scope); } + public static boolean isSystemScope(String scope){ + return SCOPE_SYSTEM.equalsIgnoreCase(scope); + } + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java index c063ea304cbae8799d2aef59ec3a8781bb15814f..e866c3d373098fadf8431acb04ee3ee4b47f1139 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/main/JarNestPackager.java @@ -108,7 +108,7 @@ public class JarNestPackager implements Repackager { } protected boolean filterArtifact(Artifact artifact) { - return Constant.scopeFilter(artifact.getScope()); + return Constant.filterArtifact(artifact, repackageMojo.getIncludeSystemScope()); } protected String createLibEntry() throws Exception { diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml index d75716eedeccbac13213d7e0729fb91c5ffaf553..ebe06e796278073f4d6a73fc560a66a0b5b8e87d 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml @@ -141,6 +141,14 @@ true 加密配置 + + includeSystemScope + boolean + 3.0.2 + false + true + 是否包含scope类型为system的依赖 + @@ -155,6 +163,7 @@ + diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml index d75716eedeccbac13213d7e0729fb91c5ffaf553..ebe06e796278073f4d6a73fc560a66a0b5b8e87d 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml @@ -141,6 +141,14 @@ true 加密配置 + + includeSystemScope + boolean + 3.0.2 + false + true + 是否包含scope类型为system的依赖 + @@ -155,6 +163,7 @@ + diff --git a/update.md b/update.md index c61e7ee53f194700677a1936f2002d21b3ace0af..f2ee7f572cab55d8b8c99fdee742d857ab76c4b7 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,7 @@ 1. 新增 `xx-outer、dir` 打包类型的插件可自定义依赖目录 -2. 修复插件拦截器无法拦截不存在的url -3. 修复主程序在 `jar-outer` 打包模式后无法启动问题 -4. 修复插件首次安装时异常 -5. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53K4G -6. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53T9W \ No newline at end of file +2. 新增`includeSystemScope`、`type=main` 打包属性 +3. 修复插件拦截器无法拦截不存在的url +4. 修复主程序在 `jar-outer` 打包模式后无法启动问题 +5. 修复插件首次安装时异常 +6. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53K4G +7. fix https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I53T9W \ No newline at end of file