Skip to content

Commit

Permalink
fix base package dependency (#158) (#159)
Browse files Browse the repository at this point in the history
* fix basePackageFacade

* jdk8 support kotlin

* add jdkVersion

* rm test resources

* format

* fix unit test

* rm unnecessary config

* rm jvmTarget

* fix SNAPSHOT version

* fix dependencyManagement

* fix package dependency to add exclusions

---------

Co-authored-by: leo james <leojames.googol@gmail.com>
(cherry picked from commit 9b87342)

Co-authored-by: Lipeng <44571204+gaosaroma@users.noreply.github.com>
  • Loading branch information
lvjing2 and gaosaroma authored Aug 13, 2024
1 parent f8c832a commit 91c69a1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.License;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -38,13 +37,17 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import static com.alipay.sofa.koupleless.base.build.plugin.common.FileUtils.createNewDirectory;
Expand Down Expand Up @@ -174,6 +177,7 @@ private void integrateBaseDependencies() throws MojoExecutionException, IOExcept
// 配置 dependencyManagement
Set<ArtifactItem> baseModuleArtifacts = getAllBundleArtifact(this.mavenProject);
getLog().info("find maven module of base: " + baseModuleArtifacts);
Map<String, Dependency> resolvedProjectDependencyManagement = getResolvedProjectDependencyManagement();
Set<Artifact> dependencyArtifacts = getDependencyArtifacts(this.mavenProject);
DependencyManagement dependencyManagement = new DependencyManagement();
List<Dependency> dependencies = nonNull(dependencyArtifacts).stream()
Expand All @@ -182,8 +186,17 @@ private void integrateBaseDependencies() throws MojoExecutionException, IOExcept
baseModule -> Objects.equals(baseModule.getGroupId(), d.getGroupId())
&& Objects.equals(baseModule.getArtifactId(), d.getArtifactId())))
// 过滤出 scope 不是 test 的依赖
.filter(d -> !"test".equals(d.getScope())).map(MavenUtils::createDependency)
.collect(Collectors.toList());
.filter(d -> !"test".equals(d.getScope()))
// 转换为 dependency
.map(d -> {
// 如果项目的依赖管理中有该依赖,则复用项目的依赖管理
if (resolvedProjectDependencyManagement
.containsKey(MavenUtils.getArtifactIdentity(d))) {
return resolvedProjectDependencyManagement
.get(MavenUtils.getArtifactIdentity(d)).clone();
}
return MavenUtils.createDependency(d);
}).collect(Collectors.toList());
dependencyManagement.setDependencies(dependencies);
pom.setDependencyManagement(dependencyManagement);

Expand All @@ -196,6 +209,14 @@ private void integrateBaseDependencies() throws MojoExecutionException, IOExcept
MavenUtils.writePomModel(getPomFileOfBundle(dependencyRootDir), pom);
}

protected Map<String, Dependency> getResolvedProjectDependencyManagement() {
if (null == this.mavenProject.getDependencyManagement()) {
return Collections.emptyMap();
}
return this.mavenProject.getDependencyManagement().getDependencies().stream()
.collect(Collectors.toMap(MavenUtils::getDependencyIdentity, d -> d));
}

private void clearDependencyRootDir() {
if (Boolean.parseBoolean(cleanAfterPackageDependencies) && dependencyRootDir != null) {
FileUtils.deleteQuietly(dependencyRootDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ public class Constants {
*/
public final static String PACKAGE_PREFIX_MARK = "*";

public final static String STRING_COLON = ":";

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.Properties;
import java.util.Set;

import static com.alipay.sofa.koupleless.base.build.plugin.constant.Constants.STRING_COLON;

/**
* @author lianglipeng.llp@alibaba-inc.com
* @version $Id: MavenUtils.java, v 0.1 2024年07月15日 13:58 立蓬 Exp $
Expand Down Expand Up @@ -191,6 +193,28 @@ public static Set<Artifact> getDependencyArtifacts(MavenProject mavenProject) th
return statusSets.getResolvedDependencies();
}

public static String getDependencyIdentity(Dependency dependency) {
if (StringUtils.isNotEmpty(dependency.getClassifier())) {
return dependency.getGroupId() + STRING_COLON + dependency.getArtifactId()
+ STRING_COLON + dependency.getVersion() + STRING_COLON
+ dependency.getClassifier();
} else {
return dependency.getGroupId() + STRING_COLON + dependency.getArtifactId()
+ STRING_COLON + dependency.getVersion();
}
}

public static String getArtifactIdentity(Artifact artifact) {
if (artifact.hasClassifier()) {
return artifact.getGroupId() + STRING_COLON + artifact.getArtifactId() + STRING_COLON
+ artifact.getVersion() + ":" + artifact.getClassifier();
} else {
return artifact.getGroupId() + STRING_COLON + artifact.getArtifactId() + STRING_COLON
+ artifact.getVersion();
}

}

private static class DependencyListMojo extends ListMojo {
MavenProject mavenProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

import com.alipay.sofa.koupleless.base.build.plugin.utils.MavenUtils;
import com.alipay.sofa.koupleless.utils.CommonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -35,10 +39,10 @@
import org.mockito.Mockito;

import java.net.URISyntaxException;
import java.util.Collections;

import static com.alipay.sofa.koupleless.utils.ReflectionUtils.setField;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -70,8 +74,13 @@ public void testExecute() throws MojoExecutionException, MojoFailureException,

Model pom = MavenUtils.buildPomModel(CommonUtils
.getResourceFile("mockBaseDir/base-bootstrap/base-all-dependencies-starter/pom.xml"));
Dependency d = pom.getDependencyManagement().getDependencies().stream().findFirst().get();
assertEquals("a1", d.getArtifactId());

assertEquals(2, pom.getDependencyManagement().getDependencies().size());
assertEquals(1, pom.getDependencyManagement().getDependencies().stream()
.filter(d -> d.getArtifactId().equals("a1")).count());
Dependency d2 = pom.getDependencyManagement().getDependencies().stream()
.filter(d -> d.getArtifactId().equals("a2")).findFirst().get();
assertFalse(d2.getExclusions().isEmpty());
}

@Test
Expand Down Expand Up @@ -113,8 +122,26 @@ private MavenProject getMockBootstrapProject() throws URISyntaxException {
when(a1.getArtifactId()).thenReturn("a1");
when(a1.getGroupId()).thenReturn("com.mock.outside");
when(a1.getVersion()).thenReturn("1.0.0");
project.setArtifacts(Collections.singleton(a1));
return project;
when(a1.getBaseVersion()).thenReturn("1.0.0");

Artifact a2 = mock(Artifact.class);
when(a2.getArtifactId()).thenReturn("a2");
when(a2.getGroupId()).thenReturn("com.mock.outside");
when(a2.getVersion()).thenReturn("1.0.0");
when(a2.getBaseVersion()).thenReturn("1.0.0");
project.setArtifacts(Sets.newHashSet(a1, a2));

MavenProject spyProject = spy(project);
DependencyManagement dm = new DependencyManagement();
Dependency d2 = MavenUtils.createDependency(a2);

Exclusion exclusion = new Exclusion();
exclusion.setArtifactId("e1");
exclusion.setGroupId("com.mock.exclustion");
d2.setExclusions(Lists.newArrayList(exclusion));
dm.setDependencies(Lists.newArrayList(d2));
doReturn(dm).when(spyProject).getDependencyManagement();
return spyProject;
}

private MavenProject getRootProject() throws URISyntaxException {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>pom</packaging>

<properties>
<revision>2.1.5</revision>
<revision>2.1.5-SNAPSHOT</revision>
<sofa.ark.version>3.1.6</sofa.ark.version>
<spring.boot.version>3.2.1</spring.boot.version>
<project.encoding>UTF-8</project.encoding>
Expand Down

0 comments on commit 91c69a1

Please sign in to comment.