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