Skip to content

Commit

Permalink
[Improve] MavenTool minor improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
benjobs committed Nov 11, 2023
1 parent 5fb3c6c commit f427894
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -71,9 +70,8 @@ public boolean equals(Object that) {

File localJar = WebUtils.getAppTempDir();
File localUploads = new File(Workspace.local().APP_UPLOADS());
HashSet<String> otherJars = new HashSet<>(thatDep.jar);
for (String jarName : jar) {
if (!otherJars.contains(jarName)
if (!thatDep.jar.contains(jarName)
|| !FileUtils.equals(new File(localJar, jarName), new File(localUploads, jarName))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,57 +193,55 @@ object MavenTool extends Logger {
*/
@throws[Exception]
def resolveArtifacts(mavenArtifacts: Set[Artifact]): Set[File] = {
if (mavenArtifacts == null) Set.empty[File];
else {
val (repoSystem, session) = getMavenEndpoint()
val artifacts = mavenArtifacts.map(
e => {
new DefaultArtifact(
e.groupId,
e.artifactId,
e.classifier,
"jar",
e.version) -> e.extensions
})
logInfo(s"start resolving dependencies: ${artifacts.mkString}")

val remoteRepos = getRemoteRepos()
// read relevant artifact descriptor info
// plz don't simplify the following lambda syntax to maintain the readability of the code.
val dependenciesArtifacts = artifacts
.map(
artifact => new ArtifactDescriptorRequest(artifact._1, remoteRepos, null) -> artifact._2)
.map(descReq => repoSystem.readArtifactDescriptor(session, descReq._1) -> descReq._2)
.flatMap(
descResult => {
descResult._1.getDependencies.filter(
d => {
val ga = s"${d.getArtifact.getGroupId}:${d.getArtifact.getArtifactId}"
val exclusion = descResult._2.contains(ga)
if (exclusion) {
val art = descResult._1.getArtifact
val name = s"${art.getGroupId}:${art.getArtifactId}"
logInfo(s"[MavenTool] $name dependencies exclusion $ga")
if (mavenArtifacts == null) {
return Set.empty[File]
}

val (repoSystem, session) = getMavenEndpoint()
val artifacts = mavenArtifacts.map(
e => {
new DefaultArtifact(e.groupId, e.artifactId, e.classifier, "jar", e.version) -> e.extensions
})

logInfo(s"start resolving dependencies: ${artifacts.mkString}")

val remoteRepos = getRemoteRepos()
// read relevant artifact descriptor info
// plz don't simplify the following lambda syntax to maintain the readability of the code.
val dependenciesArtifacts = artifacts
.map(artifact => new ArtifactDescriptorRequest(artifact._1, remoteRepos, null) -> artifact._2)
.map(descReq => repoSystem.readArtifactDescriptor(session, descReq._1) -> descReq._2)
.flatMap(
result =>
result._1.getDependencies
.filter(
dep => {
dep.getScope match {
case "compile" if !excludeArtifact.exists(_.filter(dep.getArtifact)) =>
val ga = s"${dep.getArtifact.getGroupId}:${dep.getArtifact.getArtifactId}"
val exclusion = result._2.contains(ga)
if (exclusion) {
val art = result._1.getArtifact
val name = s"${art.getGroupId}:${art.getArtifactId}"
logInfo(s"[MavenTool] $name dependencies exclusion $ga")
}
!exclusion
case _ => false
}
!exclusion
})
})
.filter(_.getScope == "compile")
.filter(x => !excludeArtifact.exists(_.filter(x.getArtifact)))
.map(_.getArtifact)

val mergedArtifacts = artifacts.map(_._1) ++ dependenciesArtifacts

logInfo(s"resolved dependencies: ${mergedArtifacts.mkString}")

// download artifacts
val artReqs =
mergedArtifacts.map(artifact => new ArtifactRequest(artifact, remoteRepos, null))
repoSystem
.resolveArtifacts(session, artReqs)
.map(_.getArtifact.getFile)
.toSet
}
.map(_.getArtifact))

val mergedArtifacts = artifacts.map(_._1) ++ dependenciesArtifacts

logInfo(s"resolved dependencies: ${mergedArtifacts.mkString}")

// download artifacts
val artReqs =
mergedArtifacts.map(artifact => new ArtifactRequest(artifact, remoteRepos, null))
repoSystem
.resolveArtifacts(session, artReqs)
.map(_.getArtifact.getFile)
.toSet
}

/** create composite maven endpoint */
Expand Down

0 comments on commit f427894

Please sign in to comment.