View Javadoc
1   package org.apache.maven.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.api.RemoteRepository;
23  import org.apache.maven.api.Scope;
24  import org.apache.maven.api.Type;
25  import org.apache.maven.api.VersionRange;
26  import org.apache.maven.api.annotations.Nonnull;
27  import org.apache.maven.api.annotations.Nullable;
28  
29  import java.io.File;
30  import java.nio.file.Path;
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Optional;
35  
36  import org.apache.maven.RepositoryUtils;
37  import org.apache.maven.api.Artifact;
38  import org.apache.maven.api.DependencyCoordinate;
39  import org.apache.maven.api.Exclusion;
40  import org.apache.maven.api.Project;
41  import org.apache.maven.api.model.DependencyManagement;
42  import org.apache.maven.api.model.Model;
43  import org.apache.maven.api.services.ArtifactManager;
44  import org.apache.maven.api.services.TypeRegistry;
45  import org.apache.maven.project.MavenProject;
46  
47  public class DefaultProject implements Project
48  {
49  
50      private final AbstractSession session;
51      private final MavenProject project;
52  
53      public DefaultProject( AbstractSession session, MavenProject project )
54      {
55          this.session = session;
56          this.project = project;
57      }
58  
59      public AbstractSession getSession()
60      {
61          return session;
62      }
63  
64      public MavenProject getProject()
65      {
66          return project;
67      }
68  
69      @Nonnull
70      @Override
71      public String getGroupId()
72      {
73          return project.getGroupId();
74      }
75  
76      @Nonnull
77      @Override
78      public String getArtifactId()
79      {
80          return project.getArtifactId();
81      }
82  
83      @Nonnull
84      @Override
85      public String getVersion()
86      {
87          return project.getVersion();
88      }
89  
90      @Nonnull
91      @Override
92      public Artifact getArtifact()
93      {
94          org.eclipse.aether.artifact.Artifact resolverArtifact = RepositoryUtils.toArtifact( project.getArtifact() );
95          Artifact artifact = session.getArtifact( resolverArtifact );
96          Path path = resolverArtifact.getFile() != null ? resolverArtifact.getFile().toPath() : null;
97          session.getService( ArtifactManager.class ).setPath( artifact, path );
98          return artifact;
99      }
100 
101     @Nonnull
102     @Override
103     public String getPackaging()
104     {
105         return project.getPackaging();
106     }
107 
108     @Nonnull
109     @Override
110     public Model getModel()
111     {
112         return project.getModel().getDelegate();
113     }
114 
115     @Nonnull
116     @Override
117     public Optional<Path> getPomPath()
118     {
119         File file = project.getFile();
120         return Optional.ofNullable( file ).map( File::toPath );
121     }
122 
123     @Nonnull
124     @Override
125     public List<DependencyCoordinate> getDependencies()
126     {
127         return new MappedList<>( getModel().getDependencies(), this::toDependency );
128     }
129 
130     @Nonnull
131     @Override
132     public List<DependencyCoordinate> getManagedDependencies()
133     {
134         DependencyManagement dependencyManagement = getModel().getDependencyManagement();
135         if ( dependencyManagement != null )
136         {
137             return new MappedList<>( dependencyManagement.getDependencies(), this::toDependency );
138         }
139         return Collections.emptyList();
140     }
141 
142     @Override
143     public boolean isExecutionRoot()
144     {
145         return project.isExecutionRoot();
146     }
147 
148     @Override
149     public Optional<Project> getParent()
150     {
151         MavenProject parent = project.getParent();
152         return parent != null ? Optional.of( session.getProject( parent ) ) : Optional.empty();
153     }
154 
155     @Override
156     public List<RemoteRepository> getRemoteProjectRepositories()
157     {
158         return new MappedList<>( project.getRemoteProjectRepositories(), session::getRemoteRepository );
159     }
160 
161     @Override
162     public List<RemoteRepository> getRemotePluginRepositories()
163     {
164         return new MappedList<>( project.getRemotePluginRepositories(), session::getRemoteRepository );
165     }
166 
167     @Nonnull
168     private DependencyCoordinate toDependency( org.apache.maven.api.model.Dependency dependency )
169     {
170         return new DependencyCoordinate()
171         {
172             @Override
173             public String getGroupId()
174             {
175                 return dependency.getGroupId();
176             }
177 
178             @Override
179             public String getArtifactId()
180             {
181                 return dependency.getArtifactId();
182             }
183 
184             @Override
185             public String getClassifier()
186             {
187                 return dependency.getClassifier();
188             }
189 
190             @Override
191             public VersionRange getVersion()
192             {
193                 return session.parseVersionRange( dependency.getVersion() );
194             }
195 
196             @Override
197             public String getExtension()
198             {
199                 return getType().getExtension();
200             }
201 
202             @Override
203             public Type getType()
204             {
205                 String type = dependency.getType();
206                 return session.getService( TypeRegistry.class ).getType( type );
207             }
208 
209             @Nonnull
210             @Override
211             public Scope getScope()
212             {
213                 return Scope.get( dependency.getScope() );
214             }
215 
216             @Override
217             public Boolean getOptional()
218             {
219                 return dependency.isOptional();
220             }
221 
222             @Nonnull
223             @Override
224             public Collection<Exclusion> getExclusions()
225             {
226                 return new MappedCollection<>( dependency.getExclusions(), this::toExclusion );
227             }
228 
229             private Exclusion toExclusion( org.apache.maven.api.model.Exclusion exclusion )
230             {
231                 return new Exclusion()
232                 {
233                     @Nullable
234                     @Override
235                     public String getGroupId()
236                     {
237                         return exclusion.getGroupId();
238                     }
239 
240                     @Nullable
241                     @Override
242                     public String getArtifactId()
243                     {
244                         return exclusion.getArtifactId();
245                     }
246                 };
247             }
248         };
249     }
250 }