Skip to content

Commit

Permalink
Extract a lightweight API implementation to be used outside of maven …
Browse files Browse the repository at this point in the history
…(wip)
  • Loading branch information
gnodet committed Mar 15, 2024
1 parent 33788fd commit ad581ba
Show file tree
Hide file tree
Showing 67 changed files with 991 additions and 302 deletions.
72 changes: 72 additions & 0 deletions maven-api-impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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</groupId>
<artifactId>maven</artifactId>
<version>4.0.0-alpha-14-SNAPSHOT</version>
</parent>

<artifactId>maven-api-impl</artifactId>
<name>Maven API Implementation</name>
<description>Provides the implementation classes for the Maven API</description>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-di</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-spi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-util</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-supplier</artifactId>
<version>${resolverVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-toolchain-builder</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,77 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.maven.api.*;
import org.apache.maven.api.Artifact;
import org.apache.maven.api.ArtifactCoordinate;
import org.apache.maven.api.Dependency;
import org.apache.maven.api.DependencyCoordinate;
import org.apache.maven.api.DependencyScope;
import org.apache.maven.api.Language;
import org.apache.maven.api.Listener;
import org.apache.maven.api.LocalRepository;
import org.apache.maven.api.Node;
import org.apache.maven.api.Packaging;
import org.apache.maven.api.PathScope;
import org.apache.maven.api.PathType;
import org.apache.maven.api.Project;
import org.apache.maven.api.ProjectScope;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.Service;
import org.apache.maven.api.Session;
import org.apache.maven.api.SessionData;
import org.apache.maven.api.Type;
import org.apache.maven.api.Version;
import org.apache.maven.api.VersionConstraint;
import org.apache.maven.api.VersionRange;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.model.Repository;
import org.apache.maven.api.services.*;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;

import static org.apache.maven.internal.impl.Utils.map;
import static org.apache.maven.internal.impl.Utils.nonNull;

public abstract class AbstractSession implements InternalSession {

protected final RepositorySystemSession session;
protected final RepositorySystem repositorySystem;
protected final List<RemoteRepository> repositories;
protected final Lookup lookup;
private final Map<Class<? extends Service>, Service> services = new ConcurrentHashMap<>();
private final List<Listener> listeners = new CopyOnWriteArrayList<>();
private final Map<org.eclipse.aether.graph.DependencyNode, Node> allNodes =
Collections.synchronizedMap(new WeakHashMap<>());
private final Map<org.eclipse.aether.artifact.Artifact, Artifact> allArtifacts =
Collections.synchronizedMap(new WeakHashMap<>());
private final Map<org.eclipse.aether.repository.RemoteRepository, RemoteRepository> allRepositories =
Collections.synchronizedMap(new WeakHashMap<>());
private final Map<String, Project> allProjects = Collections.synchronizedMap(new WeakHashMap<>());
private final Map<org.eclipse.aether.graph.Dependency, Dependency> allDependencies =
Collections.synchronizedMap(new WeakHashMap<>());

public AbstractSession(
RepositorySystemSession session,
RepositorySystem repositorySystem,
List<RemoteRepository> repositories,
List<org.eclipse.aether.repository.RemoteRepository> resolverRepositories,
Lookup lookup) {
this.session = session;
this.repositorySystem = repositorySystem;
this.repositories = getRepositories(repositories, resolverRepositories);
this.lookup = lookup;
}

@Override
public RemoteRepository getRemoteRepository(org.eclipse.aether.repository.RemoteRepository repository) {
return allRepositories.computeIfAbsent(repository, DefaultRemoteRepository::new);
Expand Down Expand Up @@ -81,16 +123,6 @@ public Dependency getDependency(@Nonnull org.eclipse.aether.graph.Dependency dep
return allDependencies.computeIfAbsent(dependency, d -> new DefaultDependency(this, d));
}

@Override
public List<Project> getProjects(List<MavenProject> projects) {
return projects == null ? null : map(projects, this::getProject);
}

@Override
public Project getProject(MavenProject project) {
return allProjects.computeIfAbsent(project.getId(), id -> new DefaultProject(this, project));
}

@Override
public List<org.eclipse.aether.repository.RemoteRepository> toRepositories(List<RemoteRepository> repositories) {
return repositories == null ? null : map(repositories, this::toRepository);
Expand All @@ -117,21 +149,142 @@ public org.eclipse.aether.repository.LocalRepository toRepository(LocalRepositor
}

@Override
public List<ArtifactRepository> toArtifactRepositories(List<RemoteRepository> repositories) {
return repositories == null ? null : map(repositories, this::toArtifactRepository);
public List<org.eclipse.aether.graph.Dependency> toDependencies(
Collection<DependencyCoordinate> dependencies, boolean managed) {
return dependencies == null ? null : map(dependencies, d -> toDependency(d, managed));
}

protected List<RemoteRepository> getRepositories(
List<RemoteRepository> repositories,
List<org.eclipse.aether.repository.RemoteRepository> resolverRepositories) {
if (repositories != null) {
return repositories;
} else if (resolverRepositories != null) {
return map(resolverRepositories, this::getRemoteRepository);
} else {
throw new IllegalArgumentException("no remote repositories provided");
}
}

@Nonnull
@Override
public abstract ArtifactRepository toArtifactRepository(RemoteRepository repository);
public List<RemoteRepository> getRemoteRepositories() {
return Collections.unmodifiableList(repositories);
}

@Nonnull
@Override
public List<org.eclipse.aether.graph.Dependency> toDependencies(
Collection<DependencyCoordinate> dependencies, boolean managed) {
return dependencies == null ? null : map(dependencies, d -> toDependency(d, managed));
public SessionData getData() {
org.eclipse.aether.SessionData data = session.getData();
return new SessionData() {
@Override
public <T> void set(@Nonnull Key<T> key, @Nullable T value) {
data.set(key, value);
}

@Override
public <T> boolean replace(@Nonnull Key<T> key, @Nullable T oldValue, @Nullable T newValue) {
return data.set(key, oldValue, newValue);
}

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T get(@Nonnull Key<T> key) {
return (T) data.get(key);
}

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T computeIfAbsent(@Nonnull Key<T> key, @Nonnull Supplier<T> supplier) {
return (T) data.computeIfAbsent(key, (Supplier<Object>) supplier);
}
};
}

@Nonnull
@Override
public LocalRepository getLocalRepository() {
return new DefaultLocalRepository(session.getLocalRepository());
}

@Nonnull
@Override
public abstract org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency, boolean managed);
public Session withLocalRepository(@Nonnull LocalRepository localRepository) {
nonNull(localRepository, "localRepository");
if (session.getLocalRepository() != null
&& Objects.equals(session.getLocalRepository().getBasedir().toPath(), localRepository.getPath())) {
return this;
}
org.eclipse.aether.repository.LocalRepository repository = toRepository(localRepository);
org.eclipse.aether.repository.LocalRepositoryManager localRepositoryManager =
repositorySystem.newLocalRepositoryManager(session, repository);

RepositorySystemSession repoSession =
new DefaultRepositorySystemSession(session).setLocalRepositoryManager(localRepositoryManager);
return newSession(repoSession, repositories);
}

@Nonnull
@Override
public Session withRemoteRepositories(@Nonnull List<RemoteRepository> repositories) {
return newSession(session, repositories);
}

protected abstract Session newSession(RepositorySystemSession session, List<RemoteRepository> repositories);

@Nonnull
@Override
@SuppressWarnings("unchecked")
public <T extends Service> T getService(Class<T> clazz) throws NoSuchElementException {
T t = (T) services.computeIfAbsent(clazz, this::lookup);
if (t == null) {
throw new NoSuchElementException(clazz.getName());
}
return t;
}

private Service lookup(Class<? extends Service> c) {
try {
return lookup.lookup(c);
} catch (LookupException e) {
NoSuchElementException nsee = new NoSuchElementException(c.getName());
e.initCause(e);
throw nsee;
}
}

@Nonnull
public RepositorySystemSession getSession() {
return session;
}

@Nonnull
public RepositorySystem getRepositorySystem() {
return repositorySystem;
}

public org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency, boolean managed) {
org.eclipse.aether.graph.Dependency dep;
if (dependency instanceof DefaultDependencyCoordinate) {
dep = ((DefaultDependencyCoordinate) dependency).getDependency();
} else {
dep = new org.eclipse.aether.graph.Dependency(
new org.eclipse.aether.artifact.DefaultArtifact(
dependency.getGroupId(),
dependency.getArtifactId(),
dependency.getClassifier(),
dependency.getType().getExtension(),
dependency.getVersion().toString(),
null),
dependency.getScope().id());
}
if (!managed && "".equals(dep.getScope())) {
dep = dep.setScope(DependencyScope.COMPILE.id());
}
return dep;
}

@Override
public List<org.eclipse.aether.artifact.Artifact> toArtifacts(Collection<Artifact> artifacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.api.ArtifactCoordinate;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.ArtifactCoordinateFactory;
import org.apache.maven.api.services.ArtifactCoordinateFactoryRequest;
import org.eclipse.aether.artifact.ArtifactType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Named;
import javax.inject.Singleton;

import java.util.Collection;

import org.apache.maven.api.Artifact;
import org.apache.maven.api.RemoteRepository;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.ArtifactDeployer;
import org.apache.maven.api.services.ArtifactDeployerException;
import org.apache.maven.api.services.ArtifactDeployerRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.api.Artifact;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.ArtifactFactory;
import org.apache.maven.api.services.ArtifactFactoryRequest;
import org.eclipse.aether.artifact.ArtifactType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.services.ArtifactInstaller;
import org.apache.maven.api.services.ArtifactInstallerException;
import org.apache.maven.api.services.ArtifactInstallerRequest;
Expand Down
Loading

0 comments on commit ad581ba

Please sign in to comment.