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.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Optional;
27  import java.util.stream.Collectors;
28  
29  import org.apache.maven.RepositoryUtils;
30  import org.apache.maven.api.Artifact;
31  import org.apache.maven.api.Dependency;
32  import org.apache.maven.api.MojoExecution;
33  import org.apache.maven.api.Node;
34  import org.apache.maven.api.Plugin;
35  import org.apache.maven.api.model.PluginExecution;
36  import org.apache.maven.api.plugin.descriptor.MojoDescriptor;
37  import org.apache.maven.api.plugin.descriptor.PluginDescriptor;
38  import org.apache.maven.api.plugin.descriptor.lifecycle.Lifecycle;
39  import org.apache.maven.api.xml.XmlNode;
40  import org.codehaus.plexus.util.xml.Xpp3Dom;
41  import org.eclipse.aether.graph.DependencyNode;
42  
43  public class DefaultMojoExecution implements MojoExecution {
44      private final InternalSession session;
45      private final org.apache.maven.plugin.MojoExecution delegate;
46  
47      public DefaultMojoExecution(InternalSession session, org.apache.maven.plugin.MojoExecution delegate) {
48          this.session = session;
49          this.delegate = delegate;
50      }
51  
52      public org.apache.maven.plugin.MojoExecution getDelegate() {
53          return delegate;
54      }
55  
56      @Override
57      public Plugin getPlugin() {
58          return new Plugin() {
59              @Override
60              public org.apache.maven.api.model.Plugin getModel() {
61                  return delegate.getPlugin().getDelegate();
62              }
63  
64              @Override
65              public PluginDescriptor getDescriptor() {
66                  return delegate.getMojoDescriptor().getPluginDescriptor().getPluginDescriptorV4();
67              }
68  
69              @Override
70              public List<Lifecycle> getLifecycles() {
71                  try {
72                      return Collections.unmodifiableList(new ArrayList<>(delegate.getMojoDescriptor()
73                              .getPluginDescriptor()
74                              .getLifecycleMappings()
75                              .values()));
76                  } catch (Exception e) {
77                      throw new RuntimeException("Unable to load plugin lifecycles", e);
78                  }
79              }
80  
81              @Override
82              public ClassLoader getClassLoader() {
83                  return delegate.getMojoDescriptor().getRealm();
84              }
85  
86              @Override
87              public Artifact getArtifact() {
88                  org.apache.maven.artifact.Artifact artifact =
89                          delegate.getMojoDescriptor().getPluginDescriptor().getPluginArtifact();
90                  org.eclipse.aether.artifact.Artifact resolverArtifact = RepositoryUtils.toArtifact(artifact);
91                  return resolverArtifact != null ? session.getArtifact(resolverArtifact) : null;
92              }
93  
94              @Override
95              public Map<String, Dependency> getDependenciesMap() {
96                  DependencyNode resolverNode =
97                          delegate.getMojoDescriptor().getPluginDescriptor().getDependencyNode();
98                  DefaultNode node = new DefaultNode(session, resolverNode, false);
99                  return Collections.unmodifiableMap(node.stream()
100                         .filter(Objects::nonNull)
101                         .map(Node::getDependency)
102                         .filter(Objects::nonNull)
103                         .collect(Collectors.toMap(d -> d.getGroupId() + ":" + d.getArtifactId(), d -> d)));
104             }
105         };
106     }
107 
108     @Override
109     public PluginExecution getModel() {
110         return delegate.getPlugin().getExecutions().stream()
111                 .filter(pe -> Objects.equals(pe.getId(), getExecutionId()))
112                 .findFirst()
113                 .map(org.apache.maven.model.PluginExecution::getDelegate)
114                 .orElse(null);
115     }
116 
117     @Override
118     public MojoDescriptor getDescriptor() {
119         return delegate.getMojoDescriptor().getMojoDescriptorV4();
120     }
121 
122     @Override
123     public String getLifecyclePhase() {
124         return delegate.getLifecyclePhase();
125     }
126 
127     @Override
128     public String getExecutionId() {
129         return delegate.getExecutionId();
130     }
131 
132     @Override
133     public String getGoal() {
134         return delegate.getGoal();
135     }
136 
137     @Override
138     public Optional<XmlNode> getConfiguration() {
139         return Optional.of(delegate.getConfiguration()).map(Xpp3Dom::getDom);
140     }
141 
142     @Override
143     public String toString() {
144         return delegate.toString();
145     }
146 }