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
101 changes: 54 additions & 47 deletions src/it/projects/list-repositories/pom.xml
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
<?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</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>
Test dependency:list-repositories
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>

</project>
<?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</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>
Test dependency:list-repositories
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>fake-remote-repository</id>
<url>http://localhost:2345</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.2.5</version>
</dependency>
</dependencies>

</project>
28 changes: 28 additions & 0 deletions src/it/projects/list-repositories/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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" );
assert buildLog.contains( 'Project remote repositories used by this build:');
assert buildLog.contains( '* fake-remote-repository (http://localhost:2345, default, releases+snapshots)');
assert buildLog.contains( '* sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots, default, snapshots) mirrored by mrm-maven-plugin');
assert buildLog.contains( '* central (https://repo.maven.apache.org/maven2, default, releases) mirrored by mrm-maven-plugin');

Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* 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.
*/
package org.apache.maven.plugins.dependency;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;

/**
* Goal that collect all project dependencies and then lists the repositories used by the build and by the transitive
* dependencies
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
* @since 2.2
*/
@Mojo(name = "list-repositories", threadSafe = true)
public class ListRepositoriesMojo extends AbstractDependencyMojo {

@Component
RepositorySystem repositorySystem;

/**
* Displays a list of the repositories used by this build.
*
* @throws MojoExecutionException with a message if an error occurs.
*/
@Override
protected void doExecute() throws MojoExecutionException {

CollectRequest request = new CollectRequest();
request.setRepositories(getProject().getRemoteProjectRepositories());
request.setRootArtifact(RepositoryUtils.toArtifact(getProject().getArtifact()));

ArtifactTypeRegistry artifactTypeRegistry =
session.getRepositorySession().getArtifactTypeRegistry();

request.setDependencies(getProject().getDependencies().stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()));

request.setManagedDependencies(Optional.ofNullable(getProject().getDependencyManagement())
.map(DependencyManagement::getDependencies)
.orElseGet(Collections::emptyList)
.stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()));

try {
CollectResult collectResult = repositorySystem.collectDependencies(session.getRepositorySession(), request);
Set<RemoteRepository> repositories = new HashSet<>();
collectResult.getRoot().accept(new TreeDependencyVisitor(new DependencyVisitor() {
@Override
public boolean visitEnter(DependencyNode node) {
repositories.addAll(node.getRepositories());
return true;
}

@Override
public boolean visitLeave(DependencyNode node) {
return true;
}
}));

StringBuilder message = new StringBuilder();

Map<Boolean, List<RemoteRepository>> repoGroupByMirrors = repositories.stream()
.collect(Collectors.groupingBy(
repo -> repo.getMirroredRepositories().isEmpty()));

prepareRemoteRepositoriesList(message, repoGroupByMirrors.get(Boolean.TRUE));
prepareRemoteMirrorRepositoriesList(message, repoGroupByMirrors.get(Boolean.FALSE));

getLog().info(message);

} catch (DependencyCollectionException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void prepareRemoteMirrorRepositoriesList(
StringBuilder message, Collection<RemoteRepository> remoteProjectRepositories) {

Map<RemoteRepository, RemoteRepository> mirrorMap = new HashMap<>();
remoteProjectRepositories.forEach(repo -> {
repo.getMirroredRepositories().forEach(mrepo -> mirrorMap.put(mrepo, repo));
});

mirrorMap.forEach((repo, mirror) -> message.append(" * ")
.append(repo)
.append(" mirrored by ")
.append(mirror)
.append(System.lineSeparator()));
}

private void prepareRemoteRepositoriesList(
StringBuilder message, Collection<RemoteRepository> remoteProjectRepositories) {

message.append("Project remote repositories used by this build:").append(System.lineSeparator());

remoteProjectRepositories.forEach(
repo -> message.append(" * ").append(repo).append(System.lineSeparator()));
}

private Map<RemoteRepository, RemoteRepository> getMirroredRepo(Set<RemoteRepository> repositories) {
Map<RemoteRepository, RemoteRepository> mirrorMap = new HashMap<>();

repositories.stream()
.filter(repo -> !repo.getMirroredRepositories().isEmpty())
.forEach(repo -> repo.getMirroredRepositories().forEach(mrepo -> mirrorMap.put(mrepo, repo)));

return mirrorMap;
}
}

This file was deleted.

3 changes: 2 additions & 1 deletion src/site/apt/index.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ ${project.name}

*{{{./list-classes-mojo.html}dependency:list-classes}} displays the fully package-qualified names of all classes found in a specified artifact.

*{{{./list-repositories-mojo.html}dependency:list-repositories}} displays all project dependencies and then lists the repositories used.
*{{{./list-repositories-mojo.html}dependency:list-repositories}} collect all project dependencies and then lists the repositories
used by the build and by the transitive dependencies.

*{{{./properties-mojo.html}dependency:properties}} set a property for each project dependency containing the
to the artifact on the file system.
Expand Down