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