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