-
Notifications
You must be signed in to change notification settings - Fork 159
/
ReadMavenPomStep.java
236 lines (198 loc) · 7.45 KB
/
ReadMavenPomStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 CloudBees Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.utility.steps.maven;
import hudson.Extension;
import hudson.FilePath;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.cps.GroovyShellDecorator;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Reads a maven pom file from the workspace.
*
* @author Robert Sandell <rsandell@cloudbees.com>.
*/
public class ReadMavenPomStep extends AbstractStepImpl {
private static final String ORG_APACHE_MAVEN_MODEL = "org.apache.maven.model";
private String file;
@DataBoundConstructor
public ReadMavenPomStep() {
}
/**
* Optional name of the maven file to read.
* If empty 'pom.xml' in the current working directory will be used.
*
* @return file name
*/
public String getFile() {
return file;
}
/**
* Optional name of the maven file to read.
* If empty 'pom.xml' in the current working directory will be used.
*
* @param file optional file name
*/
@DataBoundSetter
public void setFile(String file) {
this.file = file;
}
@Extension
public static class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(Execution.class);
}
@Override
public String getFunctionName() {
return "readMavenPom";
}
@Override
public String getDisplayName() {
return "Read a maven project file.";
}
}
public static class Execution extends AbstractSynchronousNonBlockingStepExecution<Model> {
private static final long serialVersionUID = 1L;
@StepContextParameter
private transient FilePath ws;
@Inject
private transient ReadMavenPomStep step;
@Override
protected Model run() throws Exception {
FilePath path;
if (!StringUtils.isBlank(step.getFile())) {
path = ws.child(step.getFile());
} else {
path = ws.child("pom.xml");
}
if (!path.exists()) {
throw new FileNotFoundException(path.getRemote() + " does not exist.");
}
if (path.isDirectory()) {
throw new FileNotFoundException(path.getRemote() + " is a directory.");
}
try (InputStream is = path.read()) {
return new MavenXpp3Reader().read(is);
}
}
}
/**
* Auto imports org.apache.maven.model.* .
*/
@Extension
public static class PackageAutoImporter extends GroovyShellDecorator {
@Override
public void customizeImports(CpsFlowExecution context, ImportCustomizer ic) {
ic.addStarImports(ORG_APACHE_MAVEN_MODEL);
}
}
/**
* Whitelists all non static setters, getters and constructors in the package org.apache.maven.model.
*/
@Extension
public static class WhiteLister extends Whitelist {
public WhiteLister() {
super();
}
@Override
public boolean permitsMethod(Method method, Object receiver, Object[] args) {
if (receiver == null) {
return false;
}
final Class<?> aClass = receiver.getClass();
final Package aPackage = aClass.getPackage();
if(aPackage == null) {
return false;
}
final String name = aPackage.getName();
return name.equals(ORG_APACHE_MAVEN_MODEL)
&& ( method.getName().startsWith("set")
|| method.getName().startsWith("get")
|| method.getName().startsWith("add")
|| method.getName().startsWith("find") //in case we add some helpers to the meta object
);
}
@Override
public boolean permitsConstructor(@Nonnull Constructor<?> constructor, @Nonnull Object[] args) {
if (constructor == null) {
return false;
}
final Package aPackage = constructor.getDeclaringClass().getPackage();
if (aPackage == null) {
return false;
}
return aPackage.getName().equals(ORG_APACHE_MAVEN_MODEL);
}
@Override
public boolean permitsStaticMethod(@Nonnull Method method, @Nonnull Object[] args) {
return false;
}
@Override
public boolean permitsFieldGet(@Nonnull Field field, @Nonnull Object receiver) {
if (receiver == null) {
return false;
}
final Package aPackage = receiver.getClass().getPackage();
if (aPackage == null) {
return false;
}
return aPackage.getName().equals(ORG_APACHE_MAVEN_MODEL);
}
@Override
public boolean permitsFieldSet(@Nonnull Field field, @Nonnull Object receiver, Object value) {
if (receiver == null) {
return false;
}
final Package aPackage = receiver.getClass().getPackage();
if (aPackage == null) {
return false;
}
return aPackage.getName().equals(ORG_APACHE_MAVEN_MODEL);
}
@Override
public boolean permitsStaticFieldGet(@Nonnull Field field) {
return false;
}
@Override
public boolean permitsStaticFieldSet(@Nonnull Field field, Object value) {
return false;
}
}
}