View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo.dependencies;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.ArtifactUtils;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.project.DefaultProjectBuildingRequest;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.ProjectBuilder;
32  import org.apache.maven.project.ProjectBuildingException;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  import org.apache.maven.repository.RepositorySystem;
35  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
36  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
37  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
38  
39  /**
40   * Utility methods to play with repository.
41   *
42   * @version $Id$
43   * @since 2.1
44   */
45  public class RepositoryUtils {
46      private final Log log;
47  
48      private final ProjectBuilder projectBuilder;
49  
50      private final RepositorySystem repositorySystem;
51  
52      private final List<ArtifactRepository> remoteRepositories;
53  
54      private final List<ArtifactRepository> pluginRepositories;
55  
56      private final ArtifactResolver resolver;
57  
58      private final ProjectBuildingRequest buildingRequest;
59  
60      /**
61       * @param log {@link Log}
62       * @param projectBuilder {@link ProjectBuilder}
63       * @param repositorySystem {@link RepositorySystem}
64       * @param resolver {@link ArtifactResolver}
65       * @param remoteRepositories {@link ArtifactRepository}
66       * @param pluginRepositories {@link ArtifactRepository}
67       * @param buildingRequest {@link ProjectBuildingRequest}
68       * @param repositoryMetadataManager {@link RepositoryMetadataManager}
69       */
70      public RepositoryUtils(
71              Log log,
72              ProjectBuilder projectBuilder,
73              RepositorySystem repositorySystem,
74              ArtifactResolver resolver,
75              List<ArtifactRepository> remoteRepositories,
76              List<ArtifactRepository> pluginRepositories,
77              ProjectBuildingRequest buildingRequest,
78              RepositoryMetadataManager repositoryMetadataManager) {
79          this.log = log;
80          this.projectBuilder = projectBuilder;
81          this.repositorySystem = repositorySystem;
82          this.resolver = resolver;
83          this.remoteRepositories = remoteRepositories;
84          this.pluginRepositories = pluginRepositories;
85          this.buildingRequest = buildingRequest;
86      }
87  
88      /**
89       * @param artifact not null
90       * @throws ArtifactResolverException if any
91       */
92      public void resolve(Artifact artifact) throws ArtifactResolverException {
93          List<ArtifactRepository> repos = new ArrayList<>(pluginRepositories.size() + remoteRepositories.size());
94          repos.addAll(pluginRepositories);
95          repos.addAll(remoteRepositories);
96  
97          ProjectBuildingRequest buildRequest = new DefaultProjectBuildingRequest(buildingRequest);
98          buildRequest.setRemoteRepositories(repos);
99  
100         ArtifactResult result = resolver.resolveArtifact(buildRequest, artifact);
101         artifact.setFile(result.getArtifact().getFile());
102     }
103 
104     /**
105      * Get the <code>Maven project</code> from the repository depending the <code>Artifact</code> given.
106      *
107      * @param artifact an artifact
108      * @return the Maven project for the given artifact
109      * @throws ProjectBuildingException if any
110      */
111     public MavenProject getMavenProjectFromRepository(Artifact artifact) throws ProjectBuildingException {
112         Artifact projectArtifact = artifact;
113 
114         boolean allowStubModel = false;
115         if (!"pom".equals(artifact.getType())) {
116             projectArtifact = repositorySystem.createProjectArtifact(
117                     artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
118             allowStubModel = true;
119         }
120 
121         return projectBuilder
122                 .build(projectArtifact, allowStubModel, buildingRequest)
123                 .getProject();
124     }
125 
126     /**
127      * @param artifact not null
128      * @param repo not null
129      * @return the URL in the given repo for the given artifact. If it is a snapshot artifact, the version
130      * will be the timestamp and the build number from the metadata. Could return null if the repo is blacklisted.
131      */
132     public String getDependencyUrlFromRepository(Artifact artifact, ArtifactRepository repo) {
133         if (repo.isBlacklisted()) {
134             return null;
135         }
136 
137         Artifact copyArtifact = ArtifactUtils.copyArtifact(artifact);
138         // Try to get the last artifact repo name depending the snapshot version
139         if ((artifact.isSnapshot() && repo.getSnapshots().isEnabled())) {
140             if (artifact.getBaseVersion().equals(artifact.getVersion())) {
141                 // Try to resolve it if not already done
142                 if (artifact.getMetadataList() == null
143                         || artifact.getMetadataList().isEmpty()) {
144                     try {
145                         resolve(artifact);
146                     } catch (ArtifactResolverException e) {
147                         log.error("Artifact: " + artifact.getId() + " could not be resolved.");
148                     }
149                 }
150             }
151         }
152 
153         return repo.getUrl() + "/" + repo.pathOf(copyArtifact);
154     }
155 }