Skip to content
Draft
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ under the License.
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-analyzer</artifactId>
<version>1.17.2</version>
<version>1.17.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.analyzer.ArtifactKeys;

/**
* This mojo looks at the dependencies after final resolution and looks for mismatches in your dependencyManagement
Expand Down Expand Up @@ -222,7 +223,7 @@ public List<Artifact> getExclusionErrors(Map<String, Exclusion> exclusions, Set<
* @return the resulting GA
*/
public String getExclusionKey(Artifact artifact) {
return artifact.getGroupId() + ":" + artifact.getArtifactId();
return ArtifactKeys.groupArtifactId(artifact);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package org.apache.maven.plugins.dependency.analyze;

import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.doxia.sink.Sink;
Expand Down Expand Up @@ -96,13 +99,51 @@ protected void renderBody() {
reported = true;
}

// Generate the classes provided by more than one dependency:
Set<String> ambiguousClasses = analysis.getAmbiguousUsedDependencyClasses();
if (!ambiguousClasses.isEmpty()) {
startSection(getI18nString("AmbiguousUsedClasses"));
renderAmbiguousClassesTable(
ambiguousClasses,
analysis.getCandidateArtifactsByUsedClass(),
analysis.getSelectedArtifactByUsedClass());
endSection();
reported = true;
}

if (!reported) {
text(getI18nString("noDependencyProblems"));
}

endSection();
}

private void renderAmbiguousClassesTable(
Set<String> ambiguousClasses, Map<String, Set<Artifact>> candidates, Map<String, Artifact> selected) {
startTable();
tableHeader(new String[] {"Used class", "Candidate artifacts", "Selected artifact"});

for (String className : ambiguousClasses) {
StringBuilder candidateList = new StringBuilder();
for (Artifact candidate : candidates.getOrDefault(className, Collections.<Artifact>emptySet())) {
if (candidateList.length() > 0) {
candidateList.append(", ");
}
candidateList.append(coordinates(candidate));
}
Artifact selectedArtifact = selected.get(className);
tableRow(new String[] {
className, candidateList.toString(), selectedArtifact == null ? "" : coordinates(selectedArtifact)
});
}

endTable();
}

private static String coordinates(Artifact artifact) {
return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getVersion();
}

private void renderDependenciesTable(Sink sink, Collection<Artifact> artifacts) {
startTable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.shared.dependency.analyzer.ArtifactKeys;
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;

/**
Expand All @@ -39,11 +40,11 @@ final class ForcedUsage {
ForcedUsage(ProjectDependencyAnalysis analysis, String[] usedDependencies) {
Set<String> usedDeclared = new HashSet<>();
for (Artifact artifact : analysis.getUsedDeclaredArtifacts()) {
usedDeclared.add(key(artifact));
usedDeclared.add(ArtifactKeys.groupArtifactId(artifact));
}
// the analyzer first matches an entry against the unused declared artifacts, so such an entry is needed
for (Artifact artifact : analysis.getUnusedDeclaredArtifacts()) {
usedDeclared.remove(key(artifact));
usedDeclared.remove(ArtifactKeys.groupArtifactId(artifact));
}
for (String entry : usedDependencies) {
(usedDeclared.contains(entry) ? unneeded : toForce).add(entry);
Expand All @@ -67,8 +68,4 @@ String[] getToForce() {
static String message(String entry) {
return "Unneeded forced usage of " + entry + ": the dependency is already detected as used";
}

private static String key(Artifact artifact) {
return artifact.getGroupId() + ":" + artifact.getArtifactId();
}
}
1 change: 1 addition & 0 deletions src/main/resources/analyze-report.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ report.analyze.UsedDeclaredDependencies=Used and Declared Dependencies
report.analyze.UsedUndeclaredDependencies=Used but Undeclared Dependencies
report.analyze.UnusedDeclaredDependencies=Unused but Declared Dependencies
report.analyze.CompileScopeTestOnlyDependencies=Compile Scoped Test Dependencies
report.analyze.AmbiguousUsedClasses=Ambiguous Used Classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.analyze;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
import org.codehaus.plexus.i18n.I18N;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class AnalyzeReportRendererTest {

@Mock
private Sink sink;

@Mock
private I18N i18n;

@Test
void rendersAmbiguousUsedClassesWithCandidatesAndSelectedArtifact() {
Artifact api = artifact("com.example", "api", "1.0");
Artifact shaded = artifact("com.example", "shaded", "2.0");
Map<String, Set<Artifact>> candidates = new LinkedHashMap<>();
candidates.put("com.example.Service", new LinkedHashSet<>(Arrays.asList(api, shaded)));
Map<String, Artifact> selected = new LinkedHashMap<>();
selected.put("com.example.Service", shaded);
ProjectDependencyAnalysis analysis = new ProjectDependencyAnalysis(
Collections.emptyMap(),
Collections.emptyMap(),
Collections.emptySet(),
Collections.emptySet(),
Collections.singleton("com.example.Service"),
candidates,
selected);

render(analysis);

ArgumentCaptor<String> written = ArgumentCaptor.forClass(String.class);
verify(sink, atLeastOnce()).text(written.capture());
assertThat(written.getAllValues())
.contains(
"com.example.Service", "com.example:api:1.0, com.example:shaded:2.0", "com.example:shaded:2.0");
}

@Test
void omitsAmbiguousSectionForAnalysisWithoutCandidateData() {
ProjectDependencyAnalysis analysis = new ProjectDependencyAnalysis(
Collections.emptyMap(), Collections.emptyMap(), Collections.emptySet(), Collections.emptySet());

render(analysis);

verify(sink, never()).text("report.analyze.AmbiguousUsedClasses");
}

private void render(ProjectDependencyAnalysis analysis) {
when(i18n.getString(anyString(), any(Locale.class), anyString()))
.thenAnswer(invocation -> invocation.getArgument(2));
new AnalyzeReportRenderer(sink, i18n, Locale.ENGLISH, analysis).renderBody();
}

private static Artifact artifact(String groupId, String artifactId, String version) {
return new DefaultArtifact(
groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar", null, new DefaultArtifactHandler("jar"));
}
}
Loading