Skip to content

Commit

Permalink
优化排包配置 (#918)
Browse files Browse the repository at this point in the history
* 优化显示

* fix as bizStateRecords

* 更新显示

* 优化自动排包配置

* 更新排包描述

---------

Co-authored-by: leo james <leojames.googol@gmail.com>
(cherry picked from commit 5a13e01)
  • Loading branch information
gaosaroma authored and lvjing2 committed May 27, 2024
1 parent 3ad5097 commit 8153d3e
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
6 changes: 6 additions & 0 deletions sofa-ark-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@
<version>2.14.3</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.1</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
5 changes: 5 additions & 0 deletions sofa-ark-parent/support/ark-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -604,6 +614,8 @@ protected Set<Artifact> filterExcludeArtifacts(Set<Artifact> artifacts) {
+ DEFAULT_EXCLUDE_RULES);
}

extensionExcludeArtifactsByDefault();

// extension from url
if (StringUtils.isNotBlank(packExcludesUrl)) {
extensionExcludeArtifactsFromUrl(packExcludesUrl, artifacts);
Expand All @@ -625,6 +637,84 @@ protected Set<Artifact> filterExcludeArtifacts(Set<Artifact> 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<String, List<String>> 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<String> 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<String> targetSet, Map<String, List<String>> 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 &lt;excludeGroupIds&gt;
* and &lt;excludeArtifactIds&gt; config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> excludes = (LinkedHashSet<String>) excludesField.get(repackageMojo);

Field excludeGroupIdsField = RepackageMojo.class.getDeclaredField("excludeGroupIds");
excludeGroupIdsField.setAccessible(true);
LinkedHashSet<String> excludeGroupIds = (LinkedHashSet<String>) excludeGroupIdsField
.get(repackageMojo);

Field excludeArtifactIdsField = RepackageMojo.class.getDeclaredField("excludeArtifactIds");
excludeArtifactIdsField.setAccessible(true);
LinkedHashSet<String> excludeArtifactIds = (LinkedHashSet<String>) 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8153d3e

Please sign in to comment.