From issues-return-165429-archive-asf-public=cust-asf.ponee.io@maven.apache.org Thu May 7 06:28:13 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5A34118062B for ; Thu, 7 May 2020 08:28:13 +0200 (CEST) Received: (qmail 59778 invoked by uid 500); 7 May 2020 06:28:12 -0000 Mailing-List: contact issues-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list issues@maven.apache.org Received: (qmail 59766 invoked by uid 99); 7 May 2020 06:28:12 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 May 2020 06:28:12 +0000 From: =?utf-8?q?GitBox?= To: issues@maven.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bmaven-dependency-plugin=5D_mthmulders_commented_on?= =?utf-8?q?_a_change_in_pull_request_=2350=3A_MDEP-648=3A_Add_location_of_li?= =?utf-8?q?sted_repository=2E?= Message-ID: <158883289255.26397.11302300613643181541.asfpy@gitbox.apache.org> Date: Thu, 07 May 2020 06:28:12 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit References: In-Reply-To: mthmulders commented on a change in pull request #50: URL: https://github.com/apache/maven-dependency-plugin/pull/50#discussion_r421267390 ########## File path: src/main/java/org/apache/maven/plugins/dependency/resolvers/ListRepositoriesMojo.java ########## @@ -55,22 +110,276 @@ protected void doExecute() throws MojoExecutionException { + + for ( ArtifactRepository artifactRepository : remoteRepositories ) + { + verbose( "Maven remote repositories: " + repositoryAsString( artifactRepository ) ); + } + + final Set repositories = new HashSet<>(); + final Set artifacts = new HashSet<>(); + + DependencyVisitor visitor = new DependencyVisitor() + { + @Override + public boolean visitEnter( DependencyNode dependencyNode ) + { + repositories.addAll( dependencyNode.getRemoteRepositories() ); + artifacts.add( dependencyNode.getArtifact() ); + return true; + } + + @Override + public boolean visitLeave( DependencyNode dependencyNode ) + { + return true; + } + }; + try { - CollectorResult collectResult = - dependencyCollector.collectDependencies( session.getProjectBuildingRequest(), getProject().getModel() ); + ProjectBuildingRequest projectBuildingRequest = session.getProjectBuildingRequest(); + + CollectResult collectResult = + dependencyCollector.collectDependencies( projectBuildingRequest, getProject().getModel() ); + + for ( Exception e : collectResult.getExceptions() ) + { + throw new MojoExecutionException( "Collect dependencies failed", e ); + } + + collectResult.getRoot().accept( visitor ); + + verbose( "Artifacts used by the build of " + collectResult.getRoot().getArtifact() + ":" ); + for ( Artifact artifact : artifacts ) + { + verbose( " " + artifact.toString() ); + } this.getLog().info( "Repositories used by this build:" ); + for ( ArtifactRepository repo : repositories ) + { + if ( isVerbose() ) + { + Set locations = new HashSet(); + for ( Mirror mirror : settings.getMirrors() ) + { + if ( mirror.getId().equals( repo.getId() ) + && ( mirror.getUrl().equals( repo.getUrl() ) ) ) + { + locations.add( "Maven settings (user/global)" ); + } + } + + Artifact projectArtifact = getProject().getArtifact(); + MavenProject project = getMavenProject( ArtifactUtils.key( projectArtifact ) ); + traversePom( repo, projectArtifact, project, locations ); + + for ( Artifact artifact : artifacts ) + { + MavenProject artifactProject = getMavenProject( ArtifactUtils.key( artifact ) ); + traversePom( repo, artifact, artifactProject, locations ); + } + writeRepository( repo, locations ); + } + else + { + this.getLog().info( repo.toString() ); + } + } + } + catch ( DependencyCollectionException e ) + { + throw new MojoExecutionException( "Unable to collect", e ); + } + } + + private void writeRepository( ArtifactRepository artifactRepository, Set locations ) + { + StringBuilder sb = new StringBuilder( 256 ); + sb.append( artifactRepository.toString() ); + for ( String location : locations ) + { + sb.append( " location: " ).append( location ).append( System.lineSeparator() ); + } + this.getLog().info( sb.toString() ); + } + + /** + * Parses the given String into GAV artifact coordinate information, adding the given type. + * + * @param artifactString should respect the format groupId:artifactId[:version] + * @param type The extension for the artifact, must not be null. + * @return the Artifact object for the artifactString parameter. + * @throws MojoExecutionException if the artifactString doesn't respect the format. + */ + private ArtifactCoordinate getArtifactCoordinate( String artifactString, String type ) + throws MojoExecutionException + { + if ( org.codehaus.plexus.util.StringUtils.isEmpty( artifactString ) ) + { + throw new IllegalArgumentException( "artifact parameter could not be empty" ); + } + + String groupId; // required + String artifactId; // required + String version; // optional + + String[] artifactParts = artifactString.split( ":" ); + switch ( artifactParts.length ) + { + case 2: + groupId = artifactParts[0]; + artifactId = artifactParts[1]; + version = Artifact.LATEST_VERSION; + break; + case 3: + groupId = artifactParts[0]; + artifactId = artifactParts[1]; + version = artifactParts[2]; + break; + default: + throw new MojoExecutionException( "The artifact parameter '" + artifactString + + "' should be conform to: " + "'groupId:artifactId[:version]'." ); + } + return getArtifactCoordinate( groupId, artifactId, version, type ); + } + + private ArtifactCoordinate getArtifactCoordinate( String groupId, String artifactId, String version, String type ) + { + DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate(); + coordinate.setGroupId( groupId ); + coordinate.setArtifactId( artifactId ); + coordinate.setVersion( version ); + coordinate.setExtension( type ); + return coordinate; + } + + /** + * Retrieves the Maven Project associated with the given artifact String, in the form of + * groupId:artifactId[:version]. This resolves the POM artifact at those coordinates and then builds + * the Maven project from it. + * + * @param artifactString Coordinates of the Maven project to get. + * @return New Maven project. + * @throws MojoExecutionException If there was an error while getting the Maven project. + */ + private MavenProject getMavenProject( String artifactString ) + throws MojoExecutionException + { + ArtifactCoordinate coordinate = getArtifactCoordinate( artifactString, "pom" ); + try + { + ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() ); + pbr.setRemoteRepositories( remoteRepositories ); + pbr.setProject( null ); + pbr.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); + pbr.setResolveDependencies( false ); + Artifact artifact = artifactResolver.resolveArtifact( pbr, coordinate ).getArtifact(); + return projectBuilder.build( artifact.getFile(), pbr ).getProject(); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifactString + + "'. Verify the artifact parameter.", e ); + } + } + + private void traversePom( ArtifactRepository artifactRepository, + Artifact artifact, MavenProject mavenProject, Set locations ) + throws MojoExecutionException + { + getLog().debug( "Looking for locations of repository " + repositoryAsString( artifactRepository ) + + " for " + artifact ); + if ( mavenProject != null ) + { + for ( Repository repository : mavenProject.getOriginalModel().getRepositories() ) + { + getLog().debug( "Found repository: " + repositoryAsString( repository ) + + " @ " + artifact + ":" + mavenProject.getOriginalModel().getPomFile() ); + if ( isRepositoryEqual( repository, artifactRepository ) ) + { + locations.add( mavenProject.getModel().getPomFile().toString() ); + } + } + + traverseParentPom( artifactRepository, mavenProject, locations ); + } + else + { + throw new MojoExecutionException( "No POM for the artifact '" + artifact + "'" ); + } + return; + } - for ( ArtifactRepository repo : collectResult.getRemoteRepositories() ) + private void traverseParentPom( ArtifactRepository artifactRepository, + MavenProject mavenProject, Set locations ) + throws MojoExecutionException + { + MavenProject parent = mavenProject.getParent(); + if ( parent != null ) + { + Model originalModel = parent.getOriginalModel(); + if ( originalModel.getRepositories().size() != 0 + || originalModel.getPluginRepositories().size() != 0 ) { - this.getLog().info( repo.toString() ); + String artifactKey = + ArtifactUtils.key( parent.getGroupId(), parent.getArtifactId(), parent.getVersion() ); + MavenProject parentPom = getMavenProject( artifactKey ); + + for ( Repository repository : originalModel.getRepositories() ) + { + getLog().debug( "Found parent repository " + repositoryAsString( repository ) + + " @ " + parentPom.getArtifact() + ":" + parentPom.getFile() ); + if ( isRepositoryEqual( repository, artifactRepository ) ) + { + locations.add( parentPom.getFile().toString() ); + } + } } + traverseParentPom( artifactRepository, parent, locations ); } - catch ( DependencyCollectorException e ) + return; + } + + private String repositoryAsString( Repository repository ) + { + StringBuilder sb = new StringBuilder( 32 ); + sb.append( repository.getId() ); + sb.append( " (" ); + sb.append( repository.getUrl() ); + sb.append( ")" ); + return sb.toString(); + } + + private String repositoryAsString( ArtifactRepository repository ) + { + StringBuilder sb = new StringBuilder( 32 ); + sb.append( repository.getId() ); + sb.append( " (" ); + sb.append( repository.getUrl() ); + sb.append( ")" ); + return sb.toString(); + } + + private boolean isVerbose() + { + return ( verbose || getLog().isDebugEnabled() ); Review comment: It's a bit strange that `isVerbose` checks for `log.isDebugEnabled()`, while there is a `verbose` flag already which is triggered (according to its Javadoc) by the `-X` flag in the CLI. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org