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.project;
20  
21  import java.time.Instant;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.Properties;
27  
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.model.Profile;
30  import org.apache.maven.model.building.ModelBuildingRequest;
31  import org.apache.maven.properties.internal.SystemProperties;
32  import org.eclipse.aether.RepositorySystemSession;
33  
34  /**
35   * DefaultProjectBuildingRequest
36   */
37  public class DefaultProjectBuildingRequest implements ProjectBuildingRequest {
38  
39      private RepositorySystemSession repositorySession;
40  
41      private ArtifactRepository localRepository;
42  
43      private List<ArtifactRepository> remoteRepositories;
44  
45      private List<ArtifactRepository> pluginArtifactRepositories;
46  
47      private MavenProject project;
48  
49      private int validationLevel = ModelBuildingRequest.VALIDATION_LEVEL_STRICT;
50  
51      private boolean processPlugins;
52  
53      private List<Profile> profiles;
54  
55      private List<String> activeProfileIds;
56  
57      private List<String> inactiveProfileIds;
58  
59      private Properties systemProperties;
60  
61      private Properties userProperties;
62  
63      private Instant buildStartTime;
64  
65      private boolean resolveDependencies;
66  
67      @Deprecated
68      private boolean resolveVersionRanges;
69  
70      private RepositoryMerging repositoryMerging = RepositoryMerging.POM_DOMINANT;
71  
72      public DefaultProjectBuildingRequest() {
73          processPlugins = true;
74          profiles = new ArrayList<>();
75          activeProfileIds = new ArrayList<>();
76          inactiveProfileIds = new ArrayList<>();
77          systemProperties = new Properties();
78          userProperties = new Properties();
79          remoteRepositories = new ArrayList<>();
80          pluginArtifactRepositories = new ArrayList<>();
81      }
82  
83      public DefaultProjectBuildingRequest(ProjectBuildingRequest request) {
84          this();
85          setProcessPlugins(request.isProcessPlugins());
86          setProfiles(request.getProfiles());
87          setActiveProfileIds(request.getActiveProfileIds());
88          setInactiveProfileIds(request.getInactiveProfileIds());
89          setSystemProperties(request.getSystemProperties());
90          setUserProperties(request.getUserProperties());
91          setRemoteRepositories(request.getRemoteRepositories());
92          setPluginArtifactRepositories(request.getPluginArtifactRepositories());
93          setRepositorySession(request.getRepositorySession());
94          setLocalRepository(request.getLocalRepository());
95          setBuildStartTime(request.getBuildStartTime());
96          setProject(request.getProject());
97          setResolveDependencies(request.isResolveDependencies());
98          setValidationLevel(request.getValidationLevel());
99          setResolveVersionRanges(request.isResolveVersionRanges());
100         setRepositoryMerging(request.getRepositoryMerging());
101     }
102 
103     public MavenProject getProject() {
104         return project;
105     }
106 
107     public void setProject(MavenProject mavenProject) {
108         this.project = mavenProject;
109     }
110 
111     public ProjectBuildingRequest setLocalRepository(ArtifactRepository localRepository) {
112         this.localRepository = localRepository;
113         return this;
114     }
115 
116     public ArtifactRepository getLocalRepository() {
117         return localRepository;
118     }
119 
120     public List<ArtifactRepository> getRemoteRepositories() {
121         return remoteRepositories;
122     }
123 
124     public ProjectBuildingRequest setRemoteRepositories(List<ArtifactRepository> remoteRepositories) {
125         if (remoteRepositories != null) {
126             this.remoteRepositories = new ArrayList<>(remoteRepositories);
127         } else {
128             this.remoteRepositories.clear();
129         }
130 
131         return this;
132     }
133 
134     public List<ArtifactRepository> getPluginArtifactRepositories() {
135         return pluginArtifactRepositories;
136     }
137 
138     public ProjectBuildingRequest setPluginArtifactRepositories(List<ArtifactRepository> pluginArtifactRepositories) {
139         if (pluginArtifactRepositories != null) {
140             this.pluginArtifactRepositories = new ArrayList<>(pluginArtifactRepositories);
141         } else {
142             this.pluginArtifactRepositories.clear();
143         }
144 
145         return this;
146     }
147 
148     public Properties getSystemProperties() {
149         return systemProperties;
150     }
151 
152     public ProjectBuildingRequest setSystemProperties(Properties systemProperties) {
153         if (systemProperties != null) {
154             this.systemProperties = SystemProperties.copyProperties(systemProperties);
155         } else {
156             this.systemProperties.clear();
157         }
158 
159         return this;
160     }
161 
162     public Properties getUserProperties() {
163         return userProperties;
164     }
165 
166     public ProjectBuildingRequest setUserProperties(Properties userProperties) {
167         if (userProperties != null) {
168             this.userProperties = new Properties();
169             this.userProperties.putAll(userProperties);
170         } else {
171             this.userProperties.clear();
172         }
173 
174         return this;
175     }
176 
177     public boolean isProcessPlugins() {
178         return processPlugins;
179     }
180 
181     public ProjectBuildingRequest setProcessPlugins(boolean processPlugins) {
182         this.processPlugins = processPlugins;
183         return this;
184     }
185 
186     public ProjectBuildingRequest setResolveDependencies(boolean resolveDependencies) {
187         this.resolveDependencies = resolveDependencies;
188         return this;
189     }
190 
191     public boolean isResolveDependencies() {
192         return resolveDependencies;
193     }
194 
195     /**
196      * @since 3.2.2
197      * @deprecated This got added when implementing MNG-2199 and is no longer used.
198      * Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
199      */
200     @Deprecated
201     public ProjectBuildingRequest setResolveVersionRanges(boolean value) {
202         this.resolveVersionRanges = value;
203         return this;
204     }
205 
206     /**
207      * @since 3.2.2
208      * @deprecated This got added when implementing MNG-2199 and is no longer used.
209      * Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
210      */
211     @Deprecated
212     public boolean isResolveVersionRanges() {
213         return this.resolveVersionRanges;
214     }
215 
216     public ProjectBuildingRequest setValidationLevel(int validationLevel) {
217         this.validationLevel = validationLevel;
218         return this;
219     }
220 
221     public int getValidationLevel() {
222         return validationLevel;
223     }
224 
225     public List<String> getActiveProfileIds() {
226         return activeProfileIds;
227     }
228 
229     public void setActiveProfileIds(List<String> activeProfileIds) {
230         if (activeProfileIds != null) {
231             this.activeProfileIds = new ArrayList<>(activeProfileIds);
232         } else {
233             this.activeProfileIds.clear();
234         }
235     }
236 
237     public List<String> getInactiveProfileIds() {
238         return inactiveProfileIds;
239     }
240 
241     public void setInactiveProfileIds(List<String> inactiveProfileIds) {
242         if (inactiveProfileIds != null) {
243             this.inactiveProfileIds = new ArrayList<>(inactiveProfileIds);
244         } else {
245             this.inactiveProfileIds.clear();
246         }
247     }
248 
249     public void setProfiles(List<Profile> profiles) {
250         if (profiles != null) {
251             this.profiles = new ArrayList<>(profiles);
252         } else {
253             this.profiles.clear();
254         }
255     }
256 
257     public void addProfile(Profile profile) {
258         profiles.add(profile);
259     }
260 
261     public List<Profile> getProfiles() {
262         return profiles;
263     }
264 
265     @Deprecated
266     public Date getBuildStartTime() {
267         return buildStartTime != null ? new Date(buildStartTime.toEpochMilli()) : null;
268     }
269 
270     @Deprecated
271     public void setBuildStartTime(Date buildStartTime) {
272         setBuildStartInstant(buildStartTime != null ? Instant.ofEpochMilli(buildStartTime.getTime()) : null);
273     }
274 
275     public Instant getBuildStartInstant() {
276         return this.buildStartTime;
277     }
278 
279     public void setBuildStartInstant(Instant buildStartTime) {
280         this.buildStartTime = buildStartTime;
281     }
282 
283     public RepositorySystemSession getRepositorySession() {
284         return repositorySession;
285     }
286 
287     public DefaultProjectBuildingRequest setRepositorySession(RepositorySystemSession repositorySession) {
288         this.repositorySession = repositorySession;
289         return this;
290     }
291 
292     public DefaultProjectBuildingRequest setRepositoryMerging(RepositoryMerging repositoryMerging) {
293         this.repositoryMerging = Objects.requireNonNull(repositoryMerging, "repositoryMerging cannot be null");
294         return this;
295     }
296 
297     public RepositoryMerging getRepositoryMerging() {
298         return repositoryMerging;
299     }
300 }