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