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-8578]add exclusion scope to repo, allow exclude dependencies from build(including parent, dependencies, etc) #2106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions api/maven-api-model/src/main/mdo/maven.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,16 @@
<multiplicity>*</multiplicity>
</association>
</field>
<field>
<name>exclusions</name>
<version>4.0.0+</version>
<description>Lists a set of artifacts that should be excluded from this repo's
dependency list when it comes to calculating transitive dependencies.</description>
<association>
<type>Exclusion</type>
<multiplicity>*</multiplicity>
</association>
</field>
<field xdoc.separator="blank">
<name>repositories</name>
<version>4.0.0+</version>
Expand Down Expand Up @@ -1810,6 +1820,16 @@
</description>
<type>String</type>
</field>
<field>
<name>exclusions</name>
<version>4.0.0+</version>
<description>Lists a set of artifacts that should be excluded from this repo's
dependency list when it comes to calculating transitive dependencies.</description>
<association>
<type>Exclusion</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
<codeSegments>
<codeSegment>
Expand Down
87 changes: 57 additions & 30 deletions impl/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Expand Down Expand Up @@ -65,11 +67,13 @@
*/
public class RepositoryUtils {

private static String nullify(String string) {
@Nullable
private static String nullify(@Nullable String string) {
return (string == null || string.isEmpty()) ? null : string;
}

public static org.apache.maven.artifact.Artifact toArtifact(Dependency dependency) {
@Nullable
public static org.apache.maven.artifact.Artifact toArtifact(@Nullable Dependency dependency) {
if (dependency == null) {
return null;
}
Expand All @@ -81,7 +85,8 @@ public static org.apache.maven.artifact.Artifact toArtifact(Dependency dependenc
return result;
}

public static org.apache.maven.artifact.Artifact toArtifact(Artifact artifact) {
@Nullable
public static org.apache.maven.artifact.Artifact toArtifact(@Nullable Artifact artifact) {
if (artifact == null) {
return null;
}
Expand Down Expand Up @@ -112,10 +117,10 @@ public static org.apache.maven.artifact.Artifact toArtifact(Artifact artifact) {
}

public static void toArtifacts(
Collection<org.apache.maven.artifact.Artifact> artifacts,
Collection<? extends DependencyNode> nodes,
List<String> trail,
DependencyFilter filter) {
@Nonnull Collection<org.apache.maven.artifact.Artifact> artifacts,
@Nonnull Collection<? extends DependencyNode> nodes,
@Nonnull List<String> trail,
@Nullable DependencyFilter filter) {
for (DependencyNode node : nodes) {
org.apache.maven.artifact.Artifact artifact = toArtifact(node.getDependency());

Expand All @@ -132,7 +137,8 @@ public static void toArtifacts(
}
}

public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
@Nullable
public static Artifact toArtifact(@Nullable org.apache.maven.artifact.Artifact artifact) {
if (artifact == null) {
return null;
}
Expand Down Expand Up @@ -162,7 +168,8 @@ public static Artifact toArtifact(org.apache.maven.artifact.Artifact artifact) {
}

public static Dependency toDependency(
org.apache.maven.artifact.Artifact artifact, Collection<org.apache.maven.model.Exclusion> exclusions) {
@Nullable org.apache.maven.artifact.Artifact artifact,
@Nullable Collection<org.apache.maven.model.Exclusion> exclusions) {
if (artifact == null) {
return null;
}
Expand All @@ -175,13 +182,15 @@ public static Dependency toDependency(
return new Dependency(result, artifact.getScope(), artifact.isOptional(), excl);
}

public static List<RemoteRepository> toRepos(List<ArtifactRepository> repos) {
@Nonnull
public static List<RemoteRepository> toRepos(@Nullable List<ArtifactRepository> repos) {
return Optional.ofNullable(repos).orElse(Collections.emptyList()).stream()
.map(RepositoryUtils::toRepo)
.collect(Collectors.toList());
}

public static RemoteRepository toRepo(ArtifactRepository repo) {
@Nullable
public static RemoteRepository toRepo(@Nullable ArtifactRepository repo) {
RemoteRepository result = null;
if (repo != null) {
RemoteRepository.Builder builder =
Expand All @@ -197,7 +206,8 @@ public static RemoteRepository toRepo(ArtifactRepository repo) {
return result;
}

public static String getLayout(ArtifactRepository repo) {
@Nonnull
public static String getLayout(@Nonnull ArtifactRepository repo) {
try {
return repo.getLayout().getId();
} catch (LinkageError e) {
Expand All @@ -216,15 +226,17 @@ public static String getLayout(ArtifactRepository repo) {
}
}

private static RepositoryPolicy toPolicy(ArtifactRepositoryPolicy policy) {
@Nullable
private static RepositoryPolicy toPolicy(@Nullable ArtifactRepositoryPolicy policy) {
RepositoryPolicy result = null;
if (policy != null) {
result = new RepositoryPolicy(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy());
}
return result;
}

private static Authentication toAuthentication(org.apache.maven.artifact.repository.Authentication auth) {
@Nullable
private static Authentication toAuthentication(@Nullable org.apache.maven.artifact.repository.Authentication auth) {
Authentication result = null;
if (auth != null) {
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
Expand All @@ -235,7 +247,8 @@ private static Authentication toAuthentication(org.apache.maven.artifact.reposit
return result;
}

private static Proxy toProxy(org.apache.maven.repository.Proxy proxy) {
@Nullable
private static Proxy toProxy(@Nullable org.apache.maven.repository.Proxy proxy) {
Proxy result = null;
if (proxy != null) {
AuthenticationBuilder authBuilder = new AuthenticationBuilder();
Expand All @@ -245,7 +258,8 @@ private static Proxy toProxy(org.apache.maven.repository.Proxy proxy) {
return result;
}

public static ArtifactHandler newHandler(Artifact artifact) {
@Nonnull
public static ArtifactHandler newHandler(@Nonnull Artifact artifact) {
String type = artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension());
return new DefaultArtifactHandler(
type,
Expand All @@ -258,7 +272,8 @@ public static ArtifactHandler newHandler(Artifact artifact) {
Boolean.parseBoolean(artifact.getProperty(MavenArtifactProperties.CONSTITUTES_BUILD_PATH, "")));
}

public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
@Nonnull
public static ArtifactType newArtifactType(@Nonnull String id, @Nonnull ArtifactHandler handler) {
return new DefaultArtifactType(
id,
handler.getExtension(),
Expand All @@ -268,8 +283,9 @@ public static ArtifactType newArtifactType(String id, ArtifactHandler handler) {
handler.isIncludesDependencies());
}

@Nonnull
public static Dependency toDependency(
org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
@Nonnull org.apache.maven.model.Dependency dependency, @Nonnull ArtifactTypeRegistry stereotypes) {
ArtifactType stereotype = stereotypes.get(dependency.getType());
if (stereotype == null) {
stereotype = new DefaultArtifactType(dependency.getType());
Expand Down Expand Up @@ -303,38 +319,46 @@ public static Dependency toDependency(
exclusions);
}

private static Exclusion toExclusion(org.apache.maven.model.Exclusion exclusion) {
@Nonnull
public static Exclusion toExclusion(@Nonnull org.apache.maven.model.Exclusion exclusion) {
return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
}

public static ArtifactTypeRegistry newArtifactTypeRegistry(ArtifactHandlerManager handlerManager) {
@Nonnull
public static ArtifactTypeRegistry newArtifactTypeRegistry(@Nonnull ArtifactHandlerManager handlerManager) {
return new MavenArtifactTypeRegistry(handlerManager);
}

static class MavenArtifactTypeRegistry implements ArtifactTypeRegistry {

@Nonnull
private final ArtifactHandlerManager handlerManager;

MavenArtifactTypeRegistry(ArtifactHandlerManager handlerManager) {
MavenArtifactTypeRegistry(@Nonnull ArtifactHandlerManager handlerManager) {
this.handlerManager = handlerManager;
}

public ArtifactType get(String stereotypeId) {
@Nullable
@Override
public ArtifactType get(@Nonnull String stereotypeId) {
ArtifactHandler handler = handlerManager.getArtifactHandler(stereotypeId);
return newArtifactType(stereotypeId, handler);
}
}

public static Collection<Artifact> toArtifacts(Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
@Nonnull
public static Collection<Artifact> toArtifacts(
@Nonnull Collection<org.apache.maven.artifact.Artifact> artifactsToConvert) {
return artifactsToConvert.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toList());
}

public static WorkspaceRepository getWorkspace(RepositorySystemSession session) {
@Nullable
public static WorkspaceRepository getWorkspace(@Nonnull RepositorySystemSession session) {
WorkspaceReader reader = session.getWorkspaceReader();
return (reader != null) ? reader.getRepository() : null;
}

public static boolean repositoriesEquals(List<RemoteRepository> r1, List<RemoteRepository> r2) {
public static boolean repositoriesEquals(@Nonnull List<RemoteRepository> r1, @Nonnull List<RemoteRepository> r2) {
if (r1.size() != r2.size()) {
return false;
}
Expand All @@ -348,16 +372,19 @@ public static boolean repositoriesEquals(List<RemoteRepository> r1, List<RemoteR
return true;
}

public static int repositoriesHashCode(List<RemoteRepository> repositories) {
public static int repositoriesHashCode(@Nonnull List<RemoteRepository> repositories) {
int result = 17;
for (RemoteRepository repository : repositories) {
result = 31 * result + repositoryHashCode(repository);
}
return result;
}

@Nullable
public static RepositorySystemSession overlay(
ArtifactRepository repository, RepositorySystemSession session, RepositorySystem system) {
@Nullable ArtifactRepository repository,
@Nullable RepositorySystemSession session,
@Nonnull RepositorySystem system) {
if (repository == null || repository.getBasedir() == null) {
return session;
}
Expand All @@ -379,22 +406,22 @@ public static RepositorySystemSession overlay(
return newSession;
}

private static int repositoryHashCode(RemoteRepository repository) {
private static int repositoryHashCode(@Nonnull RemoteRepository repository) {
int result = 17;
Object obj = repository.getUrl();
result = 31 * result + (obj != null ? obj.hashCode() : 0);
return result;
}

private static boolean policyEquals(RepositoryPolicy p1, RepositoryPolicy p2) {
private static boolean policyEquals(@Nonnull RepositoryPolicy p1, @Nonnull RepositoryPolicy p2) {
if (p1 == p2) {
return true;
}
// update policy doesn't affect contents
return p1.isEnabled() == p2.isEnabled() && Objects.equals(p1.getChecksumPolicy(), p2.getChecksumPolicy());
}

private static boolean repositoryEquals(RemoteRepository r1, RemoteRepository r2) {
private static boolean repositoryEquals(@Nonnull RemoteRepository r1, @Nonnull RemoteRepository r2) {
if (r1 == r2) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public DependencyResolutionResult resolve(DependencyResolutionRequest request)
}
}

List<Exclusion> exclusions = project.getExclusions();
if (exclusions != null) {
for (Exclusion exclusion : exclusions) {
collect.addExclusion(RepositoryUtils.toExclusion(exclusion));
}
}

DependencyRequest depRequest = new DependencyRequest(collect, filter);
depRequest.setTrace(trace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Developer;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Extension;
import org.apache.maven.model.IssueManagement;
import org.apache.maven.model.License;
Expand Down Expand Up @@ -312,6 +313,10 @@ public DependencyManagement getDependencyManagement() {
return getModel().getDependencyManagement();
}

public List<Exclusion> getExclusions() {
return getModel().getExclusions();
}

// ----------------------------------------------------------------------
// Test and compile source roots.
// ----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ under the License.
<plexusInterpolationVersion>1.27</plexusInterpolationVersion>
<plexusTestingVersion>1.4.0</plexusTestingVersion>
<plexusXmlVersion>4.0.4</plexusXmlVersion>
<resolverVersion>2.0.6</resolverVersion>
<resolverVersion>2.0.7.exclusion</resolverVersion>
<securityDispatcherVersion>4.1.0</securityDispatcherVersion>
<sisuVersion>0.9.0.M3</sisuVersion>
<slf4jVersion>2.0.16</slf4jVersion>
Expand Down