Liquibase script refactoring
Hi all, 

while working on #583 I discovered something odd on Liquibase.
By default it uses absolute path name as a key to know if a file has been executed already or not. 
This means that If the same script is executed from a different directory Liquibase wouldn't be able to recognize that is has already been executed and try to execute it again.

There is a property to override this called `logicalFilePath`, which value is used as a key instead of the absolute path. This property must be added to all file with changeset to execute. 
I was not able to add it to the plan SQL file and I suspect that we have to convert them to XML|YAML|JSON format to be able to specify that property. 

BTW the current layout works fine for tests and demo deployment. It could be a problem in case of more complex usage.

Regards,

_Alberto_


EDIT: 
Documentation page of this property: http://www.liquibase.org/documentation/databasechangelog.html
diff --git a/service/liquibase/src/main/java/org/eclipse/kapua/service/liquibase/KapuaLiquibaseClient.java b/service/liquibase/src/main/java/org/eclipse/kapua/service/liquibase/KapuaLiquibaseClient.java
index 9948212ed1..f6e16c8e8e 100644
--- a/service/liquibase/src/main/java/org/eclipse/kapua/service/liquibase/KapuaLiquibaseClient.java
+++ b/service/liquibase/src/main/java/org/eclipse/kapua/service/liquibase/KapuaLiquibaseClient.java
@@ -65,50 +65,7 @@ public class KapuaLiquibaseClient {
         try {
             if (parseBoolean(System.getProperty("LIQUIBASE_ENABLED", "true")) || parseBoolean(System.getenv("LIQUIBASE_ENABLED"))) {
                 try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
-
-                    //
-                    // Copy files to temporary directory
-                    String tmpDirectory = getJavaIoTmpDir().getAbsolutePath();
-
-                    File changelogTempDirectory = new File(tmpDirectory, "kapua-liquibase");
-
-                    if (changelogTempDirectory.exists()) {
-                        FileUtils.deleteDirectory(changelogTempDirectory);
-                    }
-
-                    changelogTempDirectory.mkdirs();
-                    LOG.trace("Tmp dir: {}", changelogTempDirectory.getAbsolutePath());
-
-                    Reflections reflections = new Reflections("liquibase", new ResourcesScanner());
-                    Set<String> changeLogs = reflections.getResources(Pattern.compile(".*\\.xml|.*\\.sql"));
-                    for (String script : changeLogs) {
-                        URL scriptUrl = getClass().getResource("/" + script);
-                        File changelogFile = new File(changelogTempDirectory, script.replaceFirst("liquibase/", ""));
-                        if (changelogFile.getParentFile() != null && !changelogFile.getParentFile().exists()) {
-                            LOG.trace("Creating parent dir: {}", changelogFile.getParentFile().getAbsolutePath());
-                            changelogFile.getParentFile().mkdirs();
-                        }
-                        IOUtils.write(IOUtils.toString(scriptUrl), new FileOutputStream(changelogFile));
-                        LOG.trace("Copied file: {}", changelogFile.getAbsolutePath());
-                    }
-
-                    //
-                    // Find and execute all master scripts
-                    File[] masterChangelogs = changelogTempDirectory.listFiles((FilenameFilter) (dir, name) -> name.endsWith("-master.xml"));
-
-                    LOG.info("Master Liquibase files found: {}", masterChangelogs.length);
-
-                    for (File masterChangelog : masterChangelogs) {
-                        LOG.info("Excuting liquibase script: {}", masterChangelog.getAbsolutePath());
-                        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
-                        if (schema.isPresent()) {
-                            database.setDefaultSchemaName(schema.get());
-                        }
-                        Liquibase liquibase = new Liquibase(masterChangelog.getAbsolutePath(), new FileSystemResourceAccessor(), database);
-                        liquibase.update(null);
-
-                        LOG.debug("Excuted liquibase script: {}", masterChangelog.getAbsolutePath());
-                    }
+                    loadResourcesStatic(connection, schema);
                 }
             }
         } catch (LiquibaseException | SQLException | IOException e) {
@@ -117,4 +74,50 @@ public class KapuaLiquibaseClient {
         }
     }
 
+    protected static synchronized void loadResourcesStatic(Connection connection, Optional<String> schema) throws LiquibaseException, IOException {
+        //
+        // Copy files to temporary directory
+        String tmpDirectory = getJavaIoTmpDir().getAbsolutePath();
+
+        File changelogTempDirectory = new File(tmpDirectory, "kapua-liquibase");
+
+        if (changelogTempDirectory.exists()) {
+            FileUtils.deleteDirectory(changelogTempDirectory);
+        }
+
+        changelogTempDirectory.mkdirs();
+        LOG.trace("Tmp dir: {}", changelogTempDirectory.getAbsolutePath());
+
+        Reflections reflections = new Reflections("liquibase", new ResourcesScanner());
+        Set<String> changeLogs = reflections.getResources(Pattern.compile(".*\\.xml|.*\\.sql"));
+        for (String script : changeLogs) {
+            URL scriptUrl = KapuaLiquibaseClient.class.getResource("/" + script);
+            File changelogFile = new File(changelogTempDirectory, script.replaceFirst("liquibase/", ""));
+            if (changelogFile.getParentFile() != null && !changelogFile.getParentFile().exists()) {
+                LOG.trace("Creating parent dir: {}", changelogFile.getParentFile().getAbsolutePath());
+                changelogFile.getParentFile().mkdirs();
+            }
+            IOUtils.write(IOUtils.toString(scriptUrl), new FileOutputStream(changelogFile));
+            LOG.trace("Copied file: {}", changelogFile.getAbsolutePath());
+        }
+
+        //
+        // Find and execute all master scripts
+        File[] masterChangelogs = changelogTempDirectory.listFiles((FilenameFilter) (dir, name) -> name.endsWith("-master.xml"));
+
+        LOG.info("Master Liquibase files found: {}", masterChangelogs.length);
+
+        for (File masterChangelog : masterChangelogs) {
+            LOG.info("Excuting liquibase script: {}", masterChangelog.getAbsolutePath());
+            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
+            if (schema.isPresent()) {
+                database.setDefaultSchemaName(schema.get());
+            }
+            Liquibase liquibase = new Liquibase(masterChangelog.getAbsolutePath(), new FileSystemResourceAccessor(), database);
+            liquibase.update(null);
+
+            LOG.debug("Excuted liquibase script: {}", masterChangelog.getAbsolutePath());
+        }
+    }
+
 }
diff --git a/test/src/main/java/org/eclipse/kapua/test/KapuaTest.java b/test/src/main/java/org/eclipse/kapua/test/KapuaTest.java
index 27c167f5e7..0f6a35803c 100644
--- a/test/src/main/java/org/eclipse/kapua/test/KapuaTest.java
+++ b/test/src/main/java/org/eclipse/kapua/test/KapuaTest.java
@@ -50,12 +50,12 @@ public class KapuaTest extends Assert {
 
     @Before
     public void setUp() throws SQLException {
-        connection = DriverManager.getConnection("jdbc:h2:mem:kapua;MODE=MySQL", "kapua", "kapua");
-
-        new KapuaLiquibaseClient("jdbc:h2:mem:kapua;MODE=MySQL", "kapua", "kapua").update();
-
         logger.debug("Setting up test...");
         try {
+            connection = DriverManager.getConnection("jdbc:h2:mem:kapua;MODE=MySQL", "kapua", "kapua");
+
+            new KapuaLiquibaseClient("jdbc:h2:mem:kapua;MODE=MySQL", "kapua", "kapua").update();
+
             //
             // Login
             String username = "kapua-sys";
diff --git a/travis.sh b/travis.sh
index 1f333ad975..b5ae216144 100755
--- a/travis.sh
+++ b/travis.sh
@@ -28,7 +28,7 @@ HEARTBEAT_PROCESS_PID=$!
 
 ### Build itself
 
-mvn -Dorg.eclipse.kapua.qa.waitMultiplier=2.0 -Pjavadoc >> $OUTPUT 2>&1
+mvn -Dorg.eclipse.kapua.qa.waitMultiplier=2.0 -Dgwt.compiler.localWorkers=2 -Pjavadoc >> $OUTPUT 2>&1
 tail_log
 
 ### Cleaning up heartbeat process
['travis.sh', 'test/src/main/java/org/eclipse/kapua/test/KapuaTest.java', 'service/liquibase/src/main/java/org/eclipse/kapua/service/liquibase/KapuaLiquibaseClient.java']