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.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.nio.file.Path;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Optional;
29  import java.util.Set;
30  import java.util.stream.Collectors;
31  
32  import org.apache.maven.RepositoryUtils;
33  import org.apache.maven.SessionScoped;
34  import org.apache.maven.api.Artifact;
35  import org.apache.maven.api.Node;
36  import org.apache.maven.api.Project;
37  import org.apache.maven.api.RemoteRepository;
38  import org.apache.maven.api.ResolutionScope;
39  import org.apache.maven.api.Scope;
40  import org.apache.maven.api.Session;
41  import org.apache.maven.api.annotations.Nonnull;
42  import org.apache.maven.api.services.ArtifactManager;
43  import org.apache.maven.api.services.MavenException;
44  import org.apache.maven.api.services.ProjectManager;
45  import org.apache.maven.lifecycle.LifecycleExecutionException;
46  import org.apache.maven.lifecycle.internal.LifecycleDependencyResolver;
47  import org.apache.maven.project.MavenProject;
48  import org.codehaus.plexus.PlexusContainer;
49  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
50  
51  @Named
52  @SessionScoped
53  public class DefaultProjectManager implements ProjectManager {
54  
55      private final Session session;
56      private final ArtifactManager artifactManager;
57      private final PlexusContainer container;
58  
59      @Inject
60      public DefaultProjectManager(Session session, ArtifactManager artifactManager, PlexusContainer container) {
61          this.session = session;
62          this.artifactManager = artifactManager;
63          this.container = container;
64      }
65  
66      @Nonnull
67      @Override
68      public Optional<Path> getPath(Project project) {
69          // TODO: apiv4
70          throw new UnsupportedOperationException("Not implemented yet");
71      }
72  
73      @Nonnull
74      @Override
75      public Collection<Artifact> getAttachedArtifacts(Project project) {
76          AbstractSession session = ((DefaultProject) project).getSession();
77          Collection<Artifact> attached = getMavenProject(project).getAttachedArtifacts().stream()
78                  .map(RepositoryUtils::toArtifact)
79                  .map(session::getArtifact)
80                  .collect(Collectors.toList());
81          return Collections.unmodifiableCollection(attached);
82      }
83  
84      @Override
85      public void attachArtifact(Project project, Artifact artifact, Path path) {
86          getMavenProject(project)
87                  .addAttachedArtifact(RepositoryUtils.toArtifact(
88                          ((DefaultProject) project).getSession().toArtifact(artifact)));
89          artifactManager.setPath(artifact, path);
90      }
91  
92      @Override
93      public List<String> getCompileSourceRoots(Project project) {
94          List<String> roots = getMavenProject(project).getCompileSourceRoots();
95          return Collections.unmodifiableList(roots);
96      }
97  
98      @Override
99      public void addCompileSourceRoot(Project project, String sourceRoot) {
100         List<String> roots = getMavenProject(project).getCompileSourceRoots();
101         roots.add(sourceRoot);
102     }
103 
104     @Override
105     public List<String> getTestCompileSourceRoots(Project project) {
106         List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
107         return Collections.unmodifiableList(roots);
108     }
109 
110     @Override
111     public void addTestCompileSourceRoot(Project project, String sourceRoot) {
112         List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
113         roots.add(sourceRoot);
114     }
115 
116     @Override
117     public List<RemoteRepository> getRepositories(Project project) {
118         // TODO: apiv4
119         throw new UnsupportedOperationException("Not implemented yet");
120     }
121 
122     @Override
123     public List<Artifact> getResolvedDependencies(Project project, ResolutionScope scope) {
124         Collection<String> toResolve = toScopes(scope);
125         try {
126             LifecycleDependencyResolver lifecycleDependencyResolver =
127                     container.lookup(LifecycleDependencyResolver.class);
128             Set<org.apache.maven.artifact.Artifact> artifacts = lifecycleDependencyResolver.resolveProjectArtifacts(
129                     getMavenProject(project),
130                     toResolve,
131                     toResolve,
132                     ((DefaultSession) session).getMavenSession(),
133                     false,
134                     Collections.emptySet());
135             return artifacts.stream()
136                     .map(RepositoryUtils::toArtifact)
137                     .map(((DefaultSession) session)::getArtifact)
138                     .collect(Collectors.toList());
139         } catch (LifecycleExecutionException | ComponentLookupException e) {
140             throw new MavenException("Unable to resolve project dependencies", e);
141         }
142     }
143 
144     @Override
145     public Node getCollectedDependencies(Project project, ResolutionScope scope) {
146         // TODO: apiv4
147         throw new UnsupportedOperationException("Not implemented yet");
148     }
149 
150     @Override
151     public void setProperty(Project project, String key, String value) {
152         getMavenProject(project).getProperties().setProperty(key, value);
153     }
154 
155     private MavenProject getMavenProject(Project project) {
156         return ((DefaultProject) project).getProject();
157     }
158 
159     private Collection<String> toScopes(ResolutionScope scope) {
160         return scope.scopes().stream().map(Scope::id).collect(Collectors.toList());
161     }
162 }