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.model.profile;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import static java.util.stream.Collectors.collectingAndThen;
28  import static java.util.stream.Collectors.toMap;
29  
30  /**
31   * Describes the environmental context used to determine the activation status of profiles.
32   *
33   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
34   */
35  @Deprecated(since = "4.0.0")
36  public class DefaultProfileActivationContext implements ProfileActivationContext {
37  
38      private List<String> activeProfileIds = Collections.emptyList();
39  
40      private List<String> inactiveProfileIds = Collections.emptyList();
41  
42      private Map<String, String> systemProperties = Collections.emptyMap();
43  
44      private Map<String, String> userProperties = Collections.emptyMap();
45  
46      private Map<String, String> projectProperties = Collections.emptyMap();
47  
48      private File projectDirectory;
49  
50      @Override
51      public List<String> getActiveProfileIds() {
52          return activeProfileIds;
53      }
54  
55      /**
56       * Sets the identifiers of those profiles that should be activated by explicit demand.
57       *
58       * @param activeProfileIds The identifiers of those profiles to activate, may be {@code null}.
59       * @return This context, never {@code null}.
60       */
61      public DefaultProfileActivationContext setActiveProfileIds(List<String> activeProfileIds) {
62          if (activeProfileIds != null) {
63              this.activeProfileIds = Collections.unmodifiableList(activeProfileIds);
64          } else {
65              this.activeProfileIds = Collections.emptyList();
66          }
67  
68          return this;
69      }
70  
71      @Override
72      public List<String> getInactiveProfileIds() {
73          return inactiveProfileIds;
74      }
75  
76      /**
77       * Sets the identifiers of those profiles that should be deactivated by explicit demand.
78       *
79       * @param inactiveProfileIds The identifiers of those profiles to deactivate, may be {@code null}.
80       * @return This context, never {@code null}.
81       */
82      public DefaultProfileActivationContext setInactiveProfileIds(List<String> inactiveProfileIds) {
83          if (inactiveProfileIds != null) {
84              this.inactiveProfileIds = Collections.unmodifiableList(inactiveProfileIds);
85          } else {
86              this.inactiveProfileIds = Collections.emptyList();
87          }
88  
89          return this;
90      }
91  
92      @Override
93      public Map<String, String> getSystemProperties() {
94          return systemProperties;
95      }
96  
97      /**
98       * Sets the system properties to use for interpolation and profile activation. The system properties are collected
99       * from the runtime environment like {@link System#getProperties()} and environment variables.
100      *
101      * @param systemProperties The system properties, may be {@code null}.
102      * @return This context, never {@code null}.
103      */
104     @SuppressWarnings("unchecked")
105     public DefaultProfileActivationContext setSystemProperties(Properties systemProperties) {
106         if (systemProperties != null) {
107             this.systemProperties = Collections.unmodifiableMap((Map) systemProperties);
108         } else {
109             this.systemProperties = Collections.emptyMap();
110         }
111 
112         return this;
113     }
114 
115     /**
116      * Sets the system properties to use for interpolation and profile activation. The system properties are collected
117      * from the runtime environment like {@link System#getProperties()} and environment variables.
118      *
119      * @param systemProperties The system properties, may be {@code null}.
120      * @return This context, never {@code null}.
121      */
122     public DefaultProfileActivationContext setSystemProperties(Map<String, String> systemProperties) {
123         if (systemProperties != null) {
124             this.systemProperties = Collections.unmodifiableMap(systemProperties);
125         } else {
126             this.systemProperties = Collections.emptyMap();
127         }
128 
129         return this;
130     }
131 
132     @Override
133     public Map<String, String> getUserProperties() {
134         return userProperties;
135     }
136 
137     /**
138      * Sets the user properties to use for interpolation and profile activation. The user properties have been
139      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
140      * line.
141      *
142      * @param userProperties The user properties, may be {@code null}.
143      * @return This context, never {@code null}.
144      */
145     @SuppressWarnings("unchecked")
146     public DefaultProfileActivationContext setUserProperties(Properties userProperties) {
147         if (userProperties != null) {
148             this.userProperties = Collections.unmodifiableMap((Map) userProperties);
149         } else {
150             this.userProperties = Collections.emptyMap();
151         }
152 
153         return this;
154     }
155 
156     /**
157      * Sets the user properties to use for interpolation and profile activation. The user properties have been
158      * configured directly by the user on his discretion, e.g. via the {@code -Dkey=value} parameter on the command
159      * line.
160      *
161      * @param userProperties The user properties, may be {@code null}.
162      * @return This context, never {@code null}.
163      */
164     public DefaultProfileActivationContext setUserProperties(Map<String, String> userProperties) {
165         if (userProperties != null) {
166             this.userProperties = Collections.unmodifiableMap(userProperties);
167         } else {
168             this.userProperties = Collections.emptyMap();
169         }
170 
171         return this;
172     }
173 
174     @Override
175     public File getProjectDirectory() {
176         return projectDirectory;
177     }
178 
179     /**
180      * Sets the base directory of the current project.
181      *
182      * @param projectDirectory The base directory of the current project, may be {@code null} if profile activation
183      *                         happens in the context of metadata retrieval rather than project building.
184      * @return This context, never {@code null}.
185      */
186     public DefaultProfileActivationContext setProjectDirectory(File projectDirectory) {
187         this.projectDirectory = projectDirectory;
188 
189         return this;
190     }
191 
192     @Override
193     public Map<String, String> getProjectProperties() {
194         return projectProperties;
195     }
196 
197     public DefaultProfileActivationContext setProjectProperties(Properties projectProperties) {
198         if (projectProperties != null) {
199             this.projectProperties = projectProperties.entrySet().stream()
200                     .collect(collectingAndThen(
201                             toMap(e -> String.valueOf(e.getKey()), e -> String.valueOf(e.getValue())),
202                             Collections::unmodifiableMap));
203         } else {
204             this.projectProperties = Collections.emptyMap();
205         }
206 
207         return this;
208     }
209 }