View Javadoc
1   package org.apache.maven.plugin.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.model.Model;
27  import org.apache.maven.model.Plugin;
28  import org.apache.maven.project.MavenProject;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.repository.RemoteRepository;
31  
32  /**
33   * Collects settings required to resolve the version for a plugin.
34   *
35   * @since 3.0
36   * @author Benjamin Bentmann
37   */
38  public class DefaultPluginVersionRequest
39      implements PluginVersionRequest
40  {
41  
42      private String groupId;
43  
44      private String artifactId;
45  
46      private Model pom;
47  
48      private List<RemoteRepository> repositories = Collections.emptyList();
49  
50      private RepositorySystemSession session;
51  
52      /**
53       * Creates an empty request.
54       */
55      public DefaultPluginVersionRequest()
56      {
57      }
58  
59      /**
60       * Creates a request for the specified plugin by copying settings from the specified build session. If the session
61       * has a current project, its plugin repositories will be used as well.
62       *
63       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
64       * @param session The Maven session to use, must not be {@code null}.
65       */
66      public DefaultPluginVersionRequest( Plugin plugin, MavenSession session )
67      {
68          setGroupId( plugin.getGroupId() );
69          setArtifactId( plugin.getArtifactId() );
70  
71          setRepositorySession( session.getRepositorySession() );
72  
73          MavenProject project = session.getCurrentProject();
74          if ( project != null )
75          {
76              setRepositories( project.getRemotePluginRepositories() );
77          }
78      }
79  
80      /**
81       * Creates a request for the specified plugin using the given repository session and plugin repositories.
82       *
83       * @param plugin The plugin for which to resolve a version, must not be {@code null}.
84       * @param session The repository session to use, must not be {@code null}.
85       * @param repositories The plugin repositories to query, may be {@code null}.
86       */
87      public DefaultPluginVersionRequest( Plugin plugin, RepositorySystemSession session,
88                                          List<RemoteRepository> repositories )
89      {
90          setGroupId( plugin.getGroupId() );
91          setArtifactId( plugin.getArtifactId() );
92  
93          setRepositorySession( session );
94  
95          setRepositories( repositories );
96      }
97  
98      public String getGroupId()
99      {
100         return groupId;
101     }
102 
103     public DefaultPluginVersionRequest setGroupId( String groupId )
104     {
105         this.groupId = groupId;
106 
107         return this;
108     }
109 
110     public String getArtifactId()
111     {
112         return artifactId;
113     }
114 
115     public DefaultPluginVersionRequest setArtifactId( String artifactId )
116     {
117         this.artifactId = artifactId;
118 
119         return this;
120     }
121 
122     public Model getPom()
123     {
124         return pom;
125     }
126 
127     public DefaultPluginVersionRequest setPom( Model pom )
128     {
129         this.pom = pom;
130 
131         return this;
132     }
133 
134     public List<RemoteRepository> getRepositories()
135     {
136         return repositories;
137     }
138 
139     public DefaultPluginVersionRequest setRepositories( List<RemoteRepository> repositories )
140     {
141         if ( repositories != null )
142         {
143             this.repositories = repositories;
144         }
145         else
146         {
147             this.repositories = Collections.emptyList();
148         }
149 
150         return this;
151     }
152 
153     public RepositorySystemSession getRepositorySession()
154     {
155         return session;
156     }
157 
158     public DefaultPluginVersionRequest setRepositorySession( RepositorySystemSession session )
159     {
160         this.session = session;
161 
162         return this;
163     }
164 
165 }