diff --git a/sofa-ark-bom/pom.xml b/sofa-ark-bom/pom.xml index 3d619db7f..55cbfe04d 100644 --- a/sofa-ark-bom/pom.xml +++ b/sofa-ark-bom/pom.xml @@ -375,6 +375,12 @@ 2.14.3 + + org.yaml + snakeyaml + 2.1 + + diff --git a/sofa-ark-parent/support/ark-maven-plugin/pom.xml b/sofa-ark-parent/support/ark-maven-plugin/pom.xml index 6c48df94e..d02951b8b 100644 --- a/sofa-ark-parent/support/ark-maven-plugin/pom.xml +++ b/sofa-ark-parent/support/ark-maven-plugin/pom.xml @@ -102,6 +102,11 @@ com.fasterxml.jackson.core jackson-databind + + + org.yaml + snakeyaml + 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 8422f5890..8154f8725 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 @@ -55,9 +55,11 @@ import org.apache.maven.shared.invoker.InvocationResult; import org.apache.maven.shared.invoker.Invoker; import org.apache.maven.shared.invoker.MavenInvocationException; +import org.yaml.snakeyaml.Yaml; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.nio.charset.Charset; @@ -75,6 +77,7 @@ import static com.alipay.sofa.ark.boot.mojo.MavenUtils.inUnLogScopes; import static com.alipay.sofa.ark.spi.constant.Constants.ARK_CONF_BASE_DIR; +import static com.alipay.sofa.ark.spi.constant.Constants.COMMA_SPLIT; import static com.alipay.sofa.ark.spi.constant.Constants.EXTENSION_EXCLUDES; import static com.alipay.sofa.ark.spi.constant.Constants.EXTENSION_EXCLUDES_ARTIFACTIDS; import static com.alipay.sofa.ark.spi.constant.Constants.EXTENSION_EXCLUDES_GROUPIDS; @@ -93,6 +96,13 @@ public class RepackageMojo extends TreeMojo { private static final String DEFAULT_EXCLUDE_RULES = "rules.txt"; + public final static String ARK_PROPERTIES_FILE = "ark.properties"; + + public final static String ARK_YML_FILE = "ark.yml"; + + public final static String RESOURCES_DIR = "src" + File.separator + "main" + + File.separator + "resources"; + @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject mavenProject; @@ -604,6 +614,8 @@ protected Set filterExcludeArtifacts(Set artifacts) { + DEFAULT_EXCLUDE_RULES); } + extensionExcludeArtifactsByDefault(); + // extension from url if (StringUtils.isNotBlank(packExcludesUrl)) { extensionExcludeArtifactsFromUrl(packExcludesUrl, artifacts); @@ -625,6 +637,84 @@ protected Set filterExcludeArtifacts(Set artifacts) { return result; } + protected void extensionExcludeArtifactsByDefault() { + // extension from default ark.properties and ark.yml + extensionExcludeArtifactsFromProp(); + extensionExcludeArtifactsFromYaml(); + } + + protected void extensionExcludeArtifactsFromProp() { + String configPath = baseDir + File.separator + RESOURCES_DIR + File.separator + + ARK_PROPERTIES_FILE; + File configFile = com.alipay.sofa.ark.common.util.FileUtils.file(configPath); + if (!configFile.exists()) { + getLog().info( + String.format( + "sofa-ark-maven-plugin: extension-config %s not found, will not config it", + configPath)); + return; + } + + getLog().info( + String.format("sofa-ark-maven-plugin: find extension-config %s and will config it", + configPath)); + + Properties prop = new Properties(); + try (FileInputStream fis = new FileInputStream(configPath)) { + prop.load(fis); + + parseExcludeProp(excludes, prop, EXTENSION_EXCLUDES); + parseExcludeProp(excludeGroupIds, prop, EXTENSION_EXCLUDES_GROUPIDS); + parseExcludeProp(excludeArtifactIds, prop, EXTENSION_EXCLUDES_ARTIFACTIDS); + } catch (IOException ex) { + getLog().error( + String.format("failed to parse excludes artifacts from %s.", configPath), ex); + } + } + + protected void extensionExcludeArtifactsFromYaml() { + String configPath = baseDir + File.separator + RESOURCES_DIR + File.separator + + ARK_YML_FILE; + File configFile = com.alipay.sofa.ark.common.util.FileUtils.file(configPath); + if (!configFile.exists()) { + getLog().info( + String.format( + "sofa-ark-maven-plugin: extension-config %s not found, will not config it", + configPath)); + return; + } + + getLog().info( + String.format("sofa-ark-maven-plugin: find extension-config %s and will config it", + configPath)); + + try (FileInputStream fis = new FileInputStream(configPath)) { + Yaml yaml = new Yaml(); + Map> parsedYaml = yaml.load(fis); + parseExcludeYaml(excludes, parsedYaml, EXTENSION_EXCLUDES); + parseExcludeYaml(excludeGroupIds, parsedYaml, EXTENSION_EXCLUDES_GROUPIDS); + parseExcludeYaml(excludeArtifactIds, parsedYaml, EXTENSION_EXCLUDES_ARTIFACTIDS); + + } catch (IOException ex) { + getLog().error( + String.format("failed to parse excludes artifacts from %s.", configPath), ex); + } + } + + private void parseExcludeProp(LinkedHashSet targetSet, Properties prop, String confKey) { + String[] parsed = StringUtils.split(prop.getProperty(confKey), COMMA_SPLIT); + if (null != parsed) { + targetSet.addAll(Arrays.asList(parsed)); + } + } + + private void parseExcludeYaml(LinkedHashSet targetSet, Map> yaml, + String confKey) { + if (yaml.containsKey(confKey) && null != yaml.get(confKey)) { + targetSet.addAll(yaml.get(confKey)); + } + } + /** * This method is core method for excluding artifacts in sofa-ark-maven-plugin <excludeGroupIds> * and <excludeArtifactIds> config. 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 677be0c07..401081ba6 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 @@ -48,15 +48,24 @@ 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.*; +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.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -231,6 +240,52 @@ public void testExtensionExcludeArtifactsFromUrl() throws NoSuchMethodException, extensionExcludeArtifactsFromUrl.invoke(repackageMojo, packExcludesUrl, artifacts); } + @Test + public void testExtensionExcludeArtifactsByDefault() throws NoSuchMethodException, + NoSuchFieldException, URISyntaxException, + IllegalAccessException, + InvocationTargetException { + RepackageMojo repackageMojo = new RepackageMojo(); + Method extensionExcludeArtifactsByDefault = repackageMojo.getClass().getDeclaredMethod( + "extensionExcludeArtifactsByDefault"); + extensionExcludeArtifactsByDefault.setAccessible(true); + + Field baseDirField = RepackageMojo.class.getDeclaredField("baseDir"); + baseDirField.setAccessible(true); + baseDirField.set(repackageMojo, getResourceFile("baseDir")); + + Field excludesField = RepackageMojo.class.getDeclaredField("excludes"); + excludesField.setAccessible(true); + LinkedHashSet excludes = (LinkedHashSet) excludesField.get(repackageMojo); + + Field excludeGroupIdsField = RepackageMojo.class.getDeclaredField("excludeGroupIds"); + excludeGroupIdsField.setAccessible(true); + LinkedHashSet excludeGroupIds = (LinkedHashSet) excludeGroupIdsField + .get(repackageMojo); + + Field excludeArtifactIdsField = RepackageMojo.class.getDeclaredField("excludeArtifactIds"); + excludeArtifactIdsField.setAccessible(true); + LinkedHashSet excludeArtifactIds = (LinkedHashSet) excludeArtifactIdsField + .get(repackageMojo); + + extensionExcludeArtifactsByDefault.invoke(repackageMojo); + + // 验证 ark.properties + assertTrue(excludes.contains("commons-beanutils:commons-beanutils")); + assertTrue(excludeGroupIds.contains("org.springframework")); + assertTrue(excludeArtifactIds.contains("sofa-ark-spi")); + + // 验证 ark.yml + assertTrue(excludes.contains("commons-beanutils:commons-beanutils-yml")); + assertTrue(excludeGroupIds.contains("org.springframework-yml")); + assertTrue(excludeArtifactIds.contains("sofa-ark-spi-yml")); + } + + private File getResourceFile(String resourceName) throws URISyntaxException { + URL url = this.getClass().getClassLoader().getResource(resourceName); + return new File(url.toURI()); + } + @Test public void testLogExcludeMessage() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.properties b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.properties new file mode 100644 index 000000000..ed4d14156 --- /dev/null +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.properties @@ -0,0 +1,6 @@ +# excludes config ${groupId}:{artifactId}:{version}, 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 diff --git a/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.yml b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.yml new file mode 100644 index 000000000..204e78d78 --- /dev/null +++ b/sofa-ark-parent/support/ark-maven-plugin/src/test/resources/baseDir/src/main/resources/ark.yml @@ -0,0 +1,10 @@ +# excludes 中配置 ${groupId}:{artifactId}:{version}, 不同依赖以 - 隔开 +# excludeGroupIds 中配置 ${groupId}, 不同依赖以 - 隔开 +# excludeArtifactIds 中配置 ${artifactId}, 不同依赖以 - 隔开 +excludes: + - org.apache.commons:commons-lang3-yml + - commons-beanutils:commons-beanutils-yml +excludeGroupIds: + - org.springframework-yml +excludeArtifactIds: + - sofa-ark-spi-yml \ No newline at end of file