You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2021/03/23 10:01:34 UTC

[plc4x] branch develop updated: - Changed the usage of the download-plugin to a groovy script that ensured the knx-master-data.xml is updated regularly without anoying the KNX foundation server

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 66640cd  - Changed the usage of the download-plugin to a groovy script that ensured the knx-master-data.xml is updated regularly without anoying the KNX foundation server
66640cd is described below

commit 66640cdb090d3a8aff943c919479f200486f13d2
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Mar 23 11:01:18 2021 +0100

    - Changed the usage of the download-plugin to a groovy script that ensured the knx-master-data.xml is updated regularly without anoying the KNX foundation server
---
 protocols/knxnetip/pom.xml                         | 21 +++++---
 .../src/main/script/getKnxMasterData.groovy        | 63 ++++++++++++++++++++++
 2 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/protocols/knxnetip/pom.xml b/protocols/knxnetip/pom.xml
index c09e7b8..5f62595 100644
--- a/protocols/knxnetip/pom.xml
+++ b/protocols/knxnetip/pom.xml
@@ -35,22 +35,27 @@
   <build>
     <plugins>
       <!-- Fetch the master-data which will be used to translate manufacturer ids to readable names -->
-      <!-- TODO: Replace this with a groovy script that checks if the local file is from the current date and only downloads once a day and uses the cached version in case of no internet connection -->
+      <!--
+        We're intentionally not using the download plugin as this would either download only once
+        and not get any updates or it would try downloading for every build. This however might result
+        in getting banned on the KNX server.
+
+        So this script makes sure the knx-master-data.xml is updated at most every 24h
+      -->
       <plugin>
-        <groupId>com.googlecode.maven-download-plugin</groupId>
-        <artifactId>download-maven-plugin</artifactId>
+        <groupId>org.codehaus.gmaven</groupId>
+        <artifactId>groovy-maven-plugin</artifactId>
+        <version>2.1.1</version>
         <executions>
+          <!-- Do some pre-build checks and report any findings to the user -->
           <execution>
             <id>fetch-knx-manufacturer-data</id>
             <phase>generate-resources</phase>
             <goals>
-              <goal>wget</goal>
+              <goal>execute</goal>
             </goals>
             <configuration>
-              <url>https://update.knx.org/data/XML/project-20/knx_master.xml</url>
-              <unpack>false</unpack>
-              <outputDirectory>${project.build.directory}/downloads</outputDirectory>
-              <outputFileName>knx-master-data.xml</outputFileName>
+              <source>${project.basedir}/src/main/script/getKnxMasterData.groovy</source>
             </configuration>
           </execution>
         </executions>
diff --git a/protocols/knxnetip/src/main/script/getKnxMasterData.groovy b/protocols/knxnetip/src/main/script/getKnxMasterData.groovy
new file mode 100644
index 0000000..dd5a68c
--- /dev/null
+++ b/protocols/knxnetip/src/main/script/getKnxMasterData.groovy
@@ -0,0 +1,63 @@
+import java.nio.file.Files
+import java.nio.file.Paths
+import java.nio.file.StandardCopyOption
+import java.util.regex.Matcher
+
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+*/
+
+// Make sure the cache directory exists in the used maven local repo
+def localRepoBaseDir = session.getLocalRepository().getBasedir()
+def cacheDir = new File(localRepoBaseDir, ".cache/knx-masterdata")
+if (!cacheDir.exists()) {
+    cacheDir.mkdirs()
+}
+
+// Check if a previous version exists and check if we need to re-download
+// If the file is less than 24h old, we won't re-download in order to avoid
+// being banned on the KNX server.
+def knxMasterDataFile = new File(cacheDir, "knx-mater-data.xml")
+def update = true
+if (knxMasterDataFile.exists()) {
+    // If the last update was less than 24h before, don't update it again.
+    if (knxMasterDataFile.lastModified() > (new Date().getTime() - 86400000)) {
+        update = false
+    }
+}
+
+// If we need to update the master-data
+if (update) {
+    try {
+        InputStream inputStream = new URL("https://update.knx.org/data/XML/project-20/knx_master.xml").openStream()
+        Files.copy(inputStream, knxMasterDataFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
+        println "Successfully updated knx-master-data.xml"
+    } catch(Exception e) {
+        println "Got an error updating knx-master-data.xml. Intentionally not failing the build as we might just be offline: " + e.getMessage()
+    }
+} else {
+    println "Skipped updating knx-master-data.xml as it's fresh enough"
+}
+
+// Copy the knx-master-data to the current target directory
+def targetDir = new File(project.getBasedir(),"target/downloads")
+if (!targetDir.exists()) {
+    targetDir.mkdirs()
+}
+def targetFile = new File(targetDir, "knx-master-data.xml")
+Files.copy(knxMasterDataFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING)