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.artifact.repository;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.project.MavenProject;
26  
27  /**
28   * Collects basic settings to access the repository system.
29   *
30   * @author Benjamin Bentmann
31   */
32  public class DefaultRepositoryRequest implements RepositoryRequest {
33  
34      private boolean offline;
35  
36      private boolean forceUpdate;
37  
38      private ArtifactRepository localRepository;
39  
40      private List<ArtifactRepository> remoteRepositories;
41  
42      /**
43       * Creates an empty repository request.
44       */
45      public DefaultRepositoryRequest() {
46          // enables no-arg constructor
47      }
48  
49      /**
50       * Creates a shallow copy of the specified repository request.
51       *
52       * @param repositoryRequest The repository request to copy from, must not be {@code null}.
53       */
54      public DefaultRepositoryRequest(RepositoryRequest repositoryRequest) {
55          setLocalRepository(repositoryRequest.getLocalRepository());
56          setRemoteRepositories(repositoryRequest.getRemoteRepositories());
57          setOffline(repositoryRequest.isOffline());
58          setForceUpdate(repositoryRequest.isForceUpdate());
59      }
60  
61      public static RepositoryRequest getRepositoryRequest(MavenSession session, MavenProject project) {
62          RepositoryRequest request = new DefaultRepositoryRequest();
63  
64          request.setLocalRepository(session.getLocalRepository());
65          if (project != null) {
66              request.setRemoteRepositories(project.getPluginArtifactRepositories());
67          }
68          request.setOffline(session.isOffline());
69          request.setForceUpdate(session.getRequest().isUpdateSnapshots());
70  
71          return request;
72      }
73  
74      public boolean isOffline() {
75          return offline;
76      }
77  
78      public DefaultRepositoryRequest setOffline(boolean offline) {
79          this.offline = offline;
80  
81          return this;
82      }
83  
84      public boolean isForceUpdate() {
85          return forceUpdate;
86      }
87  
88      public DefaultRepositoryRequest setForceUpdate(boolean forceUpdate) {
89          this.forceUpdate = forceUpdate;
90  
91          return this;
92      }
93  
94      public ArtifactRepository getLocalRepository() {
95          return localRepository;
96      }
97  
98      public DefaultRepositoryRequest setLocalRepository(ArtifactRepository localRepository) {
99          this.localRepository = localRepository;
100 
101         return this;
102     }
103 
104     public List<ArtifactRepository> getRemoteRepositories() {
105         if (remoteRepositories == null) {
106             remoteRepositories = new ArrayList<>();
107         }
108 
109         return remoteRepositories;
110     }
111 
112     public DefaultRepositoryRequest setRemoteRepositories(List<ArtifactRepository> remoteRepositories) {
113         this.remoteRepositories = remoteRepositories;
114 
115         return this;
116     }
117 }