<?xml version="1.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>

    <name>SBL - Common Maven configuration</name>
    <description>
        This is a common root POM for many NAV projects. It defines versions of core plugins,
        configuration of code quality metrics, CI server URLs, and distribution management
        for artifact deploys.
    </description>
    <url>https://github.com/navikt/common-java-modules</url>

    <groupId>no.nav.common</groupId>
    <artifactId>mor-pom</artifactId>
    <version>2.2020.05.28_13.26-8e581baa1ccb</version>
    <packaging>pom</packaging>

    <licenses>
        <license>
            <name>MIT License</name>
            <url>https://opensource.org/licenses/MIT</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <organization>NAV (Arbeids- og velferdsdirektoratet) - The Norwegian Labour and Welfare Administration
            </organization>
            <organizationUrl>https://www.nav.no</organizationUrl>
        </developer>
    </developers>

    <modules>
        <module>abac</module>
        <module>auth</module>
        <module>bom</module>
        <module>client</module>
        <module>cxf</module>
        <module>feature-toggle</module>
        <module>json</module>
        <module>log</module>
        <module>leader-election</module>
        <module>metrics</module>
        <module>rest</module>
        <module>sts</module>
        <module>health</module>
        <module>test</module>
        <module>types</module>
        <module>util</module>
        <module>yaml</module>
        <module>nais</module>
    </modules>

    <prerequisites>
        <maven>3.6.1</maven>
    </prerequisites>

    <properties>

        <!-- unntak fra bom-versjonering siden vi gjør oppsett i denne pommen -->
        <junit.jupiter.version>5.1.1</junit.jupiter.version>
        <junit.platform.version>1.1.1</junit.platform.version>
        <junit.vintage.version>5.1.1</junit.vintage.version>


        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <checkstyle.config.location>codestyle/nav-mod/checkstyle.xml</checkstyle.config.location>
        <pmd.config.location>codestyle/nav-mod/pmd.xml</pmd.config.location>
        <findbugs.config.location>codestyle/nav-mod/findbugs.xml</findbugs.config.location>

        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.jacoco.reportPaths>${user.dir}/target/jacoco.exec</sonar.jacoco.reportPaths>
    </properties>

    <dependencies>
        <!-- tilgjengeliggjør riktig versjon i test-scope slik at junit5-tester kjører i intellij -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>

        <!--
        slik at vi unngår "groups/excludedGroups require TestNG or JUnit48+ on project test classpath" for prosjekter
        som kun bruker junit5
         -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit.vintage.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>

                <!-- CORE PLUGINS -->

                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.4</version>
                </plugin>
                <plugin>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.22.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <archive>
                            <manifest>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <attachClasses>true</attachClasses>
                        <archive>
                            <manifest>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <version>7</version>
                        <defaultLibBundleDir>lib</defaultLibBundleDir>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <archive>
                            <manifest>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>

                <plugin>
                    <artifactId>maven-release-plugin</artifactId>
                    <version>2.5.3</version>
                    <configuration>
                        <goals>deploy</goals>
                        <autoVersionSubmodules>true</autoVersionSubmodules>
                    </configuration>
                </plugin>

                <!-- QUALITY ASSURANCE PLUGINS -->

                <plugin>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>3.1.1</version>
                    <configuration>
                        <configLocation>${checkstyle.config.location}</configLocation>
                        <violationSeverity>info</violationSeverity>
                        <logViolationsToConsole>true</logViolationsToConsole>
                        <linkXRef>false</linkXRef>
                        <failOnViolation>false</failOnViolation>
                        <includeTestSourceDirectory>true</includeTestSourceDirectory>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>no.nav</groupId>
                            <artifactId>java-codestyle</artifactId>
                            <version>1.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>3.13.0</version>
                    <configuration>
                        <targetJdk>${java.version}</targetJdk>
                        <rulesets>
                            <ruleset>${pmd.config.location}</ruleset>
                        </rulesets>
                        <minimumTokens>40</minimumTokens>
                        <aggregate>true</aggregate>
                        <linkXRef>false</linkXRef>
                        <includeTests>true</includeTests>
                        <failOnViolation>false</failOnViolation>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>no.nav</groupId>
                            <artifactId>java-codestyle</artifactId>
                            <version>1.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>findbugs-maven-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <includeFilterFile>${findbugs.config.location}</includeFilterFile>
                        <findbugsXmlOutput>true</findbugsXmlOutput>
                        <failOnError>true</failOnError>
                        <xmlOutput>true</xmlOutput>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>no.nav</groupId>
                            <artifactId>java-codestyle</artifactId>
                            <version>1.0</version>
                        </dependency>
                    </dependencies>
                </plugin>

                <!-- AUXILIARY PLUGINS -->

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>versions-maven-plugin</artifactId>
                    <version>2.5</version>
                </plugin>
                <plugin>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                        <additionalConfig>
                            <file>
                                <name>.settings/org.eclipse.core.resources.prefs</name>
                                <content>
                                    <![CDATA[eclipse.preferences.version=1
                                    <![CDATA[eclipse.preferences.version=1
                                    encoding/<project>=${project.build.sourceEncoding}]]>
                                </content>
                            </file>
                        </additionalConfig>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <version>1.3.1</version>
                    <executions>
                        <execution>
                            <id>validate-dependencies</id>
                            <goals>
                                <goal>enforce</goal>
                            </goals>
                            <configuration>
                                <rules>
                                    <dependencyConvergence/>
                                </rules>
                                <fail>true</fail>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-dependency-analyzer</artifactId>
                        <version>1.11.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>analyze</id>
                        <goals>
                            <goal>analyze-only</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>analyze-pom</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>analyze-dep-mgt</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <failBuild>true</failBuild>
                    <ignoreDirect>false</ignoreDirect>
                    <ignoredUsedUndeclaredDependencies>
                        <!--
                        This deactivates the requirement of declaring every used dependency in a project's pom.
                        We are omitting this, even though it its considered best practice, because:

                        - enforced management of transitive dependencies increases the likelihood of management errors
                          that may cause diamonds of death, which often results in nasty runtime errors. Even having decent
                          test coverage over the affected dependencies may not detect such errors due to the indeterministic
                          nature of classloading in different contexts (different OSes, JVMs, build tools, packaging formats)

                        - the main motivation of the requirement seems to be avoiding compile errors when a change of a
                          direct dependency causes a change in transitive dependencies. A transitive dependency might for
                          instance be eliminated and must be added as a direct dependency instead.

                        Since compile errors generally are easier to detect and fix than runtime error, we should try to minimize
                        the risk of having runtime errors by being allowed to use a dependency transitively when it makes sense.

                        -->
                        <ignoredUsedUndeclaredDependency>*</ignoredUsedUndeclaredDependency>
                    </ignoredUsedUndeclaredDependencies>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <configuration>
                    <destFile>${sonar.jacoco.reportPaths}</destFile>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jacoco.agent.argLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>

                    <!-- Ellers vil maven kun kjøre tester som slutter på 'Test' (!) -->
                    <includes>
                        <include>*</include>
                    </includes>

                    <!--
                     NB:
                     @Category (junit4) fungerer ikke i kombinasjon med junit-platform-surefire-provider
                     Løsningen er å skrive smoketester i junit5 med @Tag
                     -->
                    <properties>
                        <excludeTags>smoketest</excludeTags>
                    </properties>

                    <!--suppress MavenModelInspection -->
                    <argLine>${jacoco.agent.argLine} -Xmx2g</argLine>

                    <!--
                    Å sette `reuseFork` til `true` vil føre til at surefire fyrer opp en separat JVM for hver
                    eneste testklasse.

                    Dette fører til ekstremt treg eksekvering av tester (i ett tilfelle 4-5 min kontra
                    20-30 sekunder om satt til `false`)

                    Merk at å sette denne til `true` krever at man skriver tester som er godt isolerte fra hverandre.
                    F.eks. kan det føre til problemer å sette statiske eller globale felter i klasser
                    som gjenbrukes på tvers av testmetoder. Sørg for å lage en ny instanse av slike felter for hver
                    testmetode ved bruk av @BeforeEach (JUnit 5) eller lignende for å unngå tilstandsfulle testklasser.

                    Se følgende link for flere detaljer:

                    https://maven.apache.org/surefire/maven-surefire-plugin/examples/ fork-options-and-parallel-execution.html

                    -->
                    <reuseForks>true</reuseForks>

                    <forkCount>1C</forkCount> <!-- fyr opp 1 JVM per cpu-kjerne -->

                    <argLine>-Xmx1024m</argLine> <!-- Gi nok minne for å unngå terminerende JVM-er -->

                    <!-- fjerner edge-caser ved nøstede classloadere, som f.eks. hvis man tester med jetty -->
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-scm-plugin</artifactId>
                <version>1.9.4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                    <useReleaseProfile>true</useReleaseProfile>
                    <releaseProfiles>release</releaseProfiles>
                    <goals>deploy</goals>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.8</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>deploy</phase>
                        <!-- By default, this is the phase deploy goal will bind to -->
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                    <serverId>ossrh</serverId>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>3.1.1</version>
                        <configuration>
                            <source>${java.version}</source>
                        </configuration>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                                <configuration>
                                    <!-- Prevent `gpg` from using pinentry programs -->
                                    <gpgArguments>
                                        <arg>--pinentry-mode</arg>
                                        <arg>loopback</arg>
                                    </gpgArguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <scm>
        <developerConnection>scm:git:git@github.com:navikt/common-java-modules.git</developerConnection>
        <connection>scm:git:git@github.com:navikt/common-java-modules.git</connection>
        <url>https://github.com/navikt/common-java-modules</url>
        <tag>HEAD</tag>
    </scm>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
</project>
