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 java.io.File;
22  import java.nio.file.Path;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Optional;
27  
28  import org.apache.maven.RepositoryUtils;
29  import org.apache.maven.api.Artifact;
30  import org.apache.maven.api.DependencyCoordinate;
31  import org.apache.maven.api.Exclusion;
32  import org.apache.maven.api.Project;
33  import org.apache.maven.api.RemoteRepository;
34  import org.apache.maven.api.Scope;
35  import org.apache.maven.api.Type;
36  import org.apache.maven.api.VersionRange;
37  import org.apache.maven.api.annotations.Nonnull;
38  import org.apache.maven.api.annotations.Nullable;
39  import org.apache.maven.api.model.DependencyManagement;
40  import org.apache.maven.api.model.Model;
41  import org.apache.maven.api.services.ArtifactManager;
42  import org.apache.maven.api.services.TypeRegistry;
43  import org.apache.maven.project.MavenProject;
44  
45  public class DefaultProject implements Project {
46  
47      private final AbstractSession session;
48      private final MavenProject project;
49  
50      public DefaultProject(AbstractSession session, MavenProject project) {
51          this.session = session;
52          this.project = project;
53      }
54  
55      public AbstractSession getSession() {
56          return session;
57      }
58  
59      public MavenProject getProject() {
60          return project;
61      }
62  
63      @Nonnull
64      @Override
65      public String getGroupId() {
66          return project.getGroupId();
67      }
68  
69      @Nonnull
70      @Override
71      public String getArtifactId() {
72          return project.getArtifactId();
73      }
74  
75      @Nonnull
76      @Override
77      public String getVersion() {
78          return project.getVersion();
79      }
80  
81      @Nonnull
82      @Override
83      public Artifact getArtifact() {
84          org.eclipse.aether.artifact.Artifact resolverArtifact = RepositoryUtils.toArtifact(project.getArtifact());
85          Artifact artifact = session.getArtifact(resolverArtifact);
86          Path path =
87                  resolverArtifact.getFile() != null ? resolverArtifact.getFile().toPath() : null;
88          session.getService(ArtifactManager.class).setPath(artifact, path);
89          return artifact;
90      }
91  
92      @Nonnull
93      @Override
94      public String getPackaging() {
95          return project.getPackaging();
96      }
97  
98      @Nonnull
99      @Override
100     public Model getModel() {
101         return project.getModel().getDelegate();
102     }
103 
104     @Nonnull
105     @Override
106     public Optional<Path> getPomPath() {
107         File file = project.getFile();
108         return Optional.ofNullable(file).map(File::toPath);
109     }
110 
111     @Nonnull
112     @Override
113     public List<DependencyCoordinate> getDependencies() {
114         return new MappedList<>(getModel().getDependencies(), this::toDependency);
115     }
116 
117     @Nonnull
118     @Override
119     public List<DependencyCoordinate> getManagedDependencies() {
120         DependencyManagement dependencyManagement = getModel().getDependencyManagement();
121         if (dependencyManagement != null) {
122             return new MappedList<>(dependencyManagement.getDependencies(), this::toDependency);
123         }
124         return Collections.emptyList();
125     }
126 
127     @Override
128     public boolean isExecutionRoot() {
129         return project.isExecutionRoot();
130     }
131 
132     @Override
133     public Optional<Project> getParent() {
134         MavenProject parent = project.getParent();
135         return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
136     }
137 
138     @Override
139     public List<RemoteRepository> getRemoteProjectRepositories() {
140         return new MappedList<>(project.getRemoteProjectRepositories(), session::getRemoteRepository);
141     }
142 
143     @Override
144     public List<RemoteRepository> getRemotePluginRepositories() {
145         return new MappedList<>(project.getRemotePluginRepositories(), session::getRemoteRepository);
146     }
147 
148     @Nonnull
149     private DependencyCoordinate toDependency(org.apache.maven.api.model.Dependency dependency) {
150         return new DependencyCoordinate() {
151             @Override
152             public String getGroupId() {
153                 return dependency.getGroupId();
154             }
155 
156             @Override
157             public String getArtifactId() {
158                 return dependency.getArtifactId();
159             }
160 
161             @Override
162             public String getClassifier() {
163                 return dependency.getClassifier();
164             }
165 
166             @Override
167             public VersionRange getVersion() {
168                 return session.parseVersionRange(dependency.getVersion());
169             }
170 
171             @Override
172             public String getExtension() {
173                 return getType().getExtension();
174             }
175 
176             @Override
177             public Type getType() {
178                 String type = dependency.getType();
179                 return session.getService(TypeRegistry.class).getType(type);
180             }
181 
182             @Nonnull
183             @Override
184             public Scope getScope() {
185                 return Scope.get(dependency.getScope());
186             }
187 
188             @Override
189             public Boolean getOptional() {
190                 return dependency.isOptional();
191             }
192 
193             @Nonnull
194             @Override
195             public Collection<Exclusion> getExclusions() {
196                 return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
197             }
198 
199             private Exclusion toExclusion(org.apache.maven.api.model.Exclusion exclusion) {
200                 return new Exclusion() {
201                     @Nullable
202                     @Override
203                     public String getGroupId() {
204                         return exclusion.getGroupId();
205                     }
206 
207                     @Nullable
208                     @Override
209                     public String getArtifactId() {
210                         return exclusion.getArtifactId();
211                     }
212                 };
213             }
214         };
215     }
216 }