Skip to content

Commit

Permalink
DeclaredMode 优化,多bundle使用mvn tree扫描依赖,资源扫描到模块和基座时使用模块的 (#614)
Browse files Browse the repository at this point in the history
* only skip test scope jar

* set arklet declared as default

* add maven tree for parsing deps of multi module project

* reuse maven session for multi module lib parse

* support unique jars

---------

Co-authored-by: youji.zzl <[email protected]>
  • Loading branch information
lvjing2 and youji.zzl authored Mar 7, 2023
1 parent e736efd commit 993ff19
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class JarUtils {

private static final Map<String, Optional<String>> artifactIdCacheMap = new ConcurrentHashMap<>();

public static String getArtifactIdFromLocalClassPath(String fileClassPath) throws IOException {
public static String getArtifactIdFromLocalClassPath(String fileClassPath) {
// file:/Users/youji.zzl/Documents/workspace/iexpprodbase/app/bootstrap/target/classes/spring/
String libraryFile = fileClassPath.replace("file:", "");
// 1. search pom.properties
Expand All @@ -73,6 +73,8 @@ public static String getArtifactIdFromLocalClassPath(String fileClassPath) throw
Properties properties = new Properties();
properties.load(inputStream);
return properties.getProperty(JAR_ARTIFACT_ID);
} catch (IOException e) {
return null;
}
}

Expand Down Expand Up @@ -121,7 +123,8 @@ private static String doGetArtifactIdFromJarPom(String jarLocation) {
}
}
} catch (IOException e) {
throw new RuntimeException("Failed to parse artifact id from jar.", e);
throw new RuntimeException(String.format("Failed to parse artifact id from jar %s.",
jarLocation), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,7 @@ private boolean doCheckDeclared(String jarFilePath) {
return true;
}
} else {
try {
artifactId = getArtifactIdFromLocalClassPath(jarFilePath);
} catch (IOException e) {
LOGGER.error(String.format("Failed to get artifact from %s: %s", jarFilePath,
e.getMessage()));
return false;
}
artifactId = getArtifactIdFromLocalClassPath(jarFilePath);
// for not in jar, then default not delegate.
if (artifactId == null) {
LOGGER.info(String.format(
Expand All @@ -497,7 +491,8 @@ private boolean doCheckDeclared(String jarFilePath) {

// some ark related lib which each ark module needed should set declared as default
if (StringUtils.startWithToLowerCase(artifactId, "sofa-ark-")
|| artifactId.contains("arklet-alipay") || artifactId.contains("-arklet-")) {
|| artifactId.equals("arklet-alipay-sofa-boot-starter")
|| artifactId.equals("sofa-boot-alipay-arklet")) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alipay.sofa.ark.container.service.ArkServiceContainerHolder;
import com.alipay.sofa.ark.exception.ArkLoaderException;
import com.alipay.sofa.ark.loader.jar.Handler;
import com.alipay.sofa.ark.loader.jar.JarUtils;
import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.spi.service.classloader.ClassLoaderService;
import com.google.common.cache.Cache;
Expand Down Expand Up @@ -292,7 +293,7 @@ public Enumeration<URL> getResources(String name) throws IOException {
enumerationList.add(postFindResources(name));

// unique urls
return uniqueUrls(enumerationList, name);
return uniqueUrls(enumerationList);
} else {
Enumeration<URL> ret = preFindResources(name);
if (ret != null && ret.hasMoreElements()) {
Expand All @@ -312,33 +313,35 @@ public Enumeration<URL> getResources(String name) throws IOException {
}
}

private Enumeration<URL> uniqueUrls(List<Enumeration<URL>> enumerationList, String targetName) {
private Enumeration<URL> uniqueUrls(List<Enumeration<URL>> enumerationList) {
// unique urls
Set<String> temp = new HashSet<>();
List<URL> uniqueUrls = new ArrayList<>();

for (Enumeration<URL> e : enumerationList) {
if (e == null) {
continue;
}
while (e.hasMoreElements()) {
URL resourceUrl = e.nextElement();
String filePath = resourceUrl.getFile();
int startIndex = filePath.lastIndexOf(targetName);
if (startIndex == -1) {
continue;
}
String filePath = resourceUrl.getFile().replace("file:", "");

String artifactId;
if (filePath.contains(".jar")) {
int index = filePath.lastIndexOf("!/");
String jarFilePath = filePath;
if (index != -1) {
jarFilePath = filePath.substring(0, index);
}

filePath = filePath.substring(0, startIndex);
if (filePath.endsWith("!/")) {
filePath = filePath.substring(0, filePath.length() - 2);
artifactId = JarUtils.getJarArtifactId(jarFilePath);
} else {
artifactId = JarUtils.getArtifactIdFromLocalClassPath(filePath);
}
int artifactStartIndex = filePath.lastIndexOf("/");
String jarFileName = filePath.substring(artifactStartIndex);

if (!temp.contains(jarFileName)) {
if (!temp.contains(artifactId)) {
uniqueUrls.add(resourceUrl);
}
temp.add(jarFileName);
temp.add(artifactId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@
*/
package com.alipay.sofa.ark.boot.mojo;

import com.alipay.sofa.ark.common.util.StringUtils;
import com.alipay.sofa.ark.tools.ArtifactItem;
import org.apache.maven.project.MavenProject;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public class MavenUtils {
public static boolean isRootProject(MavenProject project) {
if (project == null) {
return true;
}

if (project.hasParent() && project.getParent().getBasedir() != null) {
return false;
}
return true;
}

public static MavenProject getRootProject(MavenProject project) {
if (project == null) {
return null;
Expand All @@ -29,4 +47,60 @@ public static MavenProject getRootProject(MavenProject project) {
}
return parent;
}

/**
* @param depTreeContent
* @return
*/
public static Set<ArtifactItem> convert(String depTreeContent) {
Set<ArtifactItem> artifactItems = new HashSet<>();
String[] contents = depTreeContent.split("\n");

for (String content : contents) {
ArtifactItem artifactItem = getArtifactItem(content);
if (artifactItem != null && !"test".equals(artifactItem.getScope())) {
artifactItems.add(artifactItem);
}
}

return artifactItems;
}

private static ArtifactItem getArtifactItem(String lineContent) {
if (StringUtils.isEmpty(lineContent)) {
return null;
}
String[] contentInfos = lineContent.split(" ");
if (contentInfos.length == 0) {
return null;
}
Optional<String> artifactStrOp = Arrays.stream(contentInfos).filter(c -> c.contains(":")).findFirst();
if (!artifactStrOp.isPresent()) {
return null;
}
String[] artifactInfos = artifactStrOp.get().split(":");

ArtifactItem artifactItem = new ArtifactItem();
if (artifactInfos.length == 5) {
// like "com.alipay.sofa:healthcheck-sofa-boot-starter:jar:3.11.1:provided"

artifactItem.setGroupId(artifactInfos[0]);
artifactItem.setArtifactId(artifactInfos[1]);
artifactItem.setType(artifactInfos[2]);
artifactItem.setVersion(artifactInfos[3]);
artifactItem.setScope(artifactInfos[4]);
} else if (artifactInfos.length == 6) {
// like "io.sofastack:dynamic-stock-mng:jar:ark-biz:1.0.0:compile"

artifactItem.setGroupId(artifactInfos[0]);
artifactItem.setArtifactId(artifactInfos[1]);
artifactItem.setType(artifactInfos[2]);
artifactItem.setClassifier(artifactInfos[3]);
artifactItem.setVersion(artifactInfos[4]);
artifactItem.setScope(artifactInfos[5]);
} else {
return null;
}
return artifactItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alipay.sofa.ark.tools.Libraries;
import com.alipay.sofa.ark.tools.Repackager;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
Expand All @@ -42,17 +43,27 @@
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.alipay.sofa.ark.spi.constant.Constants.ARK_CONF_BASE_DIR;
import static com.alipay.sofa.ark.spi.constant.Constants.EXTENSION_EXCLUDES;
Expand Down Expand Up @@ -313,7 +324,12 @@ private void repackage() throws MojoExecutionException, MojoFailureException {
getLog());
try {
if (repackager.isDeclaredMode()) {
Set<ArtifactItem> artifactItems = getAllArtifact();
Set<ArtifactItem> artifactItems;
if (MavenUtils.isRootProject(this.mavenProject)) {
artifactItems = getAllArtifact();
} else {
artifactItems = getAllArtifactByMavenTree();
}
repackager.prepareDeclaredLibraries(artifactItems);
}
MavenProject rootProject = MavenUtils.getRootProject(this.mavenProject);
Expand All @@ -334,11 +350,10 @@ private File getGitDirectory(MavenProject rootProject) {

private void parseArtifactItems(DependencyNode rootNode, Set<ArtifactItem> result) {
if (rootNode != null) {
if (StringUtils.equalsIgnoreCase(rootNode.getArtifact().getScope(), "test")) {
return;
if (!StringUtils.equalsIgnoreCase(rootNode.getArtifact().getScope(), "test")) {
result.add(ArtifactItem.parseArtifactItem(rootNode.getArtifact()));
}

result.add(ArtifactItem.parseArtifactItem(rootNode.getArtifact()));
if (CollectionUtils.isNotEmpty(rootNode.getChildren())) {
for (DependencyNode node : rootNode.getChildren()) {
parseArtifactItems(node, result);
Expand All @@ -355,6 +370,51 @@ private Set<ArtifactItem> getAllArtifact() throws MojoExecutionException, MojoFa
return results;
}

private Set<ArtifactItem> getAllArtifactByMavenTree() throws MojoExecutionException {
File baseDir = MavenUtils.getRootProject(this.mavenProject).getBasedir();
getLog().info("root project path: " + baseDir.getAbsolutePath());

// dependency:tree
String outputPath = baseDir.getAbsolutePath() + "/deps.log." + System.currentTimeMillis();
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File(baseDir.getAbsolutePath() + "/pom.xml"));

List<String> goals = Stream.of("dependency:tree", "-DappendOutput=true",
"-DoutputFile=" + outputPath).collect(Collectors.toList());

Properties userProperties = projectBuildingRequest.getUserProperties();
if (userProperties != null) {
userProperties.forEach((key, value) -> goals.add(String.format("-D%s=%s", key, value)));
}

getLog().info(
"execute 'mvn dependency:tree' with command 'mvn " + String.join(" ", goals) + "'");
request.setGoals(goals);
request.setBatchMode(mavenSession.getSettings().getInteractiveMode());
request.setProfiles(mavenSession.getSettings().getActiveProfiles());
request.setUserSettingsFile(mavenSession.getRequest().getUserSettingsFile());
request.setGlobalSettingsFile(mavenSession.getRequest().getGlobalSettingsFile());
Invoker invoker = new DefaultInvoker();
try {
InvocationResult result = invoker.execute(request);
if (result.getExitCode() != 0) {
throw new MojoExecutionException("execute dependency:tree failed",
result.getExecutionException());
}

String depTreeStr = FileUtils.readFileToString(FileUtils.getFile(outputPath),
Charset.defaultCharset());
return MavenUtils.convert(depTreeStr);
} catch (MavenInvocationException | IOException e) {
throw new MojoExecutionException("execute dependency:tree failed", e);
} finally {
File outputFile = new File(outputPath);
if (outputFile.exists()) {
outputFile.delete();
}
}
}

@SuppressWarnings("unchecked")
private Set<Artifact> getAdditionalArtifact() throws MojoExecutionException {
Artifact arkArtifact = repositorySystem.createArtifact(ArkConstants.getGroupId(),
Expand Down

0 comments on commit 993ff19

Please sign in to comment.