This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new 011cdadc [MDEP-648] Show where each listed repository is declared
011cdadc is described below
commit 011cdadcbca873b7317a0a5a485394bc199fbddc
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 14 18:05:09 2026 +0100
[MDEP-648] Show where each listed repository is declared
---
.../list-repositories-location/invoker.properties | 18 ++++
.../list-repositories-location/parent/pom.xml | 44 ++++++++++
src/it/projects/list-repositories-location/pom.xml | 53 ++++++++++++
.../list-repositories-location/verify.groovy | 47 +++++++++++
.../plugins/dependency/ListRepositoriesMojo.java | 98 ++++++++++++++++++++--
5 files changed, 253 insertions(+), 7 deletions(-)
diff --git a/src/it/projects/list-repositories-location/invoker.properties
b/src/it/projects/list-repositories-location/invoker.properties
new file mode 100644
index 00000000..bc2ff3cd
--- /dev/null
+++ b/src/it/projects/list-repositories-location/invoker.properties
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals =
${project.groupId}:${project.artifactId}:${project.version}:list-repositories
diff --git a/src/it/projects/list-repositories-location/parent/pom.xml
b/src/it/projects/list-repositories-location/parent/pom.xml
new file mode 100644
index 00000000..969a1628
--- /dev/null
+++ b/src/it/projects/list-repositories-location/parent/pom.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.dependency</groupId>
+ <artifactId>test-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <repositories>
+ <repository>
+ <id>parent-repository</id>
+ <url>http://localhost:2346</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>parent-plugin-repository</id>
+ <url>http://localhost:3457</url>
+ </pluginRepository>
+ </pluginRepositories>
+</project>
diff --git a/src/it/projects/list-repositories-location/pom.xml
b/src/it/projects/list-repositories-location/pom.xml
new file mode 100644
index 00000000..fd7302c6
--- /dev/null
+++ b/src/it/projects/list-repositories-location/pom.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.its.dependency</groupId>
+ <artifactId>test-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <relativePath>parent/pom.xml</relativePath>
+ </parent>
+
+ <artifactId>test</artifactId>
+
+ <name>Test</name>
+ <description>
+ Test that dependency:list-repositories reports where each repository is
declared
+ </description>
+
+ <repositories>
+ <repository>
+ <id>fake-remote-repository</id>
+ <url>http://localhost:2345</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>fake-plugin-repository</id>
+ <url>http://localhost:3456</url>
+ </pluginRepository>
+ </pluginRepositories>
+</project>
diff --git a/src/it/projects/list-repositories-location/verify.groovy
b/src/it/projects/list-repositories-location/verify.groovy
new file mode 100644
index 00000000..4c034104
--- /dev/null
+++ b/src/it/projects/list-repositories-location/verify.groovy
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+File file = new File( basedir, "build.log" )
+assert file.exists()
+
+String buildLog = file.getText( "UTF-8" )
+
+// Maven 3 reports the POM location as a plain path, Maven 4 as a file: URI,
so compare only the path tail
+void assertDeclared( String buildLog, String repo, String modelId, String
pomTail )
+{
+ List<String> lines = buildLog.readLines()
+ int index = lines.findIndexOf { it.contains( "* ${repo}" ) }
+ assert index >= 0 : "${repo} not listed"
+ String declared = lines[index + 1].replace( '\\', '/' )
+ assert declared ==~ /.* declared in \Q${modelId}\E
\S*\Q${pomTail}\E:\d+/ : "unexpected location for ${repo}: ${declared}"
+}
+
+assertDeclared( buildLog, "fake-remote-repository (http://localhost:2345,
default, releases+snapshots)",
"org.apache.maven.its.dependency:test:1.0-SNAPSHOT",
"/list-repositories-location/pom.xml" )
+assertDeclared( buildLog, "fake-plugin-repository (http://localhost:3456,
default, releases+snapshots)",
"org.apache.maven.its.dependency:test:1.0-SNAPSHOT",
"/list-repositories-location/pom.xml" )
+assertDeclared( buildLog, "parent-repository (http://localhost:2346, default,
releases+snapshots)",
"org.apache.maven.its.dependency:test-parent:1.0-SNAPSHOT",
"/list-repositories-location/parent/pom.xml" )
+assertDeclared( buildLog, "parent-plugin-repository (http://localhost:3457,
default, releases+snapshots)",
"org.apache.maven.its.dependency:test-parent:1.0-SNAPSHOT",
"/list-repositories-location/parent/pom.xml" )
+
+if ( !mavenVersion.startsWith( '4.' ) )
+{
+ // Maven 4 drops central from the super POM model
+ List<String> lines = buildLog.readLines()
+ int central = lines.findIndexOf { it.contains( '* central
(https://repo.maven.apache.org/maven2' ) }
+ assert central >= 0 : 'central not listed'
+ assert lines[central + 1].endsWith( ' declared in the Maven super POM' )
: lines[central + 1]
+}
diff --git
a/src/main/java/org/apache/maven/plugins/dependency/ListRepositoriesMojo.java
b/src/main/java/org/apache/maven/plugins/dependency/ListRepositoriesMojo.java
index 82f2fb5a..8e09235a 100644
---
a/src/main/java/org/apache/maven/plugins/dependency/ListRepositoriesMojo.java
+++
b/src/main/java/org/apache/maven/plugins/dependency/ListRepositoriesMojo.java
@@ -33,6 +33,9 @@ import java.util.stream.Collectors;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.DependencyManagement;
+import org.apache.maven.model.InputLocation;
+import org.apache.maven.model.InputSource;
+import org.apache.maven.model.Repository;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
@@ -119,8 +122,16 @@ public class ListRepositoriesMojo extends
AbstractDependencyMojo {
}
StringBuilder message = new StringBuilder();
- prepareRemoteRepositoriesList(message, "Project remote
repositories used by this build:", repositories);
- prepareRemoteRepositoriesList(message, "Plugin repositories used
by this build:", pluginRepositories);
+ prepareRemoteRepositoriesList(
+ message,
+ "Project remote repositories used by this build:",
+ repositories,
+ declaredLocations(getProject().getRepositories()));
+ prepareRemoteRepositoriesList(
+ message,
+ "Plugin repositories used by this build:",
+ pluginRepositories,
+ declaredLocations(getProject().getPluginRepositories()));
getLog().info(message);
@@ -145,8 +156,74 @@ public class ListRepositoriesMojo extends
AbstractDependencyMojo {
});
}
+ /**
+ * Maps each repository on the project model that carries an input
location (the project, its parent POMs, and
+ * the super POM) to a human readable description of where it was
declared. Repositories that only come from the
+ * POMs of transitive dependencies are not part of the project model and
have no entry.
+ */
+ private static Map<String, String> declaredLocations(List<Repository>
modelRepositories) {
+ Map<String, String> locations = new HashMap<>();
+ if (modelRepositories == null) {
+ return locations;
+ }
+ for (Repository repository : modelRepositories) {
+ String description = describeLocation(repository);
+ if (description != null) {
+ locations.putIfAbsent(locationKey(repository.getId(),
repository.getUrl()), description);
+ }
+ }
+ return locations;
+ }
+
+ private static String locationKey(String id, String url) {
+ String normalizedUrl = url == null ? "" : url.trim();
+ while (normalizedUrl.endsWith("/")) {
+ normalizedUrl = normalizedUrl.substring(0, normalizedUrl.length()
- 1);
+ }
+ return id + '\n' + normalizedUrl;
+ }
+
+ private static String describeLocation(Repository repository) {
+ InputLocation location = repository.getLocation("");
+ if (location == null) {
+ location = repository.getLocation("id");
+ }
+ if (location == null || location.getSource() == null) {
+ return null;
+ }
+ InputSource source = location.getSource();
+ if (source.getModelId() != null &&
source.getModelId().endsWith(":super-pom")) {
+ return "the Maven super POM";
+ }
+ StringBuilder description = new StringBuilder();
+ if (source.getModelId() != null && !source.getModelId().isEmpty()) {
+ description.append(source.getModelId());
+ }
+ if (source.getLocation() != null) {
+ if (description.length() > 0) {
+ description.append(' ');
+ }
+ description.append(source.getLocation());
+ if (location.getLineNumber() > 0) {
+ description.append(':').append(location.getLineNumber());
+ }
+ }
+ return description.length() == 0 ? null : description.toString();
+ }
+
+ /**
+ * Returns an indented line saying where the repository was declared, or
an empty string when that is unknown. It
+ * goes on its own line so the repository lines keep their format.
+ */
+ private static String declaredLine(RemoteRepository repo, Map<String,
String> locations) {
+ String declared = locations.get(locationKey(repo.getId(),
repo.getUrl()));
+ return declared == null ? "" : " declared in " + declared +
System.lineSeparator();
+ }
+
private void prepareRemoteMirrorRepositoriesList(
- StringBuilder message, Collection<RemoteRepository>
remoteProjectRepositories) {
+ StringBuilder message,
+ Collection<RemoteRepository> remoteProjectRepositories,
+ Map<String, String> locations) {
Map<RemoteRepository, RemoteRepository> mirrorMap = new HashMap<>();
remoteProjectRepositories.forEach(
@@ -156,11 +233,15 @@ public class ListRepositoriesMojo extends
AbstractDependencyMojo {
.append(repo)
.append(" mirrored by ")
.append(mirror)
- .append(System.lineSeparator()));
+ .append(System.lineSeparator())
+ .append(declaredLine(repo, locations)));
}
private void prepareRemoteRepositoriesList(
- StringBuilder message, String heading,
Collection<RemoteRepository> repositories) {
+ StringBuilder message,
+ String heading,
+ Collection<RemoteRepository> repositories,
+ Map<String, String> locations) {
if (repositories.isEmpty()) {
return;
}
@@ -173,8 +254,11 @@ public class ListRepositoriesMojo extends
AbstractDependencyMojo {
repoGroupByMirrors
.getOrDefault(Boolean.TRUE, Collections.emptyList())
- .forEach(repo -> message.append(" *
").append(repo).append(System.lineSeparator()));
+ .forEach(repo -> message.append(" * ")
+ .append(repo)
+ .append(System.lineSeparator())
+ .append(declaredLine(repo, locations)));
prepareRemoteMirrorRepositoriesList(
- message, repoGroupByMirrors.getOrDefault(Boolean.FALSE,
Collections.emptyList()));
+ message, repoGroupByMirrors.getOrDefault(Boolean.FALSE,
Collections.emptyList()), locations);
}
}