1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile.activation;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.File;
26
27 import org.apache.maven.model.Activation;
28 import org.apache.maven.model.ActivationFile;
29 import org.apache.maven.model.Profile;
30 import org.apache.maven.model.building.ModelProblem.Severity;
31 import org.apache.maven.model.building.ModelProblem.Version;
32 import org.apache.maven.model.building.ModelProblemCollector;
33 import org.apache.maven.model.building.ModelProblemCollectorRequest;
34 import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
35 import org.apache.maven.model.profile.ProfileActivationContext;
36 import org.codehaus.plexus.interpolation.InterpolationException;
37
38
39
40
41
42
43
44
45
46
47 @Named("file")
48 @Singleton
49 @Deprecated(since = "4.0.0")
50 public class FileProfileActivator implements ProfileActivator {
51
52 private final ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
53
54 @Inject
55 public FileProfileActivator(ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
56 this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
57 }
58
59 @Override
60 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
61 Activation activation = profile.getActivation();
62
63 if (activation == null) {
64 return false;
65 }
66
67 ActivationFile file = activation.getFile();
68
69 if (file == null) {
70 return false;
71 }
72
73 String path;
74 boolean missing;
75
76 if (file.getExists() != null && !file.getExists().isEmpty()) {
77 path = file.getExists();
78 missing = false;
79 } else if (file.getMissing() != null && !file.getMissing().isEmpty()) {
80 path = file.getMissing();
81 missing = true;
82 } else {
83 return false;
84 }
85
86 try {
87 path = profileActivationFilePathInterpolator.interpolate(path, context);
88 } catch (InterpolationException e) {
89 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
90 .setMessage("Failed to interpolate file location " + path + " for profile " + profile.getId() + ": "
91 + e.getMessage())
92 .setLocation(file.getLocation(missing ? "missing" : "exists"))
93 .setException(e));
94 return false;
95 }
96
97 if (path == null) {
98 return false;
99 }
100
101 File f = new File(path);
102
103 if (!f.isAbsolute()) {
104 return false;
105 }
106
107 boolean fileExists = f.exists();
108
109 return missing != fileExists;
110 }
111
112 @Override
113 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
114 Activation activation = profile.getActivation();
115
116 if (activation == null) {
117 return false;
118 }
119
120 ActivationFile file = activation.getFile();
121
122 return file != null;
123 }
124 }