Skip to content
Merged
Show file tree
Hide file tree
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 @@ -22,6 +22,7 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -85,6 +86,11 @@ public final class PrefixesRemoteRepositoryFilterSource extends RemoteRepository

private static final String PREFIX_FILE_PATH = ".meta/prefixes.txt";

/**
* Visible for UT.
*/
static final String PREFIX_FIRST_LINE = "## repository-prefixes/2.0";

/**
* Is filter enabled?
*
Expand Down Expand Up @@ -183,7 +189,7 @@ private PrefixTree loadPrefixTree(
if (filePath == null) {
filePath = resolvePrefixesFromRemoteRepository(session, remoteRepository);
}
if (filePath != null) {
if (isPrefixFile(filePath)) {
logger.debug(
"Loading prefixes for remote repository {} from file '{}'", remoteRepository.getId(), filePath);
try (Stream<String> lines = Files.lines(filePath, StandardCharsets.UTF_8)) {
Expand All @@ -204,6 +210,19 @@ private PrefixTree loadPrefixTree(
return PrefixTree.SENTINEL;
}

private boolean isPrefixFile(Path path) {
if (path == null || !Files.isRegularFile(path)) {
return false;
}
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
return PREFIX_FIRST_LINE.equals(reader.readLine());
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private Path resolvePrefixesFromLocalConfiguration(
RepositorySystemSession session, Path baseDir, RemoteRepository remoteRepository) {
Path filePath = baseDir.resolve(PREFIXES_FILE_PREFIX + remoteRepository.getId() + PREFIXES_FILE_SUFFIX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ protected void allowArtifact(
Path baseDir = session.getLocalRepository()
.getBasePath()
.resolve(PrefixesRemoteRepositoryFilterSource.LOCAL_REPO_PREFIX_DIR);
Path groupId = baseDir.resolve(PrefixesRemoteRepositoryFilterSource.PREFIXES_FILE_PREFIX
Path prefixes = baseDir.resolve(PrefixesRemoteRepositoryFilterSource.PREFIXES_FILE_PREFIX
+ remoteRepository.getId()
+ PrefixesRemoteRepositoryFilterSource.PREFIXES_FILE_SUFFIX);
Files.createDirectories(groupId.getParent());
Files.write(groupId, artifact.getGroupId().replaceAll("\\.", "/").getBytes(StandardCharsets.UTF_8));
Files.createDirectories(prefixes.getParent());
Files.write(
prefixes,
(PrefixesRemoteRepositoryFilterSource.PREFIX_FIRST_LINE + "\n"
+ artifact.getGroupId().replaceAll("\\.", "/"))
.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
3 changes: 3 additions & 0 deletions src/site/markdown/remote-repository-filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ Many MRMs and Maven Central itself publish these files. Some prefixes file examp
The user provided prefixes files are expected in the following location by default:
`${filterBasedir}/prefixes-${remoteRepository.id}.txt`.

**Important**: Valid prefix files start with following "magic" on their very first line: `## repository-prefixes/2.0`.
If the first line in file is not this string, the prefix file is discarded.

To disable prefixes filter, use the following setting: `-Daether.remoteRepositoryFilter.prefixes=false`.
To disable for single repository filtering, append to key `.repoId`.

Expand Down
Loading