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