diff --git a/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/constant/Constants.java b/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/constant/Constants.java index 4fb9c7f9f..caa005bd1 100644 --- a/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/constant/Constants.java +++ b/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/constant/Constants.java @@ -254,6 +254,8 @@ public class Constants { public final static String EXTENSION_INCLUDES_GROUPIDS = "includeGroupIds"; public final static String EXTENSION_INCLUDES_ARTIFACTIDS = "includeArtifactIds"; + public final static String DECLARED_ARTIFACT_IDS = "declaredArtifactIds"; + public static final List CHANNEL_QUIT = new ArrayList<>(); static { diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/main/java/com/alipay/sofa/ark/boot/mojo/RepackageMojo.java b/sofa-ark-parent/support/ark-maven-plugin/src/main/java/com/alipay/sofa/ark/boot/mojo/RepackageMojo.java index 2c63369ab..beeeacb8f 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/src/main/java/com/alipay/sofa/ark/boot/mojo/RepackageMojo.java +++ b/sofa-ark-parent/support/ark-maven-plugin/src/main/java/com/alipay/sofa/ark/boot/mojo/RepackageMojo.java @@ -16,6 +16,7 @@ */ package com.alipay.sofa.ark.boot.mojo; +import com.alipay.sofa.ark.boot.mojo.model.ArkConfigHolder; import com.alipay.sofa.ark.tools.ArtifactItem; import com.alipay.sofa.ark.tools.Libraries; import com.alipay.sofa.ark.tools.Repackager; @@ -56,11 +57,15 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.alipay.sofa.ark.boot.mojo.utils.ParseUtils.getStringSet; +import static com.alipay.sofa.ark.spi.constant.Constants.DECLARED_ARTIFACT_IDS; + /** * Repackages existing JAR archives so that they can be executed from the command * line using {@literal java -jar}. @@ -363,7 +368,7 @@ private void repackage() throws MojoExecutionException, MojoFailureException { } else { artifactItems = getAllArtifactByMavenTree(); } - repackager.prepareDeclaredLibraries(artifactItems); + repackager.prepareDeclaredLibraries(artifactItems, getDeclaredArtifactIds()); } MavenProject rootProject = MavenUtils.getRootProject(this.mavenProject); repackager.setGitDirectory(getGitDirectory(rootProject)); @@ -374,6 +379,15 @@ private void repackage() throws MojoExecutionException, MojoFailureException { updateArtifact(appTarget, repackager.getModuleTargetFile()); } + protected Set getDeclaredArtifactIds() throws IOException { + Set res = new HashSet<>(); + Properties prop = ArkConfigHolder.getArkProperties(baseDir.getAbsolutePath()); + Map arkYaml = ArkConfigHolder.getArkYaml(baseDir.getAbsolutePath()); + res.addAll(getStringSet(prop, DECLARED_ARTIFACT_IDS)); + res.addAll(getStringSet(arkYaml, DECLARED_ARTIFACT_IDS)); + return res; + } + private File getGitDirectory(MavenProject rootProject) { if (disableGitInfo) { return null; diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/CommonUtils.java b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/CommonUtils.java new file mode 100644 index 000000000..4ffc0a22f --- /dev/null +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/CommonUtils.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.alipay.sofa.ark.boot.mojo; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * @author lianglipeng.llp@alibaba-inc.com + * @version $Id: CommonUtils.java, v 0.1 2024年08月22日 21:56 立蓬 Exp $ + */ +public class CommonUtils { + + public static File getResourceFile(String resourceName) throws URISyntaxException { + URL url = CommonUtils.class.getClassLoader().getResource(resourceName); + return new File(url.toURI()); + } +} \ No newline at end of file diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/ModuleSlimStrategyTest.java b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/ModuleSlimStrategyTest.java index 3d775e314..824be644b 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/ModuleSlimStrategyTest.java +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/ModuleSlimStrategyTest.java @@ -229,11 +229,6 @@ public void testLogExcludeMessageWithMoreCases() throws URISyntaxException { strategy.logExcludeMessage(jarGroupIds, jarArtifactIds, jarList, artifacts, false); } - private File getResourceFile(String resourceName) throws URISyntaxException { - URL url = this.getClass().getClassLoader().getResource(resourceName); - return new File(url.toURI()); - } - private MavenProject getMockBootstrapProject() throws URISyntaxException { MavenProject project = new MavenProject(); project.setArtifactId("base-bootstrap"); @@ -250,7 +245,7 @@ private MavenProject getMockBootstrapProject() throws URISyntaxException { project.setParent(getRootProject()); - setField("basedir", project, getResourceFile("baseDir")); + setField("basedir", project, CommonUtils.getResourceFile("baseDir")); return project; } @@ -297,6 +292,6 @@ private Log mockLog() { } private File mockBaseDir() throws URISyntaxException { - return getResourceFile("baseDir"); + return CommonUtils.getResourceFile("baseDir"); } } \ No newline at end of file diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/RepackageMojoTest.java b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/RepackageMojoTest.java index 1bc4946b3..03cb27a9d 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/RepackageMojoTest.java +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/java/com/alipay/sofa/ark/boot/mojo/RepackageMojoTest.java @@ -45,23 +45,21 @@ import org.junit.Test; import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URISyntaxException; -import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.List; import java.util.Map; import java.util.Set; import static com.alipay.sofa.ark.boot.mojo.RepackageMojo.ArkConstants.getClassifier; import static java.lang.System.clearProperty; import static java.lang.System.setProperty; -import static java.util.Arrays.asList; import static org.apache.commons.beanutils.BeanUtils.copyProperties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -413,4 +411,17 @@ public void testInnerModelClass() throws InvocationTargetException, IllegalAcces copyProperties(new ExcludeConfigResponse(), new ExcludeConfigResponse()); assertEquals("", getClassifier()); } + + @Test + public void testGetDeclaredLibrariesWhiteList() throws URISyntaxException, IOException { + RepackageMojo repackageMojo = new RepackageMojo(); + ReflectionUtils.setField("declaredMode", repackageMojo, true); + ReflectionUtils.setField("baseDir", repackageMojo, CommonUtils.getResourceFile("baseDir")); + + Set whitelist = repackageMojo.getDeclaredArtifactIds(); + assertTrue(whitelist.contains("ark-common-yml")); + assertTrue(whitelist.contains("biz-common-yml")); + assertTrue(whitelist.contains("biz-common")); + assertTrue(whitelist.contains("ark-common")); + } } diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.properties b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.properties index b3c512724..602a333b5 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.properties +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.properties @@ -1,6 +1,9 @@ -# excludes config groupId:artifactId} {groupId:artifactId:version} or {groupId:artifactId:version:classifier}, split by ',' +# excludes config {groupId:artifactId} {groupId:artifactId:version} or {groupId:artifactId:version:classifier}, split by ',' excludes=org.apache.commons:commons-lang3,commons-beanutils:commons-beanutils # excludeGroupIds config ${groupId}, split by ',' excludeGroupIds=org.springframework # excludeArtifactIds config ${artifactId}, split by ',' -excludeArtifactIds=sofa-ark-spi \ No newline at end of file +excludeArtifactIds=sofa-ark-spi + +# declared artifactIds config ${artifactId}, split by ',' +declaredArtifactIds=ark-common,biz-common \ No newline at end of file diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.yml b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.yml index 9f774adae..abc1572f4 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.yml +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/conf/ark/bootstrap.yml @@ -7,4 +7,10 @@ excludes: excludeGroupIds: - org.springframework-yml excludeArtifactIds: - - sofa-ark-spi-yml \ No newline at end of file + - sofa-ark-spi-yml + +# declaredArtifactIds 中配置 ${artifactId}, 不同依赖以 - 隔开 + +declaredArtifactIds: + - ark-common-yml + - biz-common-yml \ No newline at end of file diff --git a/sofa-ark-parent/support/ark-tools/src/main/java/com/alipay/sofa/ark/tools/Repackager.java b/sofa-ark-parent/support/ark-tools/src/main/java/com/alipay/sofa/ark/tools/Repackager.java index f9a02d012..996350e62 100644 --- a/sofa-ark-parent/support/ark-tools/src/main/java/com/alipay/sofa/ark/tools/Repackager.java +++ b/sofa-ark-parent/support/ark-tools/src/main/java/com/alipay/sofa/ark/tools/Repackager.java @@ -195,7 +195,8 @@ public void setGitDirectory(File gitDirectory) { this.gitDirectory = gitDirectory; } - public void prepareDeclaredLibraries(Collection artifactItems) { + public void prepareDeclaredLibraries(Collection artifactItems, + Set declaredArtifactIds) { if (!this.declaredMode) { return; } @@ -207,6 +208,8 @@ public void prepareDeclaredLibraries(Collection artifactItems) { declaredLibraries.add(artifactItem.getArtifactId()); } } + + declaredLibraries.addAll(declaredArtifactIds); } /** diff --git a/sofa-ark-parent/support/ark-tools/src/test/java/com/alipay/sofa/ark/tools/RepackagerTest.java b/sofa-ark-parent/support/ark-tools/src/test/java/com/alipay/sofa/ark/tools/RepackagerTest.java index 7315d8895..cdf553394 100644 --- a/sofa-ark-parent/support/ark-tools/src/test/java/com/alipay/sofa/ark/tools/RepackagerTest.java +++ b/sofa-ark-parent/support/ark-tools/src/test/java/com/alipay/sofa/ark/tools/RepackagerTest.java @@ -24,6 +24,7 @@ import java.io.File; import java.lang.reflect.Field; import java.net.URL; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.jar.JarEntry; @@ -124,17 +125,17 @@ public void testSetInjectPluginDependencies() throws Exception { @Test public void testPrepareDeclaredLibraries() throws Exception { - repackager.prepareDeclaredLibraries(null); + repackager.prepareDeclaredLibraries(null, Collections.emptySet()); repackager.setDeclaredMode(true); - repackager.prepareDeclaredLibraries(null); + repackager.prepareDeclaredLibraries(null, Collections.emptySet()); ArtifactItem artifactItem = new ArtifactItem(); artifactItem.setArtifactId("x"); - repackager.prepareDeclaredLibraries(newArrayList(artifactItem)); + repackager.prepareDeclaredLibraries(newArrayList(artifactItem), newHashSet("y")); Field field = Repackager.class.getDeclaredField("declaredLibraries"); field.setAccessible(true); - assertEquals(newHashSet("x"), field.get(repackager)); + assertEquals(newHashSet("x", "y"), field.get(repackager)); } @Test