1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.mojo;
18
19 import org.apache.maven.plugins.annotations.Component;
20 import org.apache.maven.plugins.annotations.Parameter;
21 import org.apache.maven.project.MavenProjectHelper;
22 import org.codehaus.gmavenplus.model.IncludeClasspath;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Properties;
26
27 import static org.codehaus.gmavenplus.util.ReflectionUtils.findConstructor;
28 import static org.codehaus.gmavenplus.util.ReflectionUtils.invokeConstructor;
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class AbstractToolsMojo extends AbstractGroovyMojo {
41
42
43
44
45
46
47 @Component
48 protected MavenProjectHelper projectHelper;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Parameter
71 protected Properties properties = new Properties();
72
73
74
75
76
77
78
79 @Parameter(defaultValue = "true")
80 protected boolean allowSystemExits;
81
82
83
84
85
86
87 @Parameter(defaultValue = "true")
88 protected boolean bindPropertiesToSeparateVariables;
89
90
91
92
93
94
95
96
97
98
99
100
101 @Parameter(defaultValue = "PROJECT_AND_PLUGIN")
102 protected IncludeClasspath includeClasspath;
103
104
105
106
107
108
109 @Parameter(defaultValue = "false")
110 protected boolean bindAllProjectProperties;
111
112
113
114
115
116
117
118
119 @Parameter(defaultValue = "false")
120 protected boolean bindSessionUserOverrideProperties;
121
122
123
124
125
126
127
128
129
130
131 @Parameter(defaultValue = "false")
132 protected boolean bindAllSessionUserProperties;
133
134
135
136
137 protected void initializeProperties() {
138 if (project != null && !properties.containsKey("project")) {
139 properties.put("project", project);
140 }
141 if (session != null && !properties.containsKey("session")) {
142 properties.put("session", session);
143 }
144 if (pluginArtifacts != null && !properties.containsKey("pluginArtifacts")) {
145 properties.put("pluginArtifacts", pluginArtifacts);
146 }
147 if (mojoExecution != null && !properties.containsKey("mojoExecution")) {
148 properties.put("mojoExecution", mojoExecution);
149 }
150 if (!properties.containsKey("log")) {
151 properties.put("log", getLog());
152 }
153 if (projectHelper != null && !properties.containsKey("projectHelper")) {
154 properties.put("projectHelper", projectHelper);
155 }
156 if (!properties.containsKey("ant")) {
157 Object antBuilder = null;
158 try {
159 antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.ant.AntBuilder")));
160 } catch (ClassNotFoundException e1) {
161 getLog().debug("groovy.ant.AntBuilder not available, trying groovy.util.AntBuilder.");
162 try {
163 antBuilder = invokeConstructor(findConstructor(classWrangler.getClass("groovy.util.AntBuilder")));
164 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | InvocationTargetException e2) {
165 logUnableToInitializeAntBuilder(e2);
166 }
167 } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
168 logUnableToInitializeAntBuilder(e);
169 }
170 if (antBuilder != null) {
171 properties.put("ant", antBuilder);
172 }
173 }
174 if (bindSessionUserOverrideProperties && !bindAllProjectProperties) {
175 getLog().warn("bindSessionUserOverrideProperties set without bindAllProjectProperties, ignoring.");
176 }
177 if (bindAllSessionUserProperties && bindSessionUserOverrideProperties) {
178 getLog().warn("bindAllSessionUserProperties and bindSessionUserOverrideProperties both set, bindAllSessionUserProperties will take precedence.");
179 }
180 if (bindAllProjectProperties && project != null) {
181 properties.putAll(project.getProperties());
182 }
183 if (session != null) {
184 if (bindAllSessionUserProperties) {
185 properties.putAll(session.getUserProperties());
186 } else if (bindAllProjectProperties && bindSessionUserOverrideProperties && project != null) {
187 for (Object key : project.getProperties().keySet()) {
188 if (session.getUserProperties().get(key) != null) {
189 properties.put(key, session.getUserProperties().get(key));
190 }
191 }
192 }
193 }
194 }
195
196
197
198
199
200
201 protected void logUnableToInitializeAntBuilder(final Throwable e) {
202 getLog().warn("Unable to initialize 'ant' with a new AntBuilder object. Is Groovy a dependency? If you are using Groovy >= 2.3.0-rc-1, remember to include groovy-ant as a dependency.");
203 }
204
205 }