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.plugin.prefix;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.project.MavenProject;
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.repository.RemoteRepository;
29  
30  /**
31   * Collects settings required to resolve a plugin prefix.
32   *
33   * @since 3.0
34   */
35  public class DefaultPluginPrefixRequest implements PluginPrefixRequest {
36  
37      private String prefix;
38  
39      private List<String> pluginGroups = Collections.emptyList();
40  
41      private Model pom;
42  
43      private List<RemoteRepository> repositories = Collections.emptyList();
44  
45      private RepositorySystemSession session;
46  
47      /**
48       * Creates an empty request.
49       */
50      public DefaultPluginPrefixRequest() {}
51  
52      /**
53       * Creates a request for the specified plugin prefix and build session. The provided build session will be used to
54       * configure repository settings. If the session has a current project, its plugin repositories and model will be
55       * used as well.
56       *
57       * @param prefix The plugin prefix to resolve, must not be {@code null}.
58       * @param session The build session from which to derive further settings, must not be {@code null}.
59       */
60      public DefaultPluginPrefixRequest(String prefix, MavenSession session) {
61          setPrefix(prefix);
62  
63          setRepositorySession(session.getRepositorySession());
64  
65          MavenProject project = session.getCurrentProject();
66          if (project != null) {
67              setRepositories(project.getRemotePluginRepositories());
68              setPom(project.getModel());
69          }
70  
71          setPluginGroups(session.getPluginGroups());
72      }
73  
74      @Override
75      public String getPrefix() {
76          return prefix;
77      }
78  
79      @Override
80      public DefaultPluginPrefixRequest setPrefix(String prefix) {
81          this.prefix = prefix;
82  
83          return this;
84      }
85  
86      @Override
87      public List<String> getPluginGroups() {
88          return pluginGroups;
89      }
90  
91      @Override
92      public DefaultPluginPrefixRequest setPluginGroups(List<String> pluginGroups) {
93          if (pluginGroups != null) {
94              this.pluginGroups = Collections.unmodifiableList(pluginGroups);
95          } else {
96              this.pluginGroups = Collections.emptyList();
97          }
98  
99          return this;
100     }
101 
102     @Override
103     public Model getPom() {
104         return pom;
105     }
106 
107     @Override
108     public DefaultPluginPrefixRequest setPom(Model pom) {
109         this.pom = pom;
110 
111         return this;
112     }
113 
114     @Override
115     public List<RemoteRepository> getRepositories() {
116         return repositories;
117     }
118 
119     @Override
120     public DefaultPluginPrefixRequest setRepositories(List<RemoteRepository> repositories) {
121         if (repositories != null) {
122             this.repositories = Collections.unmodifiableList(repositories);
123         } else {
124             this.repositories = Collections.emptyList();
125         }
126 
127         return this;
128     }
129 
130     @Override
131     public RepositorySystemSession getRepositorySession() {
132         return session;
133     }
134 
135     @Override
136     public DefaultPluginPrefixRequest setRepositorySession(RepositorySystemSession session) {
137         this.session = session;
138 
139         return this;
140     }
141 }