001package org.apache.maven.plugin.version;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collections;
023import java.util.List;
024
025import org.apache.maven.execution.MavenSession;
026import org.apache.maven.model.Model;
027import org.apache.maven.model.Plugin;
028import org.apache.maven.project.MavenProject;
029import org.eclipse.aether.RepositorySystemSession;
030import org.eclipse.aether.repository.RemoteRepository;
031
032/**
033 * Collects settings required to resolve the version for a plugin.
034 *
035 * @since 3.0
036 * @author Benjamin Bentmann
037 */
038public class DefaultPluginVersionRequest
039    implements PluginVersionRequest
040{
041
042    private String groupId;
043
044    private String artifactId;
045
046    private Model pom;
047
048    private List<RemoteRepository> repositories = Collections.emptyList();
049
050    private RepositorySystemSession session;
051
052    /**
053     * Creates an empty request.
054     */
055    public DefaultPluginVersionRequest()
056    {
057    }
058
059    /**
060     * Creates a request for the specified plugin by copying settings from the specified build session. If the session
061     * has a current project, its plugin repositories will be used as well.
062     *
063     * @param plugin The plugin for which to resolve a version, must not be {@code null}.
064     * @param session The Maven session to use, must not be {@code null}.
065     */
066    public DefaultPluginVersionRequest( Plugin plugin, MavenSession session )
067    {
068        setGroupId( plugin.getGroupId() );
069        setArtifactId( plugin.getArtifactId() );
070
071        setRepositorySession( session.getRepositorySession() );
072
073        MavenProject project = session.getCurrentProject();
074        if ( project != null )
075        {
076            setRepositories( project.getRemotePluginRepositories() );
077        }
078    }
079
080    /**
081     * Creates a request for the specified plugin using the given repository session and plugin repositories.
082     *
083     * @param plugin The plugin for which to resolve a version, must not be {@code null}.
084     * @param session The repository session to use, must not be {@code null}.
085     * @param repositories The plugin repositories to query, may be {@code null}.
086     */
087    public DefaultPluginVersionRequest( Plugin plugin, RepositorySystemSession session,
088                                        List<RemoteRepository> repositories )
089    {
090        setGroupId( plugin.getGroupId() );
091        setArtifactId( plugin.getArtifactId() );
092
093        setRepositorySession( session );
094
095        setRepositories( repositories );
096    }
097
098    public String getGroupId()
099    {
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}