-
Notifications
You must be signed in to change notification settings - Fork 197
Remove maven-compat from GetMojo #1677
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /* | ||
| * 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.utils; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.maven.RepositoryUtils; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
|
Check warning on line 26 in src/main/java/org/apache/maven/plugins/dependency/utils/RepositorySessionInjector.java
|
||
| import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; | ||
|
Check warning on line 27 in src/main/java/org/apache/maven/plugins/dependency/utils/RepositorySessionInjector.java
|
||
| import org.apache.maven.artifact.repository.Authentication; | ||
| import org.apache.maven.artifact.repository.MavenArtifactRepository; | ||
| import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; | ||
|
Check warning on line 30 in src/main/java/org/apache/maven/plugins/dependency/utils/RepositorySessionInjector.java
|
||
| import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2; | ||
| import org.apache.maven.repository.Proxy; | ||
| import org.eclipse.aether.RepositorySystemSession; | ||
| import org.eclipse.aether.repository.AuthenticationContext; | ||
| import org.eclipse.aether.repository.AuthenticationSelector; | ||
| import org.eclipse.aether.repository.MirrorSelector; | ||
| import org.eclipse.aether.repository.ProxySelector; | ||
| import org.eclipse.aether.repository.RemoteRepository; | ||
|
|
||
| /** | ||
| * Applies the mirror, proxy and authentication configuration of a {@link RepositorySystemSession} to repositories | ||
| * that were built by hand rather than obtained from the project, so that they can be used for resolution. | ||
| * <p> | ||
| * This is the work {@code org.apache.maven.bridge.MavenRepositorySystem} does in its session-based | ||
| * {@code injectMirror}/{@code injectProxy}/{@code injectAuthentication} methods, reimplemented here because | ||
| * {@code org.apache.maven.bridge} is not one of the packages maven-core exports to plugin class realms — a plugin | ||
| * that references it compiles and unit-tests cleanly and then fails at runtime with | ||
| * {@code NoClassDefFoundError: org/apache/maven/bridge/MavenRepositorySystem}. Everything used below is in a package | ||
| * maven-core does export: {@code org.apache.maven} (for {@link RepositoryUtils}), {@code org.apache.maven.artifact}, | ||
| * {@code org.apache.maven.repository} and {@code org.eclipse.aether.repository}. | ||
| * <p> | ||
| * The session's selectors are populated by Maven from the mirrors, proxies and servers of | ||
| * <code>settings.xml</code>, with the servers and proxies already decrypted, and they are the same selectors | ||
| * Resolver consults when it performs the transfer. | ||
| */ | ||
| public class RepositorySessionInjector { | ||
|
|
||
| private final Map<String, ArtifactRepositoryLayout> repositoryLayouts; | ||
|
|
||
| public RepositorySessionInjector(Map<String, ArtifactRepositoryLayout> repositoryLayouts) { | ||
| this.repositoryLayouts = repositoryLayouts; | ||
| } | ||
|
|
||
| /** | ||
| * Applies the session's mirror, then proxy, then authentication configuration to each repository, in that order: | ||
| * mirroring rewrites the id and URL a repository is known by, and the proxy and the credentials are selected for | ||
| * the mirror rather than for the repository it replaced. | ||
| * | ||
| * @param session the repository session, may be {@code null} | ||
| * @param repositories the repositories to modify in place, may be {@code null} | ||
| */ | ||
| public void inject(RepositorySystemSession session, List<ArtifactRepository> repositories) { | ||
| if (session == null || repositories == null) { | ||
| return; | ||
| } | ||
|
|
||
| for (ArtifactRepository repository : repositories) { | ||
| injectMirror(session, repository); | ||
| repository.setProxy(getProxy(session, repository)); | ||
| repository.setAuthentication(getAuthentication(session, repository)); | ||
| } | ||
| } | ||
|
|
||
| private void injectMirror(RepositorySystemSession session, ArtifactRepository repository) { | ||
| MirrorSelector selector = session.getMirrorSelector(); | ||
| if (selector == null) { | ||
| return; | ||
| } | ||
|
|
||
| RemoteRepository mirror = selector.getMirror(RepositoryUtils.toRepo(repository)); | ||
| if (mirror == null) { | ||
| return; | ||
| } | ||
|
|
||
| repository.setMirroredRepositories(Collections.singletonList(createArtifactRepository( | ||
| repository.getId(), | ||
| repository.getUrl(), | ||
| repository.getLayout(), | ||
| repository.getSnapshots(), | ||
| repository.getReleases()))); | ||
|
|
||
| repository.setId(mirror.getId()); | ||
| repository.setUrl(mirror.getUrl()); | ||
|
|
||
| String layoutId = mirror.getContentType(); | ||
| if (layoutId != null && !layoutId.isEmpty()) { | ||
| repository.setLayout(getLayout(layoutId)); | ||
| } | ||
|
|
||
| repository.setBlocked(mirror.isBlocked()); | ||
| } | ||
|
|
||
| /** | ||
| * Same as {@code MavenRepositorySystem.createArtifactRepository}, including its substitution of default policies | ||
| * for null ones — {@link MavenArtifactRepository} stores null and fails later at whichever call site validates. | ||
| */ | ||
| private static ArtifactRepository createArtifactRepository( | ||
| String id, | ||
| String url, | ||
| ArtifactRepositoryLayout layout, | ||
| ArtifactRepositoryPolicy snapshots, | ||
| ArtifactRepositoryPolicy releases) { | ||
| ArtifactRepositoryPolicy snapshotPolicy = snapshots != null ? snapshots : new ArtifactRepositoryPolicy(); | ||
| ArtifactRepositoryPolicy releasePolicy = releases != null ? releases : new ArtifactRepositoryPolicy(); | ||
|
|
||
| if (layout instanceof ArtifactRepositoryLayout2) { | ||
| return ((ArtifactRepositoryLayout2) layout) | ||
| .newMavenArtifactRepository(id, url, snapshotPolicy, releasePolicy); | ||
| } | ||
| return new MavenArtifactRepository(id, url, layout, snapshotPolicy, releasePolicy); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a mirror's layout id. Maven core wraps an unknown id in a layout that delegates to the default one; | ||
| * falling back to the default layout outright computes the same paths. | ||
| */ | ||
| private ArtifactRepositoryLayout getLayout(String layoutId) { | ||
| ArtifactRepositoryLayout layout = repositoryLayouts.get(layoutId); | ||
| return layout != null ? layout : repositoryLayouts.get("default"); | ||
| } | ||
|
|
||
| private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) { | ||
| ProxySelector selector = session.getProxySelector(); | ||
| if (selector == null) { | ||
| return null; | ||
| } | ||
|
|
||
| RemoteRepository repo = RepositoryUtils.toRepo(repository); | ||
| org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo); | ||
| if (proxy == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Proxy result = new Proxy(); | ||
| result.setHost(proxy.getHost()); | ||
| result.setProtocol(proxy.getType()); | ||
| result.setPort(proxy.getPort()); | ||
|
|
||
| if (proxy.getAuthentication() != null) { | ||
| repo = new RemoteRepository.Builder(repo).setProxy(proxy).build(); | ||
| try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) { | ||
| result.setUserName(authCtx.get(AuthenticationContext.USERNAME)); | ||
| result.setPassword(authCtx.get(AuthenticationContext.PASSWORD)); | ||
| result.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN)); | ||
| result.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION)); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) { | ||
| AuthenticationSelector selector = session.getAuthenticationSelector(); | ||
| if (selector == null) { | ||
| return null; | ||
| } | ||
|
|
||
| RemoteRepository repo = RepositoryUtils.toRepo(repository); | ||
| org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo); | ||
| if (auth == null) { | ||
| return null; | ||
| } | ||
|
|
||
| repo = new RemoteRepository.Builder(repo).setAuthentication(auth).build(); | ||
| try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) { | ||
| Authentication result = new Authentication( | ||
| authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD)); | ||
| result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH)); | ||
| result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE)); | ||
| return result; | ||
|
Comment on lines
+186
to
+190
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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.artifact.repository.metadata; | ||
|
|
||
| import org.apache.maven.artifact.Artifact; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
|
|
||
| /** | ||
| * Metadata for the artifact version directory of the repository. | ||
| * <p> | ||
| * Class copied verbatim from maven-compat so that the maven-compat dependency can be dropped. It is used by the | ||
| * unit tests only, never by the plugin itself, and it is a plain data holder over | ||
| * {@link AbstractRepositoryMetadata} (which lives in maven-core), so copying it is enough — there is no component | ||
| * implementation left behind in maven-compat. Same technique as maven-plugin-plugin used for | ||
| * {@code GroupRepositoryMetadata} in MPLUGIN-384. | ||
| * | ||
| * @author <a href="mailto:brett@apache.org">Brett Porter</a> | ||
| */ | ||
| public class SnapshotArtifactRepositoryMetadata extends AbstractRepositoryMetadata { | ||
| private final Artifact artifact; | ||
|
|
||
| public SnapshotArtifactRepositoryMetadata(Artifact artifact) { | ||
| super(createMetadata(artifact, null)); | ||
| this.artifact = artifact; | ||
| } | ||
|
|
||
| public SnapshotArtifactRepositoryMetadata(Artifact artifact, Snapshot snapshot) { | ||
| super(createMetadata(artifact, createVersioning(snapshot))); | ||
| this.artifact = artifact; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean storedInGroupDirectory() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean storedInArtifactVersionDirectory() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String getGroupId() { | ||
| return artifact.getGroupId(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getArtifactId() { | ||
| return artifact.getArtifactId(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getBaseVersion() { | ||
| return artifact.getBaseVersion(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object getKey() { | ||
| return "snapshot " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getBaseVersion(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSnapshot() { | ||
| return artifact.isSnapshot(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getNature() { | ||
| return isSnapshot() ? SNAPSHOT : RELEASE; | ||
| } | ||
|
|
||
| @Override | ||
| public ArtifactRepository getRepository() { | ||
| return artifact.getRepository(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setRepository(ArtifactRepository remoteRepository) { | ||
| artifact.setRepository(remoteRepository); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be only one use of this class. Unless you intend this to be a general utility for external use, consider moving it into the package where it's used and making it non-public