-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[MNG-6869] New flag to verify Maven installation status #995
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
Open
mthmulders
wants to merge
44
commits into
apache:master
Choose a base branch
from
infosupport:mng-6869-new-flag-to-verify-maven-status
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
904caa4
[MNG-6869] Initial idea
Giovds 2dab934
[MNG-6869] Further brainstorming
mthmulders 1082a0d
[MNG-6869] Refactor local variable name
Giovds 8d46d92
[MNG-6969] WIP commit
Giovds 9aaf62f
[MNG-6869] Correct way to find local repo path
mthmulders 57c7020
[MNG-6869] Use java.nio instead of java.io
Giovds 55f35fd
[MNG-6869] Use java.nio instead of java.io
mthmulders fdcce30
[MNG-6869] Implement verification of artifact resolution (WIP)
mthmulders e4ec517
[MNG-6869] Test artifact resolution with temporary local repository
mthmulders 77526f5
[MNG-6869] Setup checks for remote connections
Giovds fbbcc20
[MNG-6869] Formatting
mthmulders ba9354a
[MNG-6869] Refactor: rename method
mthmulders 85b0907
[MNG-6869] Formatting
mthmulders 12a6b4b
[MNG-6869] Verify connection to remote repositories
mthmulders 140a63c
[MNG-6869] Reformat with Spotless
mthmulders 7afb18e
[MNG-6869] Add todo to fix local repository issue
Giovds ca4cb33
[MNG-6869] Narrow declaration of thrown exception
mthmulders 0764fd0
[MNG-6869] Refine detection of possible connection issues
mthmulders 30c9649
[MNG-6869] Use local repository correctly
Giovds d134358
[MNG-6869] Clean up temp file after usage
Giovds 6ba97e6
[MNG-6869] Include local repository path in issue text
mthmulders 79338ae
[MNG-6869] Mention local repo check has been completed
mthmulders b470aca
[MNG-6869] Output detected issues prefixed with a number
mthmulders 8629a8a
[MNG-6869] Properly close streeam
mthmulders 7fcc380
[MNG-6869] Make logger a static constant
mthmulders 2b01442
[MNG-6869] Make it explicit we're populating instance variables
mthmulders 95c52f2
[MNG-6869] Reorder instance variables
mthmulders d0f3b29
[MNG-6869] Mark method arguments as final
mthmulders 58e2f46
[MNG-6869] Store temporary local repo as Path
mthmulders f67f7c3
[MNG-6869] Rename logger to LOGGER
mthmulders 27a7e0a
[MNG-6869] Checkstyle: redundant modifier
mthmulders d2416a2
[MNG-6869] Minor review points
mthmulders 2a9ce5e
[MNG-6869] Remove commented code
Giovds ed191ad
[MNG-6869] Use exception classification of Transporter
Giovds 6adddbd
[MNG-6869] Make formatException signature non-optional
Giovds 9443a2a
[MNG-6869] Remove unnecessary comment
Giovds 41177b3
[MNG-6869] Remove unused DI fields
Giovds fc2e7f7
[MNG-6869] Make no assumptions about the existence of Maven Central
mthmulders 2114cda
[MNG-6869] Simplify
mthmulders 853819c
[MNG-6869] Prevent possible ClassCastException
mthmulders f39aec2
[MNG-6869] Revert unrelated change
mthmulders 6c4348d
Merge branch 'master' into mng-6869-new-flag-to-verify-maven-status
Giovds eaf2421
[MNG-6869] Make it work with recent changes
Giovds 63006df
[MNG-6869] Make use of try with resources
Giovds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
maven-embedder/src/main/java/org/apache/maven/cli/MavenStatusCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /* | ||
| * 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.cli; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.apache.maven.api.ArtifactCoordinate; | ||
| import org.apache.maven.api.Session; | ||
| import org.apache.maven.api.services.ArtifactResolver; | ||
| import org.apache.maven.api.services.ArtifactResolverException; | ||
| import org.apache.maven.api.services.ArtifactResolverResult; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
| import org.apache.maven.bridge.MavenRepositorySystem; | ||
| import org.apache.maven.execution.DefaultMavenExecutionResult; | ||
| import org.apache.maven.execution.MavenExecutionRequest; | ||
| import org.apache.maven.execution.MavenExecutionRequestPopulationException; | ||
| import org.apache.maven.execution.MavenExecutionRequestPopulator; | ||
| import org.apache.maven.execution.MavenSession; | ||
| import org.apache.maven.internal.impl.DefaultArtifactCoordinate; | ||
| import org.apache.maven.internal.impl.DefaultSessionFactory; | ||
| import org.apache.maven.internal.impl.InternalMavenSession; | ||
| import org.apache.maven.internal.impl.InternalSession; | ||
| import org.apache.maven.resolver.RepositorySystemSessionFactory; | ||
| import org.apache.maven.session.scope.internal.SessionScope; | ||
| import org.codehaus.plexus.PlexusContainer; | ||
| import org.codehaus.plexus.component.repository.exception.ComponentLookupException; | ||
| import org.eclipse.aether.RepositorySystemSession; | ||
| import org.eclipse.aether.artifact.Artifact; | ||
| import org.eclipse.aether.artifact.DefaultArtifact; | ||
| import org.eclipse.aether.resolution.ArtifactResolutionException; | ||
| import org.eclipse.aether.resolution.ArtifactResult; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class MavenStatusCommand { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(MavenStatusCommand.class); | ||
|
|
||
| /** | ||
| * In order to verify artifacts can be downloaded from the remote repositories we want to resolve an actual | ||
| * artifact. The Apache Maven artifact was chosen as it eventually, be it by proxy, mirror or directly, will be | ||
| * gathered from the central repository. The version is chosen arbitrarily since any listed should work. | ||
| */ | ||
| public static final Artifact APACHE_MAVEN_ARTIFACT = | ||
| new DefaultArtifact("org.apache.maven", "apache-maven", null, "pom", "3.8.6"); | ||
|
|
||
| private final MavenExecutionRequestPopulator mavenExecutionRequestPopulator; | ||
| private final ArtifactResolver artifactResolver; | ||
| private final RemoteRepositoryConnectionVerifier remoteRepositoryConnectionVerifier; | ||
| private final DefaultSessionFactory defaultSessionFactory; | ||
| private final RepositorySystemSessionFactory repoSession; | ||
| private final MavenRepositorySystem repositorySystem; | ||
| private final SessionScope sessionScope; | ||
| private Path tempLocalRepository; | ||
|
|
||
| public MavenStatusCommand(final PlexusContainer container) throws ComponentLookupException { | ||
| this.remoteRepositoryConnectionVerifier = new RemoteRepositoryConnectionVerifier(container); | ||
| this.mavenExecutionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class); | ||
| this.artifactResolver = container.lookup(ArtifactResolver.class); | ||
| this.defaultSessionFactory = container.lookup(DefaultSessionFactory.class); | ||
| this.repoSession = container.lookup(RepositorySystemSessionFactory.class); | ||
| this.sessionScope = container.lookup(SessionScope.class); | ||
| this.repositorySystem = container.lookup(MavenRepositorySystem.class); | ||
| } | ||
|
|
||
| public List<String> verify(final MavenExecutionRequest cliRequest) throws MavenExecutionRequestPopulationException { | ||
| final MavenExecutionRequest mavenExecutionRequest = mavenExecutionRequestPopulator.populateDefaults(cliRequest); | ||
|
|
||
| final ArtifactRepository localRepository = cliRequest.getLocalRepository(); | ||
|
|
||
| final List<String> localRepositoryIssues = | ||
| verifyLocalRepository(Paths.get(URI.create(localRepository.getUrl()))); | ||
|
|
||
| // We overwrite the local repository with a temporary directory to avoid using a cached version of the artifact. | ||
| setTemporaryLocalRepositoryPathOnRequest(cliRequest); | ||
|
|
||
| final List<String> remoteRepositoryIssues = | ||
| verifyRemoteRepositoryConnections(cliRequest.getRemoteRepositories(), mavenExecutionRequest); | ||
| final List<String> artifactResolutionIssues = verifyArtifactResolution(mavenExecutionRequest); | ||
|
|
||
| cleanupTempFiles(); | ||
|
|
||
| // Collect all issues into a single list | ||
| return Stream.of(localRepositoryIssues, remoteRepositoryIssues, artifactResolutionIssues) | ||
| .flatMap(Collection::stream) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private void cleanupTempFiles() { | ||
| if (tempLocalRepository != null) { | ||
| try (Stream<Path> files = Files.walk(tempLocalRepository)) { | ||
| files.sorted(Comparator.reverseOrder()) // Sort in reverse order so that directories are deleted last | ||
| .map(Path::toFile) | ||
| .forEach(File::delete); | ||
| } catch (IOException ioe) { | ||
| LOGGER.debug("Failed to delete temporary local repository", ioe); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void setTemporaryLocalRepositoryPathOnRequest(final MavenExecutionRequest request) { | ||
| try { | ||
| tempLocalRepository = Files.createTempDirectory("mvn-status").toAbsolutePath(); | ||
| request.setLocalRepositoryPath(tempLocalRepository.toString()); | ||
| request.setLocalRepository(repositorySystem.createLocalRepository(request, tempLocalRepository.toFile())); | ||
| } catch (Exception ex) { | ||
| LOGGER.debug("Could not create temporary local repository", ex); | ||
| LOGGER.warn("Artifact resolution test is less accurate as it may use earlier resolution results."); | ||
| } | ||
| } | ||
|
|
||
| private List<String> verifyRemoteRepositoryConnections( | ||
| final List<ArtifactRepository> remoteRepositories, final MavenExecutionRequest mavenExecutionRequest) { | ||
| final List<String> issues = new ArrayList<>(); | ||
|
|
||
| for (ArtifactRepository remoteRepository : remoteRepositories) { | ||
| try (RepositorySystemSession.CloseableSession repositorySession = repoSession | ||
| .newRepositorySessionBuilder(mavenExecutionRequest) | ||
| .build()) { | ||
| remoteRepositoryConnectionVerifier | ||
| .verifyConnectionToRemoteRepository(repositorySession, remoteRepository) | ||
| .ifPresent(issues::add); | ||
| } | ||
| } | ||
|
|
||
| return issues; | ||
| } | ||
|
|
||
| private List<String> verifyArtifactResolution(final MavenExecutionRequest mavenExecutionRequest) { | ||
| this.sessionScope.enter(); | ||
| try (RepositorySystemSession.CloseableSession repoSession = this.repoSession | ||
| .newRepositorySessionBuilder(mavenExecutionRequest) | ||
| .build()) { | ||
| final Session session = this.defaultSessionFactory.newSession( | ||
| new MavenSession(repoSession, mavenExecutionRequest, new DefaultMavenExecutionResult())); | ||
| InternalMavenSession internalSession = InternalMavenSession.from(session); | ||
| sessionScope.seed(InternalMavenSession.class, internalSession); | ||
| ArtifactCoordinate artifactCoordinate = | ||
| new DefaultArtifactCoordinate(InternalSession.from(session), APACHE_MAVEN_ARTIFACT); | ||
| ArtifactResolverResult resolverResult = | ||
| artifactResolver.resolve(session, Collections.singleton(artifactCoordinate)); | ||
| resolverResult | ||
| .getArtifacts() | ||
| .forEach((key, value) -> LOGGER.debug("Successfully resolved {} to {}", key, value)); | ||
|
|
||
| return Collections.emptyList(); | ||
| } catch (ArtifactResolverException are) { | ||
| return extractIssuesFromArtifactResolverException(are); | ||
| } finally { | ||
| this.sessionScope.exit(); | ||
| LOGGER.info("Artifact resolution check completed"); | ||
| } | ||
| } | ||
|
|
||
| private List<String> extractIssuesFromArtifactResolverException(final Exception exception) { | ||
| final boolean isArtifactResolutionException = exception.getCause() instanceof ArtifactResolutionException; | ||
| if (isArtifactResolutionException) { | ||
| final ArtifactResolutionException are = (ArtifactResolutionException) exception.getCause(); | ||
| return are.getResults().stream() | ||
| .map(ArtifactResult::getExceptions) | ||
| .flatMap(List::stream) | ||
| .map(Throwable::getMessage) | ||
| .collect(Collectors.toList()); | ||
| } else { | ||
| return Collections.singletonList(exception.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private List<String> verifyLocalRepository(final Path localRepositoryPath) { | ||
| final List<String> issues = new ArrayList<>(); | ||
|
|
||
| if (!Files.isDirectory(localRepositoryPath)) { | ||
| issues.add(String.format("Local repository path '%s' is not a directory.", localRepositoryPath)); | ||
| } | ||
|
|
||
| if (!Files.isReadable(localRepositoryPath)) { | ||
| issues.add(String.format("No read permissions on local repository '%s'.", localRepositoryPath)); | ||
| } | ||
|
|
||
| if (!Files.isWritable(localRepositoryPath)) { | ||
| issues.add(String.format("No write permissions on local repository '%s'.", localRepositoryPath)); | ||
| } | ||
|
|
||
mthmulders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOGGER.info("Local repository setup check completed"); | ||
| return issues; | ||
| } | ||
| } | ||
94 changes: 94 additions & 0 deletions
94
maven-embedder/src/main/java/org/apache/maven/cli/RemoteRepositoryConnectionVerifier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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.cli; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.Optional; | ||
|
|
||
| import org.apache.maven.RepositoryUtils; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
| import org.codehaus.plexus.PlexusContainer; | ||
| import org.codehaus.plexus.component.repository.exception.ComponentLookupException; | ||
| import org.eclipse.aether.RepositorySystemSession; | ||
| import org.eclipse.aether.repository.RemoteRepository; | ||
| import org.eclipse.aether.spi.connector.transport.GetTask; | ||
| import org.eclipse.aether.spi.connector.transport.Transporter; | ||
| import org.eclipse.aether.spi.connector.transport.TransporterProvider; | ||
| import org.eclipse.aether.transfer.NoTransporterException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Helper class to verify connection to a remote repository. | ||
| */ | ||
| public class RemoteRepositoryConnectionVerifier { | ||
| private final Logger logger; | ||
| private final TransporterProvider transporterProvider; | ||
|
|
||
| public RemoteRepositoryConnectionVerifier(final PlexusContainer container) throws ComponentLookupException { | ||
| this.logger = LoggerFactory.getILoggerFactory().getLogger(RemoteRepositoryConnectionVerifier.class.getName()); | ||
| this.transporterProvider = container.lookup(TransporterProvider.class); | ||
| } | ||
|
|
||
| public Optional<String> verifyConnectionToRemoteRepository( | ||
| final RepositorySystemSession session, final ArtifactRepository artifactRepository) { | ||
| final RemoteRepository repository = RepositoryUtils.toRepo(artifactRepository); | ||
|
|
||
| try { | ||
| final Transporter transporter = transporterProvider.newTransporter(session, repository); | ||
| return verifyConnectionUsingTransport(transporter, repository); | ||
| } catch (final NoTransporterException nte) { | ||
| final String message = String.format( | ||
| "There is no compatible transport for remote repository '%s' with location '%s'", | ||
| repository.getId(), repository.getUrl()); | ||
| return Optional.of(message); | ||
| } | ||
| } | ||
|
|
||
| private Optional<String> verifyConnectionUsingTransport( | ||
| final Transporter transporter, final RemoteRepository remoteRepository) { | ||
| try { | ||
| final GetTask task = new GetTask(URI.create("")); | ||
| transporter.get(task); | ||
| // We could connect, but uncertain to what. Could be the repository, could be a valid web page. | ||
| logger.info( | ||
| "Connection check for repository '{}' at '{}' completed", | ||
| remoteRepository.getId(), | ||
| remoteRepository.getUrl()); | ||
| return Optional.empty(); | ||
| } catch (final Exception e) { | ||
| final int errorOrArtifactNotFound = transporter.classify(e); | ||
| if (Transporter.ERROR_NOT_FOUND == errorOrArtifactNotFound) { | ||
| // No-op since we could connect to the repository | ||
| // However we do not know what should or shouldn't be present | ||
| return Optional.empty(); | ||
| } | ||
| // In this case it is Transporter.ERROR_OTHER | ||
| return Optional.of(formatException(remoteRepository, e)); | ||
| } | ||
| } | ||
|
|
||
| private String formatException(final RemoteRepository remoteRepository, final Exception e) { | ||
| final String repositoryId = remoteRepository.getId(); | ||
| final String repositoryUrl = remoteRepository.getUrl(); | ||
| final String repository = String.format("%s [%s]", repositoryId, repositoryUrl); | ||
|
|
||
| return String.format("Connection to %s not possible. Cause: %s", repository, e.getMessage()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this is 1 step too much. Is this the only way to verify if a remote repository is accessible? If you need to use an explicit file instead of a directory, is it possible to use http HEAD ?
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.
Just to verify: are you referring to the fact that this code actually downloads a file (e.g., it could've been an HTTP HEAD request), or to the fact that the
--statusverifies if artifact resolution works?In the case of the former: is a remote repository required to support an HTTP HEAD request? Could we rely on the fact that if HTTP HEAD is OK, HTTP GET will be OK, too?
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.
It depends on what you define as "artifact resolution works". I'm not interested if the implementation is correct. I wish there's a way to only confirm that the connection works. It is about returning questions on StackOverflow that say Maven can't download things, but it is just that it cannot reach a repository. Possible rootcauses are missing proxies (or misconfigured) and misconfigured mirrors. Maybe @michael-o or @cstamas can think of a low-level reliable way to conform this.
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.
From my experience this is not testing a random artifact which works but more enabling the aether logs which help, ultimately logging the request to be able to replay it with curl to do the check. Rest is random tests which can easily be proven false positive/negative due to proxies (correct) configuration.
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.
The goal of this code is not to explain what is wrong and how the user should fix it. I agree with @rmannibucau that the Aether logs are more suitable for that. What this code wants to do is tell the user they have a problem, without even building a project.
Ideally, in the case of the StackOverflow questions that @rfscholte refers to, we could say "please share the output of
mvn --status". Their output might look like this for example:Even though this doesn't tell the user the solution, it points them in a few directions where they could look. It's a lot more compact than the complete Aether logs - which the user could still inspect, in case this message doesn't point them in the right direction already.