<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright (c) 2019, RTE (http://www.rte-france.com)
    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.

-->
<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>

    <parent>
        <groupId>com.powsybl</groupId>
        <artifactId>powsybl-parent</artifactId>
        <version>7</version>
    </parent>
    <artifactId>powsybl-parent-ws</artifactId>
    <packaging>pom</packaging>

    <name>Powsybl Parent WS</name>
    <description>Powsybl Parent for Web Services</description>

    <properties>
        <maven.jib.version>2.7.0</maven.jib.version>
        <maven.spring-boot.version>2.4.7</maven.spring-boot.version>
        <maven.git-commit-id.version>4.0.3</maven.git-commit-id.version>

        <jib.from.image>powsybl/java:1.0.0</jib.from.image>

        <!--
        SPRING_PROFILES_ACTIVE: remove extra profiles. This is used for example to remove the "local" profile
        which allows running apps in an IDE and have it connect to localhost for its dependencies

        SPRING_CONFIG_LOCATION: change the working dir from spring config location to /config. The working dir
        is RW in /home, while the config is RO in /config. Keep the other spring default locations.

        NOTE: using a single line without whitespace to avoid messing up the environment
        -->
        <jib.container.environment>SPRING_PROFILES_ACTIVE=default,SPRING_CONFIG_LOCATION=optional:classpath:/;optional:classpath:/config/;optional:file:/config/;optional:file:/config/*/</jib.container.environment>

        <!-- copied from spring-boot-starter-parent -->
        <maven.resource.delimiter>@</maven.resource.delimiter>
        <maven.compiler.parameters>true</maven.compiler.parameters>

        <git-id.offline>true</git-id.offline>
        <git-id.failOnNoGitDirectory>false</git-id.failOnNoGitDirectory>
        <git-id.failOnUnableToExtractRepoInfo>false</git-id.failOnUnableToExtractRepoInfo>

        <!-- for liquibase plugin -->
        <liquibase-core.version>3.10.3</liquibase-core.version>
        <liquibase-hibernate.version>3.10.2</liquibase-hibernate.version> <!-- the same as liquibase-core does not exists 3.10.2 is the closest -->
        <validation-api.version>2.0.1.Final</validation-api.version>
        <liquibase-diff.outputFile>src/main/resources/db/changelog/changesets/changelog_${maven.build.timestamp}.xml</liquibase-diff.outputFile>
        <liquibase.username>sa</liquibase.username>
        <liquibase.databaseDriver>org.h2.Driver</liquibase.databaseDriver>
        <liquibase.databaseUrl>jdbc:h2:./target/dummyDB</liquibase.databaseUrl>
        <liquibase.referenceDatabaseUrl>hibernate:spring:${liquibase-hibernate-package}?dialect=org.hibernate.dialect.PostgreSQLDialect&amp;hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy</liquibase.referenceDatabaseUrl>
        <liquibase.referenceDatabaseDriver>liquibase.ext.hibernate.database.connection.HibernateDriver</liquibase.referenceDatabaseDriver>
        <liquibase-hibernate-package>PROPERTY_TO_BE_DEFINED_IN_PROJECTS</liquibase-hibernate-package>
    </properties>


    <build>
        <!-- copied from spring-boot-starter-parent -->
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/application*.yaml</include>
                    <include>**/application*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/application*.yml</exclude>
                    <exclude>**/application*.yaml</exclude>
                    <exclude>**/application*.properties</exclude>
                </excludes>
            </resource>
        </resources>

        <!--
          by default jib uses artifactId:artifactVersion for its to.image.name
          For example, it would use "powsybl-case-server". We want "powsybl/case-server".
          we also want to omit the version to use latest for snapshots but not for releases
        -->
        <!-- Note: this is executed even for this pom but does nothing, the value is not inherited by child projects. so we don't care. -->
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <!--disable parent pom attaching of groovy-javadoc. Fixes the following error during release:
                        gpg: impossible d'ouvrir « /tmp/powsybl-parent/powsybl-parent/powsybl-parent-ws/target/powsybl-parent-ws-5-groovydoc.jar » : Aucun fichier ou dossier de ce type
                        gpg: signing failed: erreur d'ouverture de fichier
                        -->
                        <id>attach-artifact</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                    <goals>
                        <goal>bsh-property</goal>
                    </goals>
                    <configuration>
                        <properties>
                            <property>jib.to.image</property>
                        </properties>
                        <source>
String useJibDefaultName =
    session.getUserProperties().getProperty("powsybl.docker.useJibDefaultName",
    project.getProperties().getProperty("powsybl.docker.useJibDefaultName",
    "false"));
String image =
    session.getUserProperties().getProperty("image",
    project.getProperties().getProperty("image"));
String jibtoimage =
    session.getUserProperties().getProperty("jib.to.image",
    project.getProperties().getProperty("jib.to.image"));
String finalName;
if ("false".equals(useJibDefaultName) @and null == image @and null == jibtoimage) {
    String name;
    String keepArtifactName =
        session.getUserProperties().getProperty("powsybl.docker.keepArtifactName",
        project.getProperties().getProperty("powsybl.docker.keepArtifactName",
        "false"));
    if ("false".equals(keepArtifactName)) {
        name = project.artifactId.replaceFirst("-","/");
    } else {
        name = project.artifactId; //the default
    }
    String keepSnapshotVersion =
        session.getUserProperties().getProperty("powsybl.docker.keepSnapshotVersion",
        project.getProperties().getProperty("powsybl.docker.keepSnapshotVersion",
        "false"));
    if (!"false".equals(keepSnapshotVersion) @or !project.version.endsWith("-SNAPSHOT")) {
        name = name + ":" + project.version;
    }
    print("Setting jib.to.image to " + name);
    finalName = name;
} else {
    // we have to set this to something otherwise we get an exception
    print("Not setting jib.to.image because powsybl.docker.useJibDefaultName or jib.to.image or image is set.");
    finalName = jibtoimage;
}
jib.to.image = finalName;
                        </source>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase-core.version}</version>
                <configuration>
                    <changeLogFile>src/main/resources/db/changelog/db.changelog-master.yaml</changeLogFile>
                    <diffChangeLogFile>${liquibase-diff.outputFile}</diffChangeLogFile>
                    <logging>info</logging>
                    <referenceUrl>${liquibase.referenceDatabaseUrl}</referenceUrl>
                    <referenceDriver>${liquibase.referenceDatabaseDriver}</referenceDriver>
                    <username>${liquibase.username}</username>
                    <url>${liquibase.databaseUrl}</url>
                    <driver>${liquibase.databaseDriver}</driver>
                </configuration>
                <!-- Liquibase dependencies-->
                <dependencies>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-hibernate5</artifactId>
                        <version>${liquibase-hibernate.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                        <version>${maven.spring-boot.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>${validation-api.version}</version>
                    </dependency>
                </dependencies>

            </plugin>

        </plugins>


        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                    <artifactId>jib-maven-plugin</artifactId>
                    <version>${maven.jib.version}</version>
                    <configuration>
                        <from>
                            <image>${jib.from.image}</image>
                        </from>
                    </configuration>
                </plugin>

                <!-- copied from spring-boot-starter-parent -->
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <parameters>${maven.compiler.parameters}</parameters>
                    </configuration>
                </plugin>
                <!-- copied from spring-boot-starter-parent -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <delimiters>
                            <delimiter>${maven.resource.delimiter}</delimiter>
                        </delimiters>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${maven.spring-boot.version}</version>
                    <executions>
                        <execution>
                            <id>build-info</id>
                            <goals>
                                <goal>build-info</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- copied from spring-boot-starter-parent -->
                <plugin>
                    <groupId>pl.project13.maven</groupId>
                    <artifactId>git-commit-id-plugin</artifactId>
                    <version>${maven.git-commit-id.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>revision</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <offline>${git-id.offline}</offline>
                        <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
                        <generateGitPropertiesFile>true</generateGitPropertiesFile>
                        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
                        <failOnNoGitDirectory>${git-id.failOnNoGitDirectory}</failOnNoGitDirectory>
                        <failOnUnableToExtractRepoInfo>${git-id.failOnUnableToExtractRepoInfo}</failOnUnableToExtractRepoInfo>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <activation>
                <property>
                    <name>powsybl.spring-boot.skip</name>
                    <value>!true</value>
                </property>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>repackage</id>
                                    <goals>
                                        <goal>repackage</goal>
                                    </goals>
                                    <configuration>
                                        <attach>false</attach>
                                        <classifier>exec</classifier>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

        <profile>
            <id>jibpackagedocker</id>
            <activation>
                <property>
                    <name>powsybl.docker.package</name>
                    <value>true</value>
                </property>
            </activation>

            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.google.cloud.tools</groupId>
                            <artifactId>jib-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>jibDockerPackage</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>buildTar</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

        <profile>
            <id>jibinstalldocker</id>
            <activation>
                <property>
                    <name>powsybl.docker.install</name>
                    <value>true</value>
                </property>
            </activation>

            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.google.cloud.tools</groupId>
                            <artifactId>jib-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>jibDockerBuild</id>
                                    <phase>install</phase>
                                    <goals>
                                        <goal>dockerBuild</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>

        <profile>
            <id>jibdeploydocker</id>
            <activation>
                <property>
                    <name>powsybl.docker.deploy</name>
                    <value>true</value>
                </property>
            </activation>

            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>com.google.cloud.tools</groupId>
                            <artifactId>jib-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>jibDockerDeploy</id>
                                    <phase>deploy</phase>
                                    <goals>
                                        <goal>build</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
</project>
