Skip to content

Commit

Permalink
增加declared白名单
Browse files Browse the repository at this point in the history
  • Loading branch information
gaosaroma committed Aug 22, 2024
1 parent 90233e1 commit f875a6b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> CHANNEL_QUIT = new ArrayList<>();

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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));
Expand All @@ -374,6 +379,15 @@ private void repackage() throws MojoExecutionException, MojoFailureException {
updateArtifact(appTarget, repackager.getModuleTargetFile());
}

protected Set<String> getDeclaredArtifactIds() throws IOException {
Set<String> res = new HashSet<>();
Properties prop = ArkConfigHolder.getArkProperties(baseDir.getAbsolutePath());
Map<String, Object> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}

Expand Down Expand Up @@ -297,6 +292,6 @@ private Log mockLog() {
}

private File mockBaseDir() throws URISyntaxException {
return getResourceFile("baseDir");
return CommonUtils.getResourceFile("baseDir");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -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
excludeArtifactIds=sofa-ark-spi

# declared artifactIds config ${artifactId}, split by ','
declaredArtifactIds=ark-common,biz-common
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ excludes:
excludeGroupIds:
- org.springframework-yml
excludeArtifactIds:
- sofa-ark-spi-yml
- sofa-ark-spi-yml

# declaredArtifactIds 中配置 ${artifactId}, 不同依赖以 - 隔开

declaredArtifactIds:
- ark-common-yml
- biz-common-yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public void setGitDirectory(File gitDirectory) {
this.gitDirectory = gitDirectory;
}

public void prepareDeclaredLibraries(Collection<ArtifactItem> artifactItems) {
public void prepareDeclaredLibraries(Collection<ArtifactItem> artifactItems,
Set<String> declaredArtifactIds) {
if (!this.declaredMode) {
return;
}
Expand All @@ -207,6 +208,8 @@ public void prepareDeclaredLibraries(Collection<ArtifactItem> artifactItems) {
declaredLibraries.add(artifactItem.getArtifactId());
}
}

declaredLibraries.addAll(declaredArtifactIds);
}

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

0 comments on commit f875a6b

Please sign in to comment.