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 InternalMavenSession session;
39      private final MavenProject project;
40      private final Packaging packaging;
41  
42      public DefaultProject(InternalMavenSession session, MavenProject project) {
43          this.session = session;
44          this.project = project;
45          ClassLoader ttcl = Thread.currentThread().getContextClassLoader();
46          try {
47              Thread.currentThread().setContextClassLoader(project.getClassRealm());
48              this.packaging = session.requirePackaging(project.getPackaging());
49          } finally {
50              Thread.currentThread().setContextClassLoader(ttcl);
51          }
52      }
53  
54      public InternalMavenSession getSession() {
55          return session;
56      }
57  
58      public MavenProject getProject() {
59          return project;
60      }
61  
62      @Nonnull
63      @Override
64      public String getGroupId() {
65          return project.getGroupId();
66      }
67  
68      @Nonnull
69      @Override
70      public String getArtifactId() {
71          return project.getArtifactId();
72      }
73  
74      @Nonnull
75      @Override
76      public String getVersion() {
77          return project.getVersion();
78      }
79  
80      @Nonnull
81      @Override
82      public List<Artifact> getArtifacts() {
83          org.eclipse.aether.artifact.Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project));
84          org.eclipse.aether.artifact.Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact());
85  
86          ArrayList<Artifact> result = new ArrayList<>(2);
87          result.add(session.getArtifact(pomArtifact));
88          if (!ArtifactIdUtils.equalsVersionlessId(pomArtifact, projectArtifact)) {
89              result.add(session.getArtifact(projectArtifact));
90          }
91          return Collections.unmodifiableList(result);
92      }
93  
94      @Nonnull
95      @Override
96      public Packaging getPackaging() {
97          return packaging;
98      }
99  
100     @Nonnull
101     @Override
102     public Model getModel() {
103         return project.getModel().getDelegate();
104     }
105 
106     @Nonnull
107     @Override
108     public Path getPomPath() {
109         return nonNull(project.getFile(), "pomPath").toPath();
110     }
111 
112     @Override
113     public Path getBasedir() {
114         return nonNull(project.getBasedir(), "basedir").toPath();
115     }
116 
117     @Nonnull
118     @Override
119     public List<DependencyCoordinate> getDependencies() {
120         return new MappedList<>(getModel().getDependencies(), this::toDependency);
121     }
122 
123     @Nonnull
124     @Override
125     public List<DependencyCoordinate> getManagedDependencies() {
126         DependencyManagement dependencyManagement = getModel().getDependencyManagement();
127         if (dependencyManagement != null) {
128             return new MappedList<>(dependencyManagement.getDependencies(), this::toDependency);
129         }
130         return Collections.emptyList();
131     }
132 
133     @Override
134     public boolean isTopProject() {
135         return getBasedir().equals(getSession().getTopDirectory());
136     }
137 
138     @Override
139     public boolean isRootProject() {
140         return getBasedir().equals(getRootDirectory());
141     }
142 
143     @Override
144     public Path getRootDirectory() {
145         return project.getRootDirectory();
146     }
147 
148     @Override
149     public Optional<Project> getParent() {
150         MavenProject parent = project.getParent();
151         return parent != null ? Optional.of(session.getProject(parent)) : Optional.empty();
152     }
153 
154     @Nonnull
155     private DependencyCoordinate toDependency(org.apache.maven.api.model.Dependency dependency) {
156         return new DependencyCoordinate() {
157             @Override
158             public String getGroupId() {
159                 return dependency.getGroupId();
160             }
161 
162             @Override
163             public String getArtifactId() {
164                 return dependency.getArtifactId();
165             }
166 
167             @Override
168             public String getClassifier() {
169                 return dependency.getClassifier();
170             }
171 
172             @Override
173             public VersionConstraint getVersion() {
174                 return session.parseVersionConstraint(dependency.getVersion());
175             }
176 
177             @Override
178             public String getExtension() {
179                 return getType().getExtension();
180             }
181 
182             @Override
183             public Type getType() {
184                 String type = dependency.getType();
185                 return session.requireType(type);
186             }
187 
188             @Nonnull
189             @Override
190             public DependencyScope getScope() {
191                 String scope = dependency.getScope() != null ? dependency.getScope() : "";
192                 return session.requireDependencyScope(scope);
193             }
194 
195             @Override
196             public Boolean getOptional() {
197                 return dependency.isOptional();
198             }
199 
200             @Nonnull
201             @Override
202             public Collection<Exclusion> getExclusions() {
203                 return new MappedCollection<>(dependency.getExclusions(), this::toExclusion);
204             }
205 
206             private Exclusion toExclusion(org.apache.maven.api.model.Exclusion exclusion) {
207                 return new Exclusion() {
208                     @Nullable
209                     @Override
210                     public String getGroupId() {
211                         return exclusion.getGroupId();
212                     }
213 
214                     @Nullable
215                     @Override
216                     public String getArtifactId() {
217                         return exclusion.getArtifactId();
218                     }
219                 };
220             }
221         };
222     }
223 }