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 java.io.File;
22
23 import org.apache.maven.api.di.Inject;
24 import org.apache.maven.api.di.Named;
25 import org.apache.maven.api.di.Singleton;
26 import org.apache.maven.api.model.Activation;
27 import org.apache.maven.api.model.ActivationFile;
28 import org.apache.maven.api.model.Profile;
29 import org.apache.maven.api.services.BuilderProblem;
30 import org.apache.maven.api.services.InterpolatorException;
31 import org.apache.maven.api.services.ModelProblem;
32 import org.apache.maven.api.services.ModelProblemCollector;
33 import org.apache.maven.api.services.model.ProfileActivationContext;
34 import org.apache.maven.api.services.model.ProfileActivator;
35 import org.apache.maven.internal.impl.model.ProfileActivationFilePathInterpolator;
36
37
38
39
40
41
42
43
44
45 @Named("file")
46 @Singleton
47 public class FileProfileActivator implements ProfileActivator {
48
49 private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
50
51 @Inject
52 public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
53 this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
54 }
55
56 @Override
57 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
58 Activation activation = profile.getActivation();
59
60 if (activation == null) {
61 return false;
62 }
63
64 ActivationFile file = activation.getFile();
65
66 if (file == null) {
67 return false;
68 }
69
70 String path;
71 boolean missing;
72
73 boolean hasExists = file.getExists() != null && !file.getExists().isEmpty();
74 boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty();
75 if (hasExists) {
76 if (hasMissing) {
77 problems.add(
78 BuilderProblem.Severity.WARNING,
79 ModelProblem.Version.BASE,
80 String.format(
81 "Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. "
82 + "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.",
83 profile.getId(), file.getMissing()),
84 file.getLocation("missing"));
85 }
86 path = file.getExists();
87 missing = false;
88 } else if (hasMissing) {
89 path = file.getMissing();
90 missing = true;
91 } else {
92 return false;
93 }
94
95 try {
96 path = profileActivationFilePathInterpolator.interpolate(path, context);
97 } catch (InterpolatorException e) {
98 problems.add(
99 BuilderProblem.Severity.ERROR,
100 ModelProblem.Version.BASE,
101 "Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
102 + e.getMessage(),
103 file.getLocation(missing ? "missing" : "exists"),
104 e);
105 return false;
106 }
107
108 if (path == null) {
109 return false;
110 }
111
112 File f = new File(path);
113
114 if (!f.isAbsolute()) {
115 return false;
116 }
117
118 boolean fileExists = f.exists();
119
120 return missing != fileExists;
121 }
122
123 @Override
124 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
125 Activation activation = profile.getActivation();
126
127 if (activation == null) {
128 return false;
129 }
130
131 ActivationFile file = activation.getFile();
132
133 return file != null;
134 }
135 }