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.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.services.*;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.PlexusContainer;
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      private final PlexusContainer container;
50  
51      @Inject
52      public DefaultProjectManager(InternalSession session, ArtifactManager artifactManager, PlexusContainer container) {
53          this.session = session;
54          this.artifactManager = artifactManager;
55          this.container = container;
56      }
57  
58      @Nonnull
59      @Override
60      public Optional<Path> getPath(Project project) {
61          return artifactManager.getPath(project.getArtifact());
62      }
63  
64      @Nonnull
65      @Override
66      public Collection<Artifact> getAttachedArtifacts(Project project) {
67          InternalSession session = ((DefaultProject) project).getSession();
68          Collection<Artifact> attached = map(
69                  getMavenProject(project).getAttachedArtifacts(),
70                  a -> session.getArtifact(RepositoryUtils.toArtifact(a)));
71          return Collections.unmodifiableCollection(attached);
72      }
73  
74      @Override
75      public void attachArtifact(Project project, Artifact artifact, Path path) {
76          getMavenProject(project)
77                  .addAttachedArtifact(RepositoryUtils.toArtifact(
78                          ((DefaultProject) project).getSession().toArtifact(artifact)));
79          artifactManager.setPath(artifact, path);
80      }
81  
82      @Override
83      public List<String> getCompileSourceRoots(Project project) {
84          List<String> roots = getMavenProject(project).getCompileSourceRoots();
85          return Collections.unmodifiableList(roots);
86      }
87  
88      @Override
89      public void addCompileSourceRoot(Project project, String sourceRoot) {
90          List<String> roots = getMavenProject(project).getCompileSourceRoots();
91          roots.add(sourceRoot);
92      }
93  
94      @Override
95      public List<String> getTestCompileSourceRoots(Project project) {
96          List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
97          return Collections.unmodifiableList(roots);
98      }
99  
100     @Override
101     public void addTestCompileSourceRoot(Project project, String sourceRoot) {
102         List<String> roots = getMavenProject(project).getTestCompileSourceRoots();
103         roots.add(sourceRoot);
104     }
105 
106     @Override
107     public List<RemoteRepository> getRepositories(Project project) {
108         return ((DefaultProject) project)
109                 .getProject().getRemoteProjectRepositories().stream()
110                         .map(session::getRemoteRepository)
111                         .collect(Collectors.toList());
112     }
113 
114     @Override
115     public void setProperty(Project project, String key, String value) {
116         getMavenProject(project).getProperties().setProperty(key, value);
117     }
118 
119     private MavenProject getMavenProject(Project project) {
120         return ((DefaultProject) project).getProject();
121     }
122 }