<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.dbmaster.plugins</groupId>
    <artifactId>dbmaster-plugin-parent</artifactId>
    <version>1.12.1</version>
    <packaging>pom</packaging>
   
    <name>${project.groupId}:${project.artifactId}</name>
    <description>Maven parent module for DBMaster plugins</description>
    <url>https://github.com/dbmaster/dbmaster-api</url>

    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>schristin</id>
            <name>Slava Christin</name>
            <organization>Branegy Systems, Inc</organization>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:git://github.com/dbmaster/dbmaster-api.git</connection>
        <developerConnection>scm:git:ssh://github.com:dbmaster/dbmaster-api.git</developerConnection>
        <url>https://github.com/dbmaster/dbmaster-api/tree/master/dbmaster-plugin-parent</url>
        <tag>v${project.version}-pp</tag>
    </scm>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <artifact.bundle.path>${project.build.directory}/${project.build.finalName}.jar</artifact.bundle.path>

        <!-- command line override, default -->
        <test.properties.path>${basedir}/src/test/resources/test.properties</test.properties.path>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.dbmaster</groupId>
            <artifactId>dbmaster-api</artifactId>
            <version>1.12.0</version> <!-- release.version -->
            <classifier>compile</classifier>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.dbmaster</groupId>
            <artifactId>dbmaster-api</artifactId>
            <version>1.12.0</version> <!-- release.version -->
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <distributionManagement>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </distributionManagement>
   
    <build>
        <resources>
            <resource>
                <directory>src/main/groovy</directory>
                <includes>
                    <include>**/*.groovy</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.9.1</version>
                    <executions>
                        <execution>
                            <id>add-sources</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/main/groovy</source>
                                </sources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>add-test-sources</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/test/groovy</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <extensions>true</extensions>
                    <configuration>
                        <compilerId>groovy-eclipse-compiler</compilerId>
                        <!-- set verbose to be true if you want lots of uninteresting messages -->
                        <verbose>true</verbose>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-compiler</artifactId>
                            <version>2.9.2-01</version>
                        </dependency>
                        <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch -->
                        <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-eclipse-batch</artifactId>
                            <!-- <version>2.3.7-01</version> -->
                            <version>2.0.7-03</version>
                            <!-- or choose a different compiler version -->
                            <!-- <version>2.1.8-01</version> -->
                        </dependency>
                    </dependencies>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <systemPropertyVariables>
                            <test.properties.path>${test.properties.path}</test.properties.path>
                            <artifact.bundle.path>${artifact.bundle.path}</artifact.bundle.path>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>gmaven-plugin</artifactId>
                    <version>1.5</version>
                    <configuration>
                        <providerSelection>2.0</providerSelection>
                    </configuration>
                    <executions>
                        <execution>
                            <id>setup-properties</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <source><![CDATA[
                               // skipTests, maven.test.skip, skipITs
                               
                               def isPropertySet(String property){
                                  return System.properties.containsKey(property) && System.properties[property] != "false"; 
                               }
                               
                               boolean skip = isPropertySet("skipTests") || isPropertySet("skipITs") || isPropertySet("maven.test.skip");
                               File testConfig = new File(project.properties["test.properties.path"]);
                               if (skip){
                                  log.info("Integration tests were skipped by flag");
                               } else if (!testConfig.exists()){
                                  log.warn("Integration tests were force skipped. Config file ${testConfig.path} is not found");
                                  skip = true;
                               } else {
                                  File testDir =  new File(project.basedir,"src/test/groovy");
                                  if (!testDir.exists()){
                                      log.info("Directory src/test/groovy is not found");
                                      skip = true;
                                  } else {
                                     skip = true;
                                  
                                     ArrayDeque<File> files = new ArrayDeque<File>();
                                     files.add(testDir);
                                     
                                     File current;
                                     l: while (!files.isEmpty()){
                                        for (File f:files.pop().listFiles()){
                                           if (f.isDirectory()){
                                              files.add(f);
                                           } else if (f.getName().endsWith("IT.groovy")){
                                              skip = false;
                                              break l;
                                           } 
                                        }
                                     }
                                     
                                     if (skip){
                                        log.info("Integration tests were skipped. No tests were found.");
                                     }
                                  }
                               }
                               
                               if (!skip){
                                  testConfig.withInputStream { stream -> 
                                     def props = new Properties()
                                     props.load(stream)
                                     
                                     String path;
                                     
                                     if (project.properties["dbmaster.build.path"] != null){
                                        path = project.properties["dbmaster.build.path"];
                                     } else {
                                        path = props["dbmaster.build.path"];
                                        project.properties["dbmaster.build.path"] = path;
                                     }
                                     if (path == null){
                                        throw new IllegalArgumentException("Property 'dbmaster.build.path' is not set in test.properties");
                                     }
                                     if (!(new File(path).exists())){
                                        throw new IllegalArgumentException("Property 'dbmaster.build.path=${path}' from '${testConfig.path}' is not valid. Correct it or use -DskipITs");
                                     }
                                     
                                     path = props["integration.test.debug"];
                                     if (project.properties["integration.test.debug"] == null && path != null){
                                        project.properties["integration.test.debug"] = path;
                                     }
                                     if (project.properties["integration.test.debug"] == null){
                                        project.properties["integration.test.debug"] = "";
                                     }
                                     
                                     path = props["testng.report.path"];
                                     if (project.properties["testng.report.path"] == null && path != null){
                                        project.properties["testng.report.path"] = path;
                                     }
                                     if (project.properties["testng.report.path"] == null){
                                        project.properties["testng.report.path"] = "testng_report";
                                     }
                                  }
                               }
                               project.properties["dbmaster.skipItTests"] = String.valueOf(skip);
                            ]]></source>
                         </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <skip>${dbmaster.skipItTests}</skip>
                                <target>
                                    <path id="testng.classpath">
                                        <pathelement location="${io.dbmaster:dbmaster-test:jar}" />
                                        <pathelement location="${org.testng:testng:jar}" />
                                        <pathelement location="${org.beanshell:bsh:jar}" />
                                        <pathelement location="${com.beust:jcommander:jar}" />
                                    </path>
                                    <!-- maven.test.classpath -->
                                    <taskdef name="dbm" classname="org.testng.TestNGrooveTask" classpathref="testng.classpath" />
                                    <!-- <echoproperties /> -->
                                    <dbm outputdir="${project.build.directory}/${testng.report.path}" classpathref="testng.classpath"
                                         haltonfailure="true" haltonFSP="true" workingDir="${dbmaster.build.path}" jvmargs="${integration.test.debug}">
                                        <!-- <classfileset dir="target/test-classes" includes="**/*IT.class" /> -->
                                        <scriptfileset dir="${basedir}/src/test/groovy">
                                            <include name="**/*IT.groovy" />
                                        </scriptfileset>
                                        <jvmarg value="-Dtest.properties.path=${test.properties.path}" />
                                        <jvmarg value="-Dartifact.bundle.path=${artifact.bundle.path}" />
                                    </dbm>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>2.3.7</version>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                            <!-- release.version -->
                            <Import-Package>io.dbmaster.api;version="[1.12.0,2.0)",!*</Import-Package>
                        </instructions>
                    </configuration>
                </plugin>
            
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-gpg-plugin</artifactId>
                    <version>1.5</version>
                    <executions>
                        <execution>
                            <id>sign-artifacts</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>sign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.6.7</version>
                    <extensions>true</extensions>
                    <configuration>
                        <serverId>ossrh</serverId>
                        <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                        <autoReleaseAfterClose>true</autoReleaseAfterClose>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>