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

[MNG-8370] Add maven.repo.local.head #1915

Open
wants to merge 1 commit into
base: maven-3.9.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -66,8 +67,6 @@
import org.eclipse.aether.util.repository.SimpleResolutionErrorPolicy;
import org.eclipse.sisu.Nullable;

import static java.util.stream.Collectors.toList;

/**
* @since 3.3.0
*/
Expand All @@ -82,6 +81,16 @@ public class DefaultRepositorySystemSessionFactory {
*/
private static final String MAVEN_REPO_LOCAL_TAIL = "maven.repo.local.tail";

/**
* User property for chained LRM: the new "head" local repository to use, and "push" the existing into tail.
* Similar to <code>maven.repo.local.tail</code>, this property may contain comma separated list of paths to be
* used as local repositories (combine with chained local repository), but while latter is "appending" this
* one is "prepending".
*
* @since 3.9.10
*/
public static final String MAVEN_REPO_LOCAL_HEAD = "maven.repo.local.head";

/**
* User property for chained LRM: should artifact availability be ignored in tail local repositories or not.
* Default: {@code true}, will ignore availability from tail local repositories.
Expand Down Expand Up @@ -363,25 +372,54 @@ public DefaultRepositorySystemSession newRepositorySession(MavenExecutionRequest
}

private void setUpLocalRepositoryManager(MavenExecutionRequest request, DefaultRepositorySystemSession session) {
LocalRepository localRepo =
new LocalRepository(request.getLocalRepository().getBasedir());
List<String> paths = new ArrayList<>();
String localRepoHead = ConfigUtils.getString(session, null, MAVEN_REPO_LOCAL_HEAD);
if (localRepoHead != null) {
Arrays.stream(localRepoHead.split(","))
.filter(p -> p != null && !p.trim().isEmpty())
.map(this::resolve)
.forEach(paths::add);
}

LocalRepositoryManager lrm = repoSystem.newLocalRepositoryManager(session, localRepo);
paths.add(request.getLocalRepository().getBasedir());

String localRepoTail = ConfigUtils.getString(session, null, MAVEN_REPO_LOCAL_TAIL);
if (localRepoTail != null) {
boolean ignoreTailAvailability =
ConfigUtils.getBoolean(session, true, MAVEN_REPO_LOCAL_TAIL_IGNORE_AVAILABILITY);
List<LocalRepositoryManager> tail = new ArrayList<>();
List<String> paths = Arrays.stream(localRepoTail.split(","))
Arrays.stream(localRepoTail.split(","))
.filter(p -> p != null && !p.trim().isEmpty())
.collect(toList());
.map(this::resolve)
.forEach(paths::add);
}

LocalRepository localRepo = new LocalRepository(paths.remove(0));
LocalRepositoryManager lrm = repoSystem.newLocalRepositoryManager(session, localRepo);

if (paths.isEmpty()) {
// we have only one local repo path
session.setLocalRepositoryManager(lrm);
} else {
List<LocalRepositoryManager> tail = new ArrayList<>();
for (String path : paths) {
tail.add(repoSystem.newLocalRepositoryManager(session, new LocalRepository(path)));
}
boolean ignoreTailAvailability =
ConfigUtils.getBoolean(session, true, MAVEN_REPO_LOCAL_TAIL_IGNORE_AVAILABILITY);

session.setLocalRepositoryManager(new ChainedLocalRepositoryManager(lrm, tail, ignoreTailAvailability));
}
}

private String resolve(String string) {
if (string.startsWith("~/") || string.startsWith("~\\")) {
// resolve based on $HOME
return Paths.get(System.getProperty("user.home"))
.resolve(string.substring(2))
.normalize()
.toAbsolutePath()
.toString();
} else {
session.setLocalRepositoryManager(lrm);
// resolve based on $CWD
return Paths.get(string).normalize().toAbsolutePath().toString();
}
}

Expand Down