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