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.nio.file.Paths;
26  import java.util.*;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.RepositoryUtils;
30  import org.apache.maven.api.*;
31  import org.apache.maven.api.annotations.Nonnull;
32  import org.apache.maven.api.di.SessionScoped;
33  import org.apache.maven.api.model.Resource;
34  import org.apache.maven.api.services.*;
35  import org.apache.maven.project.MavenProject;
36  import org.eclipse.sisu.Typed;
37  
38  import static java.util.stream.Collectors.toList;
39  import static org.apache.maven.internal.impl.Utils.map;
40  import static org.apache.maven.internal.impl.Utils.nonNull;
41  
42  @Named
43  @Typed
44  @SessionScoped
45  public class DefaultProjectManager implements ProjectManager {
46  
47      private final InternalMavenSession session;
48      private final ArtifactManager artifactManager;
49  
50      @Inject
51      public DefaultProjectManager(InternalMavenSession 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          Optional<Artifact> mainArtifact = project.getMainArtifact();
60          if (mainArtifact.isPresent()) {
61              return artifactManager.getPath(mainArtifact.get());
62          }
63          return Optional.empty();
64      }
65  
66      @Nonnull
67      @Override
68      public Collection<Artifact> getAttachedArtifacts(Project project) {
69          InternalMavenSession session = ((DefaultProject) project).getSession();
70          Collection<Artifact> attached = map(
71                  getMavenProject(project).getAttachedArtifacts(),
72                  a -> session.getArtifact(RepositoryUtils.toArtifact(a)));
73          return Collections.unmodifiableCollection(attached);
74      }
75  
76      @Override
77      public Collection<Artifact> getAllArtifacts(Project project) {
78          ArrayList<Artifact> result = new ArrayList<>(2);
79          result.addAll(project.getArtifacts());
80          result.addAll(getAttachedArtifacts(project));
81          return Collections.unmodifiableCollection(result);
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<Path> getCompileSourceRoots(Project project, ProjectScope scope) {
94          MavenProject prj = getMavenProject(nonNull(project, "project"));
95          List<String> roots;
96          if (nonNull(scope, "scope") == ProjectScope.MAIN) {
97              roots = prj.getCompileSourceRoots();
98          } else if (scope == ProjectScope.TEST) {
99              roots = prj.getTestCompileSourceRoots();
100         } else {
101             throw new IllegalArgumentException("Unsupported scope " + scope);
102         }
103         return roots.stream()
104                 .map(Paths::get)
105                 .collect(Collectors.collectingAndThen(toList(), Collections::unmodifiableList));
106     }
107 
108     @Override
109     public void addCompileSourceRoot(Project project, ProjectScope scope, Path sourceRoot) {
110         MavenProject prj = getMavenProject(nonNull(project, "project"));
111         String root = nonNull(sourceRoot, "sourceRoot").toAbsolutePath().toString();
112         if (nonNull(scope, "scope") == ProjectScope.MAIN) {
113             prj.addCompileSourceRoot(root);
114         } else if (scope == ProjectScope.TEST) {
115             prj.addTestCompileSourceRoot(root);
116         } else {
117             throw new IllegalArgumentException("Unsupported scope " + scope);
118         }
119     }
120 
121     @Override
122     public List<Resource> getResources(@Nonnull Project project, @Nonnull ProjectScope scope) {
123         Project prj = nonNull(project, "project");
124         if (nonNull(scope, "scope") == ProjectScope.MAIN) {
125             return prj.getBuild().getResources();
126         } else if (scope == ProjectScope.TEST) {
127             return prj.getBuild().getTestResources();
128         } else {
129             throw new IllegalArgumentException("Unsupported scope " + scope);
130         }
131     }
132 
133     @Override
134     public void addResource(@Nonnull Project project, @Nonnull ProjectScope scope, @Nonnull Resource resource) {
135         // TODO: we should not modify the underlying model here, but resources should be stored
136         // TODO: in a separate field in the project, however, that could break v3 plugins
137         MavenProject prj = getMavenProject(nonNull(project, "project"));
138         org.apache.maven.model.Resource res = new org.apache.maven.model.Resource(nonNull(resource, "resource"));
139         if (nonNull(scope, "scope") == ProjectScope.MAIN) {
140             prj.addResource(res);
141         } else if (scope == ProjectScope.TEST) {
142             prj.addTestResource(res);
143         } else {
144             throw new IllegalArgumentException("Unsupported scope " + scope);
145         }
146     }
147 
148     @Override
149     public List<RemoteRepository> getRemoteProjectRepositories(Project project) {
150         return Collections.unmodifiableList(new MappedList<>(
151                 ((DefaultProject) project).getProject().getRemoteProjectRepositories(), session::getRemoteRepository));
152     }
153 
154     @Override
155     public List<RemoteRepository> getRemotePluginRepositories(Project project) {
156         return Collections.unmodifiableList(new MappedList<>(
157                 ((DefaultProject) project).getProject().getRemotePluginRepositories(), session::getRemoteRepository));
158     }
159 
160     @Override
161     public void setProperty(Project project, String key, String value) {
162         Properties properties = getMavenProject(project).getProperties();
163         if (value == null) {
164             properties.remove(key);
165         } else {
166             properties.setProperty(key, value);
167         }
168     }
169 
170     @Override
171     public Map<String, String> getProperties(Project project) {
172         return Collections.unmodifiableMap(
173                 new PropertiesAsMap(((DefaultProject) project).getProject().getProperties()));
174     }
175 
176     @Override
177     public Optional<Project> getExecutionProject(Project project) {
178         // Session keep tracks of the Project per project id,
179         // so we cannot use session.getProject(p) for forked projects
180         // which are temporary clones
181         return Optional.ofNullable(getMavenProject(project).getExecutionProject())
182                 .map(p -> new DefaultProject(session, p));
183     }
184 
185     private MavenProject getMavenProject(Project project) {
186         return ((DefaultProject) project).getProject();
187     }
188 }