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;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.nio.file.DirectoryNotEmptyException;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.nio.file.StandardCopyOption;
32  import java.util.ArrayDeque;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.Deque;
37  import java.util.HashMap;
38  import java.util.HashSet;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Objects;
42  import java.util.concurrent.ConcurrentHashMap;
43  import java.util.stream.Collectors;
44  import java.util.stream.Stream;
45  
46  import org.apache.maven.api.model.Model;
47  import org.apache.maven.api.services.Lookup;
48  import org.apache.maven.eventspy.EventSpy;
49  import org.apache.maven.execution.ExecutionEvent;
50  import org.apache.maven.execution.MavenSession;
51  import org.apache.maven.internal.impl.resolver.MavenWorkspaceReader;
52  import org.apache.maven.project.MavenProject;
53  import org.apache.maven.project.artifact.ProjectArtifact;
54  import org.eclipse.aether.artifact.Artifact;
55  import org.eclipse.aether.repository.WorkspaceRepository;
56  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  /**
61   * An implementation of a workspace reader that knows how to search the Maven reactor for artifacts, either as packaged
62   * jar if it has been built, or only compile output directory if packaging hasn't happened yet.
63   *
64   */
65  @Named(ReactorReader.HINT)
66  @SessionScoped
67  class ReactorReader implements MavenWorkspaceReader {
68      public static final String HINT = "reactor";
69  
70      public static final String PROJECT_LOCAL_REPO = "project-local-repo";
71  
72      private static final Collection<String> COMPILE_PHASE_TYPES = new HashSet<>(
73              Arrays.asList("jar", "ejb-client", "war", "rar", "ejb3", "par", "sar", "wsr", "har", "app-client"));
74  
75      private static final Logger LOGGER = LoggerFactory.getLogger(ReactorReader.class);
76  
77      private final MavenSession session;
78      private final WorkspaceRepository repository;
79      // groupId -> (artifactId -> (version -> project)))
80      private Map<String, Map<String, Map<String, MavenProject>>> projects;
81      private Map<String, Map<String, Map<String, MavenProject>>> allProjects;
82      private Path projectLocalRepository;
83      // projectId -> Deque<lifecycle>
84      private final Map<String, Deque<String>> lifecycles = new ConcurrentHashMap<>();
85  
86      @Inject
87      ReactorReader(MavenSession session) {
88          this.session = session;
89          this.repository = new WorkspaceRepository("reactor", null);
90      }
91  
92      //
93      // Public API
94      //
95  
96      public WorkspaceRepository getRepository() {
97          return repository;
98      }
99  
100     public File findArtifact(Artifact artifact) {
101         MavenProject project = getProject(artifact);
102 
103         if (project != null) {
104             File file = findArtifact(project, artifact, true);
105             if (file == null && project != project.getExecutionProject()) {
106                 file = findArtifact(project.getExecutionProject(), artifact, true);
107             }
108             return file;
109         }
110 
111         // No project, but most certainly a dependency which has been built previously
112         File packagedArtifactFile = findInProjectLocalRepository(artifact);
113         if (packagedArtifactFile != null && packagedArtifactFile.exists()) {
114             return packagedArtifactFile;
115         }
116 
117         return null;
118     }
119 
120     public List<String> findVersions(Artifact artifact) {
121         List<String> versions = getProjects()
122                 .getOrDefault(artifact.getGroupId(), Collections.emptyMap())
123                 .getOrDefault(artifact.getArtifactId(), Collections.emptyMap())
124                 .values()
125                 .stream()
126                 .map(MavenProject::getVersion)
127                 .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
128         if (!versions.isEmpty()) {
129             return versions;
130         }
131         return getAllProjects()
132                 .getOrDefault(artifact.getGroupId(), Collections.emptyMap())
133                 .getOrDefault(artifact.getArtifactId(), Collections.emptyMap())
134                 .values()
135                 .stream()
136                 .filter(p -> Objects.nonNull(findArtifact(p, artifact, false)))
137                 .map(MavenProject::getVersion)
138                 .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
139     }
140 
141     @Override
142     public Model findModel(Artifact artifact) {
143         MavenProject project = getProject(artifact);
144         return project == null ? null : project.getModel().getDelegate();
145     }
146 
147     //
148     // Implementation
149     //
150 
151     private File findArtifact(MavenProject project, Artifact artifact, boolean checkUptodate) {
152         // POMs are always returned from the file system
153         if ("pom".equals(artifact.getExtension())) {
154             return project.getFile();
155         }
156 
157         // Get the matching artifact from the project
158         Artifact projectArtifact = findMatchingArtifact(project, artifact);
159         if (projectArtifact != null) {
160             // If the artifact has been associated to a file, use it
161             File packagedArtifactFile = projectArtifact.getFile();
162             if (packagedArtifactFile != null && packagedArtifactFile.exists()) {
163                 return packagedArtifactFile;
164             }
165         }
166 
167         // Check in the project local repository
168         File packagedArtifactFile = findInProjectLocalRepository(artifact);
169         if (packagedArtifactFile != null
170                 && packagedArtifactFile.exists()
171                 && (!checkUptodate || isPackagedArtifactUpToDate(project, packagedArtifactFile))) {
172             return packagedArtifactFile;
173         }
174 
175         if (!hasBeenPackagedDuringThisSession(project)) {
176             // fallback to loose class files only if artifacts haven't been packaged yet
177             // and only for plain old jars. Not war files, not ear files, not anything else.
178             return determineBuildOutputDirectoryForArtifact(project, artifact);
179         }
180 
181         // The fall-through indicates that the artifact cannot be found;
182         // for instance if package produced nothing or classifier problems.
183         return null;
184     }
185 
186     private File determineBuildOutputDirectoryForArtifact(final MavenProject project, final Artifact artifact) {
187         if (isTestArtifact(artifact)) {
188             if (project.hasLifecyclePhase("test-compile")) {
189                 return new File(project.getBuild().getTestOutputDirectory());
190             }
191         } else {
192             String type = artifact.getProperty("type", "");
193             File outputDirectory = new File(project.getBuild().getOutputDirectory());
194 
195             // Check if the project is being built during this session, and if we can expect any output.
196             // There is no need to check if the build has created any outputs, see MNG-2222.
197             boolean projectCompiledDuringThisSession =
198                     project.hasLifecyclePhase("compile") && COMPILE_PHASE_TYPES.contains(type);
199 
200             // Check if the project is part of the session (not filtered by -pl, -rf, etc). If so, we check
201             // if a possible earlier Maven invocation produced some output for that project which we can use.
202             boolean projectHasOutputFromPreviousSession =
203                     !session.getProjects().contains(project) && outputDirectory.exists();
204 
205             if (projectHasOutputFromPreviousSession || projectCompiledDuringThisSession) {
206                 return outputDirectory;
207             }
208         }
209 
210         // The fall-through indicates that the artifact cannot be found;
211         // for instance if package produced nothing or classifier problems.
212         return null;
213     }
214 
215     private boolean isPackagedArtifactUpToDate(MavenProject project, File packagedArtifactFile) {
216         Path outputDirectory = Paths.get(project.getBuild().getOutputDirectory());
217         if (!outputDirectory.toFile().exists()) {
218             return true;
219         }
220 
221         try (Stream<Path> outputFiles = Files.walk(outputDirectory)) {
222             // Not using File#lastModified() to avoid a Linux JDK8 milliseconds precision bug: JDK-8177809.
223             long artifactLastModified =
224                     Files.getLastModifiedTime(packagedArtifactFile.toPath()).toMillis();
225 
226             if (session.getProjectBuildingRequest().getBuildStartTime() != null) {
227                 long buildStartTime =
228                         session.getProjectBuildingRequest().getBuildStartTime().getTime();
229                 if (artifactLastModified > buildStartTime) {
230                     return true;
231                 }
232             }
233 
234             for (Path outputFile : (Iterable<Path>) outputFiles::iterator) {
235                 if (Files.isDirectory(outputFile)) {
236                     continue;
237                 }
238 
239                 long outputFileLastModified =
240                         Files.getLastModifiedTime(outputFile).toMillis();
241                 if (outputFileLastModified > artifactLastModified) {
242                     LOGGER.warn(
243                             "File '{}' is more recent than the packaged artifact for '{}', "
244                                     + "please run a full `mvn package` build",
245                             relativizeOutputFile(outputFile),
246                             project.getArtifactId());
247                     return true;
248                 }
249             }
250 
251             return true;
252         } catch (IOException e) {
253             LOGGER.warn(
254                     "An I/O error occurred while checking if the packaged artifact is up-to-date "
255                             + "against the build output directory. "
256                             + "Continuing with the assumption that it is up-to-date.",
257                     e);
258             return true;
259         }
260     }
261 
262     private boolean hasBeenPackagedDuringThisSession(MavenProject project) {
263         boolean packaged = false;
264         for (String phase : getLifecycles(project)) {
265             switch (phase) {
266                 case "clean":
267                     packaged = false;
268                     break;
269                 case "package":
270                 case "install":
271                 case "deploy":
272                     packaged = true;
273                     break;
274                 default:
275                     break;
276             }
277         }
278         return packaged;
279     }
280 
281     private Path relativizeOutputFile(final Path outputFile) {
282         Path projectBaseDirectory =
283                 Paths.get(session.getRequest().getMultiModuleProjectDirectory().toURI());
284         return projectBaseDirectory.relativize(outputFile);
285     }
286 
287     /**
288      * Tries to resolve the specified artifact from the artifacts of the given project.
289      *
290      * @param project The project to try to resolve the artifact from, must not be <code>null</code>.
291      * @param requestedArtifact The artifact to resolve, must not be <code>null</code>.
292      * @return The matching artifact from the project or <code>null</code> if not found. Note that this
293      */
294     private Artifact findMatchingArtifact(MavenProject project, Artifact requestedArtifact) {
295         String requestedRepositoryConflictId = ArtifactIdUtils.toVersionlessId(requestedArtifact);
296         return getProjectArtifacts(project)
297                 .filter(artifact ->
298                         Objects.equals(requestedRepositoryConflictId, ArtifactIdUtils.toVersionlessId(artifact)))
299                 .findFirst()
300                 .orElse(null);
301     }
302 
303     /**
304      * Determines whether the specified artifact refers to test classes.
305      *
306      * @param artifact The artifact to check, must not be {@code null}.
307      * @return {@code true} if the artifact refers to test classes, {@code false} otherwise.
308      */
309     private static boolean isTestArtifact(Artifact artifact) {
310         return ("test-jar".equals(artifact.getProperty("type", "")))
311                 || ("jar".equals(artifact.getExtension()) && "tests".equals(artifact.getClassifier()));
312     }
313 
314     private File findInProjectLocalRepository(Artifact artifact) {
315         Path target = getArtifactPath(artifact);
316         return Files.isRegularFile(target) ? target.toFile() : null;
317     }
318 
319     /**
320      * We are interested in project success events, in which case we call
321      * the {@link #installIntoProjectLocalRepository(MavenProject)} method.
322      * The mojo started event is also captured to determine the lifecycle
323      * phases the project has been through.
324      *
325      * @param event the execution event
326      */
327     private void processEvent(ExecutionEvent event) {
328         MavenProject project = event.getProject();
329         switch (event.getType()) {
330             case MojoStarted:
331                 String phase = event.getMojoExecution().getLifecyclePhase();
332                 if (phase != null) {
333                     Deque<String> phases = getLifecycles(project);
334                     if (!Objects.equals(phase, phases.peekLast())) {
335                         phases.addLast(phase);
336                         if ("clean".equals(phase)) {
337                             cleanProjectLocalRepository(project);
338                         }
339                     }
340                 }
341                 break;
342             case ProjectSucceeded:
343             case ForkedProjectSucceeded:
344                 installIntoProjectLocalRepository(project);
345                 break;
346             default:
347                 break;
348         }
349     }
350 
351     private Deque<String> getLifecycles(MavenProject project) {
352         return lifecycles.computeIfAbsent(project.getId(), k -> new ArrayDeque<>());
353     }
354 
355     /**
356      * Copy packaged and attached artifacts from this project to the
357      * project local repository.
358      * This allows a subsequent build to resume while still being able
359      * to locate attached artifacts.
360      *
361      * @param project the project to copy artifacts from
362      */
363     private void installIntoProjectLocalRepository(MavenProject project) {
364         if ("pom".equals(project.getPackaging())
365                         && !"clean".equals(getLifecycles(project).peekLast())
366                 || hasBeenPackagedDuringThisSession(project)) {
367             getProjectArtifacts(project).filter(this::isRegularFile).forEach(this::installIntoProjectLocalRepository);
368         }
369     }
370 
371     private void cleanProjectLocalRepository(MavenProject project) {
372         try {
373             Path artifactPath = getProjectLocalRepo()
374                     .resolve(project.getGroupId())
375                     .resolve(project.getArtifactId())
376                     .resolve(project.getVersion());
377             if (Files.isDirectory(artifactPath)) {
378                 try (Stream<Path> paths = Files.list(artifactPath)) {
379                     for (Path path : (Iterable<Path>) paths::iterator) {
380                         Files.delete(path);
381                     }
382                 }
383                 try {
384                     Files.delete(artifactPath);
385                     Files.delete(artifactPath.getParent());
386                     Files.delete(artifactPath.getParent().getParent());
387                 } catch (DirectoryNotEmptyException e) {
388                     // ignore
389                 }
390             }
391         } catch (IOException e) {
392             LOGGER.error("Error while cleaning project local repository", e);
393         }
394     }
395 
396     /**
397      * Retrieve a stream of the project's artifacts
398      */
399     private Stream<Artifact> getProjectArtifacts(MavenProject project) {
400         Stream<org.apache.maven.artifact.Artifact> artifacts = Stream.concat(
401                 Stream.concat(
402                         // pom artifact
403                         Stream.of(new ProjectArtifact(project)),
404                         // main project artifact if not a pom
405                         "pom".equals(project.getPackaging()) ? Stream.empty() : Stream.of(project.getArtifact())),
406                 // attached artifacts
407                 project.getAttachedArtifacts().stream());
408         return artifacts.map(RepositoryUtils::toArtifact);
409     }
410 
411     private boolean isRegularFile(Artifact artifact) {
412         return artifact.getFile() != null && artifact.getFile().isFile();
413     }
414 
415     private void installIntoProjectLocalRepository(Artifact artifact) {
416         Path target = getArtifactPath(artifact);
417         try {
418             LOGGER.info("Copying {} to project local repository", artifact);
419             Files.createDirectories(target.getParent());
420             Files.copy(
421                     artifact.getPath(),
422                     target,
423                     StandardCopyOption.REPLACE_EXISTING,
424                     StandardCopyOption.COPY_ATTRIBUTES);
425         } catch (IOException e) {
426             LOGGER.error("Error while copying artifact to project local repository", e);
427         }
428     }
429 
430     private Path getArtifactPath(Artifact artifact) {
431         String groupId = artifact.getGroupId();
432         String artifactId = artifact.getArtifactId();
433         String version = artifact.getBaseVersion();
434         String classifier = artifact.getClassifier();
435         String extension = artifact.getExtension();
436         Path repo = getProjectLocalRepo();
437         return repo.resolve(groupId)
438                 .resolve(artifactId)
439                 .resolve(version)
440                 .resolve(artifactId
441                         + "-" + version
442                         + (classifier != null && !classifier.isEmpty() ? "-" + classifier : "")
443                         + "." + extension);
444     }
445 
446     private Path getProjectLocalRepo() {
447         if (projectLocalRepository == null) {
448             Path root = session.getRequest().getMultiModuleProjectDirectory().toPath();
449             List<MavenProject> projects = session.getProjects();
450             if (projects != null) {
451                 projectLocalRepository = projects.stream()
452                         .filter(project -> Objects.equals(root.toFile(), project.getBasedir()))
453                         .findFirst()
454                         .map(project -> project.getBuild().getDirectory())
455                         .map(Paths::get)
456                         .orElseGet(() -> root.resolve("target"))
457                         .resolve(PROJECT_LOCAL_REPO);
458             } else {
459                 return root.resolve("target").resolve(PROJECT_LOCAL_REPO);
460             }
461         }
462         return projectLocalRepository;
463     }
464 
465     private MavenProject getProject(Artifact artifact) {
466         return getAllProjects()
467                 .getOrDefault(artifact.getGroupId(), Collections.emptyMap())
468                 .getOrDefault(artifact.getArtifactId(), Collections.emptyMap())
469                 .getOrDefault(artifact.getBaseVersion(), null);
470     }
471 
472     // groupId -> (artifactId -> (version -> project)))
473     private Map<String, Map<String, Map<String, MavenProject>>> getAllProjects() {
474         // compute the projects mapping
475         if (allProjects == null) {
476             List<MavenProject> allProjects = session.getAllProjects();
477             if (allProjects != null) {
478                 Map<String, Map<String, Map<String, MavenProject>>> map = new HashMap<>();
479                 allProjects.forEach(project -> map.computeIfAbsent(project.getGroupId(), k -> new HashMap<>())
480                         .computeIfAbsent(project.getArtifactId(), k -> new HashMap<>())
481                         .put(project.getVersion(), project));
482                 this.allProjects = map;
483             } else {
484                 return Collections.emptyMap();
485             }
486         }
487         return allProjects;
488     }
489 
490     private Map<String, Map<String, Map<String, MavenProject>>> getProjects() {
491         // compute the projects mapping
492         if (projects == null) {
493             List<MavenProject> projects = session.getProjects();
494             if (projects != null) {
495                 Map<String, Map<String, Map<String, MavenProject>>> map = new HashMap<>();
496                 projects.forEach(project -> map.computeIfAbsent(project.getGroupId(), k -> new HashMap<>())
497                         .computeIfAbsent(project.getArtifactId(), k -> new HashMap<>())
498                         .put(project.getVersion(), project));
499                 this.projects = map;
500             } else {
501                 return Collections.emptyMap();
502             }
503         }
504         return projects;
505     }
506 
507     /**
508      * Singleton class used to receive events by implementing the EventSpy.
509      * It simply forwards all {@code ExecutionEvent}s to the {@code ReactorReader}.
510      */
511     @Named
512     @Singleton
513     @SuppressWarnings("unused")
514     static class ReactorReaderSpy implements EventSpy {
515 
516         private final Lookup lookup;
517 
518         @Inject
519         ReactorReaderSpy(Lookup lookup) {
520             this.lookup = lookup;
521         }
522 
523         @Override
524         public void init(Context context) throws Exception {}
525 
526         @Override
527         public void onEvent(Object event) throws Exception {
528             if (event instanceof ExecutionEvent) {
529                 ReactorReader reactorReader = lookup.lookup(ReactorReader.class);
530                 reactorReader.processEvent((ExecutionEvent) event);
531             }
532         }
533 
534         @Override
535         public void close() throws Exception {}
536     }
537 }