-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.gradle
224 lines (195 loc) · 7.55 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
import groovyx.net.http.HTTPBuilder
import org.apache.http.entity.mime.MultipartEntityBuilder
import static groovyx.net.http.Method.POST
group 'com.iqnox.twx.extensions'
version '1.0-SNAPSHOT'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'wrapper'
defaultTasks 'clean', 'packageExtension'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.apache.httpcomponents:httpmime:4.5.2'
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'
}
}
project.sourceCompatibility = 1.8
project.targetCompatibility = 1.8
// set the properties accordingly
project.ext {
incrementVersionNumber = true
extensionJar = "ContentLoaderExtened_Extension.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 additional Entities file that you want to be included in the extension
zipDir = "${baseDir}/zip" // where to store the generated zip
thingworxSdkDir = "${baseDir}/lib/common" // where the thingworx sdk is located
packageVendor = "IQNOX" // MANIFEST.MF information
packageName = "ContentLoaderExtension" // MANIFEST.MF information
packageTitle = "ContentLoaderExtension" // MANIFEST.MF information
packageVersion = version
thingworxServerRoot = "http://Administrator:Administrator12345@localhost:8015" // When calling the Upload task, what is the target server
}
repositories {
mavenCentral()
jcenter()
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
compile {
extendsFrom packageDependencies
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile dependencies are dependencies that you know that already exist in Tomcat
compile group: 'commons-io', name: 'commons-io', version: '2.5'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.6'
// we depend on the thingworx sdk that should be in the thingorxSdkDir
compile fileTree(dir: project.ext.thingworxSdkDir, include: ['*.jar'])
// package dependencies are external dependences that are also written in the metadata.xml under JarResoruces
// include all local jar, and other jars using manven
packageDependencies fileTree(dir: project.ext.localJarDir, include: ['**/*.jar'])
}
jar {
archiveName = project.ext.extensionJar
manifest {
attributes(["Built-By" : project.ext.packageVendor,
"Build-Date": new Date().format("yyyy-MM-dd HH:mm:ss")])
attributes(["Package-Title" : project.ext.packageTitle,
"Package-Version": project.ext.packageVersion,
"Package-Vendor" : project.ext.packageVendor], project.ext.packageName
)
}
}
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 (f.getName().startsWith("commons-codec") ||
f.getName().startsWith("commons-io") ||
f.getName().startsWith("commons-logging") ||
f.getName().startsWith("netty") ||
f.getName().startsWith("log4j") ||
f.getName().startsWith("httpclient") ||
f.getName().startsWith("httpcore") ||
f.getName().startsWith("slf4j")
) {
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()
}
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
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/"
}
copy {
from entitiesDir
into "${buildDir}/zip/Entities/"
}
}
}
task packageExtension(type: Zip, dependsOn: prepPackage, overwrite: true) {
archiveName = "${project.name}.zip"
destinationDir = 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")
}
}
}
}