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 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.stream.Collectors;
30
31 import org.apache.maven.RepositoryUtils;
32 import org.apache.maven.api.*;
33 import org.apache.maven.api.annotations.Nonnull;
34 import org.apache.maven.api.di.SessionScoped;
35 import org.apache.maven.api.model.Resource;
36 import org.apache.maven.api.services.*;
37 import org.apache.maven.project.MavenProject;
38 import org.eclipse.sisu.Typed;
39
40 import static org.apache.maven.internal.impl.Utils.map;
41
42 @Named
43 @Typed
44 @SessionScoped
45 public class DefaultProjectManager implements ProjectManager {
46
47 private final InternalSession session;
48 private final ArtifactManager artifactManager;
49
50 @Inject
51 public DefaultProjectManager(InternalSession session, ArtifactManager artifactManager) {
52 this.session = session;
53 this.artifactManager = artifactManager;
54 }
55
56 @Nonnull
57 @Override
58 public Optional<Path> getPath(Project project) {
59 return artifactManager.getPath(project.getArtifact());
60 }
61
62 @Nonnull
63 @Override
64 public Collection<Artifact> getAttachedArtifacts(Project project) {
65 InternalSession session = ((DefaultProject) project).getSession();
66 Collection<Artifact> attached = map(
67 getMavenProject(project).getAttachedArtifacts(),
68 a -> session.getArtifact(RepositoryUtils.toArtifact(a)));
69 return Collections.unmodifiableCollection(attached);
70 }
71
72 @Override
73 public void attachArtifact(Project project, Artifact artifact, Path path) {
74 getMavenProject(project)
75 .addAttachedArtifact(RepositoryUtils.toArtifact(
76 ((DefaultProject) project).getSession().toArtifact(artifact)));
77 artifactManager.setPath(artifact, path);
78 }
79
80 @Override
81 public List<String> getCompileSourceRoots(Project project) {
82 List<String> roots = getMavenProject(project).getCompileSourceRoots();
83 return Collections.unmodifiableList(roots);
84 }
85
86 @Override
87 public void addCompileSourceRoot(Project project, String sourceRoot) {
88 List<String> roots = getMavenProject(project).getCompileSourceRoots();
89 roots.add(sourceRoot);
90 }
91
92 @Override
93 public List<String> getTestCompileSourceRoots(Project project) {
94 List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
95 return Collections.unmodifiableList(roots);
96 }
97
98 @Override
99 public void addTestCompileSourceRoot(Project project, String sourceRoot) {
100 List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
101 roots.add(sourceRoot);
102 }
103
104 @Override
105 public List<Resource> getResources(Project project) {
106 return getMavenProject(project).getBuild().getDelegate().getResources();
107 }
108
109 @Override
110 public void addResource(Project project, Resource resource) {
111 getMavenProject(project).addResource(new org.apache.maven.model.Resource(resource));
112 }
113
114 @Override
115 public List<Resource> getTestResources(Project project) {
116 return getMavenProject(project).getBuild().getDelegate().getTestResources();
117 }
118
119 @Override
120 public void addTestResource(Project project, Resource resource) {
121 getMavenProject(project).addTestResource(new org.apache.maven.model.Resource(resource));
122 }
123
124 @Override
125 public List<RemoteRepository> getRepositories(Project project) {
126 return ((DefaultProject) project)
127 .getProject().getRemoteProjectRepositories().stream()
128 .map(session::getRemoteRepository)
129 .collect(Collectors.toList());
130 }
131
132 @Override
133 public void setProperty(Project project, String key, String value) {
134 getMavenProject(project).getProperties().setProperty(key, value);
135 }
136
137 @Override
138 public Optional<Project> getExecutionProject(Project project) {
139
140
141
142 return Optional.ofNullable(getMavenProject(project).getExecutionProject())
143 .map(p -> new DefaultProject(session, p));
144 }
145
146 private MavenProject getMavenProject(Project project) {
147 return ((DefaultProject) project).getProject();
148 }
149 }