-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle
245 lines (211 loc) · 8.01 KB
/
build.gradle
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
237
238
239
240
241
242
243
244
import groovyx.net.http.HTTPBuilder
import org.apache.http.entity.mime.MultipartEntityBuilder
import static groovyx.net.http.Method.POST
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.httpcomponents:httpmime:4.5.2'
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}
}
plugins {
id 'java'
id 'wrapper'
}
group 'com.iqnox'
version '1.0-SNAPSHOT'
defaultTasks 'clean', 'packageExtension'
project.sourceCompatibility = 1.8
project.targetCompatibility = 1.8
// set the properties accordingly
project.ext {
incrementVersionNumber = true
extensionJar = "thingworx-sftp-extension.jar" // name of the jar
baseDir = projectDir
common = 'common'
uiDir = "${baseDir}/ui" // if there are any widgets
localJarDir = "${baseDir}/lib/local" // if you have any jars add them here
srcDir = "${baseDir}/src/main" // where are the sources located
buildDir = "${baseDir}/build" // where is the build saved
configDir = "${baseDir}/configfiles" // folder location of the metadata.xml file
entitiesDir = "${baseDir}/configfiles/Entities" // folder location of the metadata.xml file
zipDir = "${baseDir}/zip" // where to store the generated zip
thingworxSdkDir = "${baseDir}/lib/twxSdk" // where the thingworx sdk is located
packageVendor = "IQNOX"
packageName = "SftpExtension"
packageTitle = "SftpExtension"
packageVersion = version
thingworxServerRoot = "http://Administrator:Administrator12345@localhost:8016"
}
repositories {
mavenCentral()
flatDir {
dirs project.ext.thingworxSdkDir
}
flatDir {
dirs project.ext.localJarDir
}
}
sourceSets {
main {
java {
srcDir project.ext.srcDir
}
}
}
clean.doFirst {
delete project.ext.zipDir
}
configurations {
packageDependencies
implementation {
extendsFrom packageDependencies
}
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.11'
// import the statically defined jar files, and the jars in the thingworx sdk
implementation fileTree(dir: project.ext.thingworxSdkDir, include: ['*.jar'])
packageDependencies fileTree(dir: project.ext.localJarDir, include: ['*.jar'])
// add all the dependencies for the extension using the packageDependencies configuration.
// use the implementation for jars that always exist at runtime
packageDependencies 'com.jcraft:jsch:0.1.55'
}
jar {
archiveFileName = project.ext.extensionJar
manifest {
attributes "Built-By" : project.ext.packageVendor
attributes "Build-Date": new Date().format("yyyy-MM-dd HH:mm:ss")
attributes "Package-Title" : project.ext.packageTitle
attributes "Package-Version": project.ext.packageVersion
attributes "Package-Vendor" : project.ext.packageVendor
}
}
def addDependenciesInMetadata() {
def file = "${buildDir}/zip/metadata.xml"
def parser = new groovy.util.XmlParser(false, true);
def xml = parser.parse(file)
xml.ExtensionPackages.ExtensionPackage.JarResources[0]?.appendNode(
"FileResource",
[type: "JAR", file: project.ext.extensionJar]
)
for (File f : configurations.packageDependencies) {
if (shouldExcludeFile(f.getName())) {
continue;
}
xml.ExtensionPackages.ExtensionPackage.JarResources[0]?.appendNode(
"FileResource",
[type: "JAR", file: f.getName()]
)
}
PrintWriter pw = new PrintWriter(new File(file))
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
private boolean shouldExcludeFile(String fileName) {
fileName.startsWith("commons-codec") ||
fileName.startsWith("commons-io") ||
fileName.startsWith("commons-lang") ||
fileName.startsWith("commons-logging") ||
fileName.startsWith("commons-compress") ||
fileName.startsWith("commons-collections4") ||
fileName.startsWith("bcprov") ||
fileName.startsWith("netty") ||
fileName.startsWith("gson") ||
fileName.startsWith("okhttp") ||
fileName.startsWith("okio") ||
fileName.startsWith("retrofit") ||
fileName.startsWith("jackson") ||
fileName.startsWith("jaxen") ||
fileName.startsWith("jdom") ||
fileName.startsWith("joda") ||
fileName.startsWith("log4j") ||
fileName.startsWith("logback") ||
fileName.startsWith("httpclient") ||
fileName.startsWith("httpcore") ||
fileName.startsWith("slf4j") ||
fileName.startsWith("thingworx") ||
fileName.startsWith("oauth2-oidc-sdk") ||
fileName.startsWith("guava") ||
fileName.startsWith("nimbus-jose-jwt") ||
fileName.startsWith("json-smart") ||
fileName.startsWith("error_prone_annotations") ||
fileName.startsWith("checker-qual")
}
task prepPackage(dependsOn: jar) {
if (project.ext.incrementVersionNumber)
increaseVersionNumber()
doLast {
delete project.ext.zipDir
delete "${buildDir}/zip/"
// add here all the jars from the packageDependencies configuration
copy {
from configurations.packageDependencies {
exclude {FileTreeElement details -> shouldExcludeFile(details.file.name)}
}
into "${buildDir}/zip/lib/common/"
}
// add the configuration
copy {
from "${project.ext.configDir}"
into "${buildDir}/zip/"
}
addDependenciesInMetadata()
// add the extension jar itself
copy {
from "${buildDir}/libs/${project.ext.extensionJar}"
into "${buildDir}/zip/lib/common/"
}
// add the ui files
copy {
from uiDir
into "${buildDir}/zip/ui/"
}
}
}
tasks.register('packageExtension', Zip) {
dependsOn(prepPackage)
archiveFileName = "${project.name}.zip"
destinationDirectory = new File(project.ext.zipDir)
from "${buildDir}/zip/"
}
def increaseVersionNumber() {
def file = "${configDir}/metadata.xml"
def parser = new groovy.util.XmlParser(false, true);
def xml = parser.parse(file)
def currentVersion = xml.ExtensionPackages.ExtensionPackage.@packageVersion[0];
def versionComponents = currentVersion.split('\\.');
def minorVersion = ++Integer.parseInt(versionComponents[versionComponents.length - 1]);
versionComponents[versionComponents.length - 1] = String.valueOf(minorVersion);
xml.ExtensionPackages.ExtensionPackage.@packageVersion = String.join('.', versionComponents);
// xml.ExtensionPackages.ExtensionPackage.get(0).attributes().put('packageVersion', String.join(',', versionComponents));
println 'Updated to version ' + String.join('.', versionComponents)
println xml.ExtensionPackages.ExtensionPackage.@packageVersion[0];
PrintWriter pw = new PrintWriter(new File(file))
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
task upload(dependsOn: packageExtension) {
doLast {
def http = new HTTPBuilder("${project.ext.thingworxServerRoot}/Thingworx/")
def extZip = file("${project.ext.zipDir}/${project.name}.zip")
http.request(POST) { req ->
uri.path = 'ExtensionPackageUploader'
uri.query = ['purpose': 'import']
headers."X-XSRF-TOKEN" = "TWX-XSRF-TOKEN-VALUE"
requestContentType = 'multipart/form-data'
MultipartEntityBuilder entity = MultipartEntityBuilder.create().setLaxMode()
entity.addBinaryBody('file', extZip)
req.setEntity(entity.build())
response.success = { resp ->
println("Upload successful!")
}
response.failure = { resp ->
println(resp.statusLine)
throw new StopExecutionException("Thingworx upload failed! See server response above")
}
}
}
}