1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model.profile;
20
21 import org.apache.maven.api.di.Named;
22 import org.apache.maven.api.di.Singleton;
23 import org.apache.maven.api.model.Activation;
24 import org.apache.maven.api.model.ActivationFile;
25 import org.apache.maven.api.model.Profile;
26 import org.apache.maven.api.services.BuilderProblem;
27 import org.apache.maven.api.services.MavenException;
28 import org.apache.maven.api.services.ModelProblem;
29 import org.apache.maven.api.services.ModelProblemCollector;
30 import org.apache.maven.api.services.model.ProfileActivationContext;
31 import org.apache.maven.api.services.model.ProfileActivator;
32
33
34
35
36
37
38
39
40
41 @Named("file")
42 @Singleton
43 public class FileProfileActivator implements ProfileActivator {
44
45 @Override
46 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
47 Activation activation = profile.getActivation();
48
49 if (activation == null) {
50 return false;
51 }
52
53 ActivationFile file = activation.getFile();
54
55 if (file == null) {
56 return false;
57 }
58
59 String path;
60 boolean missing;
61
62 boolean hasExists = file.getExists() != null && !file.getExists().isEmpty();
63 boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty();
64 if (hasExists) {
65 if (hasMissing) {
66 problems.add(
67 BuilderProblem.Severity.WARNING,
68 ModelProblem.Version.BASE,
69 String.format(
70 "Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. "
71 + "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.",
72 profile.getId(), file.getMissing()),
73 file.getLocation("missing"));
74 }
75 path = file.getExists();
76 missing = false;
77 } else if (hasMissing) {
78 path = file.getMissing();
79 missing = true;
80 } else {
81 return false;
82 }
83
84 boolean fileExists;
85 try {
86 fileExists = context.exists(path, false);
87 } catch (MavenException e) {
88 problems.add(
89 BuilderProblem.Severity.ERROR,
90 ModelProblem.Version.BASE,
91 "Failed to check file existence " + path + " for profile " + profile.getId() + ": "
92 + e.getMessage(),
93 file.getLocation(missing ? "missing" : "exists"),
94 e);
95 return false;
96 }
97
98 return missing != fileExists;
99 }
100
101 @Override
102 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
103 Activation activation = profile.getActivation();
104
105 if (activation == null) {
106 return false;
107 }
108
109 ActivationFile file = activation.getFile();
110
111 return file != null;
112 }
113 }