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.nio.file.Path;
22  import java.util.*;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.api.*;
26  import org.apache.maven.api.annotations.Nonnull;
27  import org.apache.maven.api.annotations.Nullable;
28  import org.apache.maven.api.model.DependencyManagement;
29  import org.apache.maven.api.model.Model;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.artifact.ProjectArtifact;
32  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
33  
34  import static org.apache.maven.internal.impl.Utils.nonNull;
35  
36  public class DefaultProject implements Project {
37  
38      private final InternalSession session;
39      private final MavenProject project;
40      private final Packaging packaging;
41  
42      public DefaultProject(InternalSession session, MavenProject project) {
43          this.session = session;
44          this.project = project;
45          this.packaging = session.requirePackaging(project.getPackaging());
46      }
47  
48      public InternalSession getSession() {
49          return session;
50      }
51  
52      public MavenProject getProject() {
53          return project;
54      }
55  
56      @Nonnull
57      @Override
58      public String getGroupId() {
59          return project.getGroupId();
60      }
61  
62      @Nonnull
63      @Override
64      public String getArtifactId() {
65          return project.getArtifactId();
66      }
67  
68      @Nonnull
69      @Override
70      public String getVersion() {
71          return project.getVersion();
72      }
73  
74      @Nonnull
75      @Override
76      public List<Artifact> getArtifacts() {
77          org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project));
78          org.eclipse.aether.artifact.Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact());
79  
80          ArrayList<Artifact> result = new ArrayList<>(2);
81          result.add(session.getArtifact(pomArtifact));
82          if (!ArtifactIdUtils.equalsVersionlessId(pomArtifact, projectArtifact)) {
83              result.add(session.getArtifact(projectArtifact));
84          }
85          return Collections.unmodifiableList(result);
86      }
87  
88      @Nonnull
89      @Override
90      public Packaging getPackaging() {
91          return packaging;
92      }
93  
94      @Nonnull
95      @Override
96      public Model getModel() {
97          return project.getModel().getDelegate();
98      }
99  
100     @Nonnull
101     @Override
102     public Path getPomPath() {
103         return nonNull(project.getFile(), "pomPath").toPath();
104     }
105 
106     @Override
107     public Path getBasedir() {
108         return nonNull(project.getBasedir(), "basedir").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 isTopProject() {
129         return getBasedir().equals(getSession().getTopDirectory());
130     }
131 
132     @Override
133     public boolean isRootProject() {
134         return getBasedir().equals(getRootDirectory());
135     }
136 
137     @Override
138     public Path getRootDirectory() {
139         return project.getRootDirectory();
140     }
141 
142     @Override
143     public Optional<Project> getParent() {
144         MavenProject parent = project.getParent();
145         return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
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 VersionConstraint getVersion() {
168                 return session.parseVersionConstraint(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.requireType(type);
180             }
181 
182             @Nonnull
183             @Override
184             public DependencyScope getScope() {
185                 String scope = dependency.getScope() != null ? dependency.getScope() : "";
186                 return session.requireDependencyScope(scope);
187             }
188 
189             @Override
190             public Boolean getOptional() {
191                 return dependency.isOptional();
192             }
193 
194             @Nonnull
195             @Override
196             public Collection<Exclusion> getExclusions() {
197                 return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
198             }
199 
200             private Exclusion toExclusion(org.apache.maven.api.model.Exclusion exclusion) {
201                 return new Exclusion() {
202                     @Nullable
203                     @Override
204                     public String getGroupId() {
205                         return exclusion.getGroupId();
206                     }
207 
208                     @Nullable
209                     @Override
210                     public String getArtifactId() {
211                         return exclusion.getArtifactId();
212                     }
213                 };
214             }
215         };
216     }
217 }