1 package org.apache.maven.model.profile.activation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import org.apache.maven.model.Activation;
25 import org.apache.maven.model.ActivationFile;
26 import org.apache.maven.model.Profile;
27 import org.apache.maven.model.building.ModelProblemCollector;
28 import org.apache.maven.model.building.ModelProblem.Severity;
29 import org.apache.maven.model.building.ModelProblem.Version;
30 import org.apache.maven.model.building.ModelProblemCollectorRequest;
31 import org.apache.maven.model.path.PathTranslator;
32 import org.apache.maven.model.profile.ProfileActivationContext;
33 import org.codehaus.plexus.component.annotations.Component;
34 import org.codehaus.plexus.component.annotations.Requirement;
35 import org.codehaus.plexus.interpolation.AbstractValueSource;
36 import org.codehaus.plexus.interpolation.MapBasedValueSource;
37 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
38 import org.codehaus.plexus.util.StringUtils;
39
40
41
42
43
44
45
46 @Component( role = ProfileActivator.class, hint = "file" )
47 public class FileProfileActivator
48 implements ProfileActivator
49 {
50
51 @Requirement
52 private PathTranslator pathTranslator;
53
54 public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
55 {
56 this.pathTranslator = pathTranslator;
57 return this;
58 }
59
60 public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
61 {
62 Activation activation = profile.getActivation();
63
64 if ( activation == null )
65 {
66 return false;
67 }
68
69 ActivationFile file = activation.getFile();
70
71 if ( file == null )
72 {
73 return false;
74 }
75
76 String path;
77 boolean missing;
78
79 if ( StringUtils.isNotEmpty( file.getExists() ) )
80 {
81 path = file.getExists();
82 missing = false;
83 }
84 else if ( StringUtils.isNotEmpty( file.getMissing() ) )
85 {
86 path = file.getMissing();
87 missing = true;
88 }
89 else
90 {
91 return false;
92 }
93
94 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
95
96 final File basedir = context.getProjectDirectory();
97
98 if ( basedir != null )
99 {
100 interpolator.addValueSource( new AbstractValueSource( false )
101 {
102 public Object getValue( String expression )
103 {
104
105
106
107
108 if ( "basedir".equals( expression ) )
109 {
110 return basedir.getAbsolutePath();
111 }
112 return null;
113 }
114 } );
115 }
116 else if ( path.contains( "${basedir}" ) )
117 {
118 return false;
119 }
120
121 interpolator.addValueSource( new MapBasedValueSource( context.getProjectProperties() ) );
122
123 interpolator.addValueSource( new MapBasedValueSource( context.getUserProperties() ) );
124
125 interpolator.addValueSource( new MapBasedValueSource( context.getSystemProperties() ) );
126
127 try
128 {
129 path = interpolator.interpolate( path, "" );
130 }
131 catch ( Exception e )
132 {
133 problems.add( new ModelProblemCollectorRequest( Severity.ERROR, Version.BASE )
134 .setMessage( "Failed to interpolate file location " + path + " for profile " + profile.getId() + ": " + e.getMessage() )
135 .setLocation( file.getLocation( missing ? "missing" : "exists" ) )
136 .setException( e ) );
137 return false;
138 }
139
140 path = pathTranslator.alignToBaseDirectory( path, basedir );
141
142 File f = new File( path );
143
144 if ( !f.isAbsolute() )
145 {
146 return false;
147 }
148
149 boolean fileExists = f.exists();
150
151 return missing ? !fileExists : fileExists;
152 }
153
154 }