Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化排包配置 #918

Merged
merged 12 commits into from
May 5, 2024
6 changes: 6 additions & 0 deletions sofa-ark-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn 级别

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);
}
}
Comment on lines +646 to +673
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method extensionExcludeArtifactsFromProp effectively handles properties file for artifact exclusion. Consider adding a debug log before returning when the config file does not exist for better traceability.

652a653
>             getLog().debug("Config file does not exist: " + configPath);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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 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));
getLog().debug("Config file does not exist: " + 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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warn 级别

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);
}
}
Comment on lines +675 to +702
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method extensionExcludeArtifactsFromYaml is well-implemented for parsing YAML configurations. Similar to the previous method, consider adding a debug log for missing config files.

681a682
>             getLog().debug("Config file does not exist: " + configPath);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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);
}
}
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));
getLog().debug("Config file does not exist: " + 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
Loading