<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.modeshape</groupId>
        <artifactId>modeshape-parent</artifactId>
        <version>3.0.0.Beta4</version>
        <relativePath>../modeshape-parent/pom.xml</relativePath>
    </parent>

    <!-- The groupId and version values are inherited from parent -->
    <artifactId>modeshape-jdbc</artifactId>
    <packaging>jar</packaging>
    <name>ModeShape JDBC Driver</name>
    <description>JDBC driver to allow clients to use JCR-SQL2 to query a local or remote ModeShape JCR repository.</description>
    <url>http://www.modeshape.org</url>
    <!--
      Define the dependencies. Note that all version and scopes default to those defined in the dependencyManagement section of the
      parent pom.
    -->
    <dependencies>
        <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
        </dependency>
        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-jdbc-local</artifactId>
        </dependency>
        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-web-jcr-rest-client</artifactId>
        </dependency>
        <!--
        Testing (note the scope)
        -->
        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-common</artifactId>
            <type>test-jar</type>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-jcr</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-jcr</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.infinispan</groupId>
            <artifactId>infinispan-core</artifactId>
            <version>${infinispan.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.modeshape</groupId>
            <artifactId>modeshape-jdbc-local</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
        </dependency>
        <!--
          PicketBox (JAAS implementation used in test cases)
          -->
        <dependency>
            <groupId>org.picketbox</groupId>
            <artifactId>picketbox-bare</artifactId>
        </dependency>
        <!--
        Logging (require SLF4J API for compiling, but use Log4J and its SLF4J binding for testing)
        -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                    <include>**/*</include>
                </includes>
            </resource>
            <!-- Apply the properties set in the POM to the resource files -->
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/JdbcI18n.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <executions>
                    <!--During the default test phase, excluded the Http tests which will run during the integration phase-->
                    <execution>
                        <!--The id of the default test phase of the maven plugin (do not change) -->
                        <id>default-test</id>
                        <configuration>
                            <excludes>
                                <exclude>**/*Http*Test.java</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                    <!--Execute the Http tests during the integration tests, in a running cargo container -->
                    <execution>
                        <id>integration-tests</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/*Http*Test.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--Configuration inherited from parent pom -->
            <plugin>
                <groupId>org.codehaus.cargo</groupId>
                <artifactId>cargo-maven2-plugin</artifactId>
                <configuration>
                    <configuration>
                        <deployables>
                            <!-- application to deploy (brought in as a transitive dependency of modeshape-rest-client -->
                            <deployable>
                                <groupId>org.modeshape</groupId>
                                <artifactId>modeshape-web-jcr-rest-war</artifactId>
                                <type>war</type>
                                <properties>
                                    <context>modeshape</context>
                                </properties>
                            </deployable>
                        </deployables>
                    </configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>assembly</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <executions>
                            <!--Creates a temporary jar which will be unpacked and processed by ANT (see below) -->
                            <execution>
                                <id>default</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <attach>false</attach>
                                    <descriptors>
                                        <descriptor>src/assembly/kit.xml</descriptor>
                                    </descriptors>
                                    <attach>false</attach>
                                </configuration>
                            </execution>
                            <!--Creates an assembly which will be included in the binary distribution -->
                            <execution>
                                <id>module-with-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <descriptorRefs>
                                        <descriptorRef>module-with-dependencies</descriptorRef>
                                    </descriptorRefs>
                                    <archive>
                                        <manifest>
                                            <addClasspath>true</addClasspath>
                                        </manifest>
                                    </archive>
                                    <attach>true</attach>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!--
                       The following ant tasks are used to remove any duplicates from the modeshape-jdbc jar.
                       Unfortunately, maven has a bug in this regard and there's no way to resolve duplicates using
                       the 2.0.x version.  So, by using ant, the duplicate files (i.e., META-INF/..)  can
                       be eliminated.

                       Note:  this is all changing in future releases.

                       test dups:  unzip -l modeshape-jdbc-${project.version}-http-jar-with-dependencies.jar | awk '{print $NF}' | sort | uniq -c | grep -v "1 "
                    -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>repackage-client-jar</id>
                                <phase>package</phase>
                                <configuration>
                                    <tasks>
                                        <unzip src="target/modeshape-jdbc-${project.version}-jar-with-dependencies.jar"
                                               dest="target/tempjars"/>

                                        <unzip src="target/tempjars/jaxrs-api-${resteasy.version}.jar" dest="target/temploc"/>
                                        <unzip src="target/tempjars/jcr-${jcr.version}.jar" dest="target/temploc"/>

                                        <unzip src="target/tempjars/commons-codec-1.4.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE.txt"
                                              tofile="target/temploc/META-INF/LICENSE_commons-codec.txt"/>

                                        <unzip src="target/tempjars/commons-logging-1.1.1.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE"
                                              tofile="target/temploc/META-INF/LICENSE_commons-logging.txt"/>
                                        <move file="target/temploc/META-INF/NOTICE"
                                              tofile="target/temploc/META-INF/NOTICE_commons-logging.txt"/>

                                        <unzip src="target/tempjars/joda-time-${joda.time.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE.txt"
                                              tofile="target/temploc/META-INF/LICENSE_joda-time.txt"/>
                                        <move file="target/temploc/META-INF/NOTICE.txt"
                                              tofile="target/temploc/META-INF/NOTICE_joda-time.txt"/>

                                        <unzip src="target/tempjars/httpclient-${httpclient.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE.txt"
                                              tofile="target/temploc/META-INF/LICENSE_httpclient.txt"/>
                                        <move file="target/temploc/META-INF/NOTICE.txt"
                                              tofile="target/temploc/META-INF/NOTICE_httpclient.txt"/>

                                        <unzip src="target/tempjars/httpcore-${httpclient.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE.txt"
                                              tofile="target/temploc/META-INF/LICENSE_httpcore.txt"/>
                                        <move file="target/temploc/META-INF/NOTICE.txt"
                                              tofile="target/temploc/META-INF/NOTICE_httpcore.txt"/>

                                        <unzip src="target/tempjars/jettison-${jettison.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/LICENSE"
                                              tofile="target/temploc/META-INF/LICENSE_jettison.txt"/>

                                        <unzip src="target/tempjars/resteasy-jaxrs-${resteasy.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/services/javax.ws.rs.ext.Providers"
                                              tofile="target/tempproviders/jaxrs_providers"/>

                                        <unzip src="target/tempjars/resteasy-jaxb-provider-${resteasy.version}.jar" dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/services/javax.ws.rs.ext.Providers"
                                              tofile="target/tempproviders/jaxb_providers"/>

                                        <unzip src="target/tempjars/resteasy-jettison-provider-${resteasy.version}.jar"
                                               dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/services/javax.ws.rs.ext.Providers"
                                              tofile="target/tempproviders/jet_providers"/>

                                        <concat destfile="target/temploc/META-INF/services/javax.ws.rs.ext.Providers">
                                            <fileset file="target/tempproviders/jaxrs_providers"/>
                                            <fileset file="target/tempproviders/jaxb_providers"/>
                                            <fileset file="target/tempproviders/jet_providers"/>
                                        </concat>

                                        <unzip src="target/tempjars/modeshape-common-${project.version}.jar"
                                               dest="target/temploc"/>
                                        <unzip src="target/tempjars/modeshape-jcr-api-${project.version}.jar"
                                               dest="target/temploc"/>
                                        <unzip src="target/tempjars/modeshape-web-jcr-rest-client-${project.version}.jar"
                                               dest="target/temploc"/>

                                        <move file="target/tempjars/META-INF/services/java.sql.Driver"
                                              tofile="target/tempdrivers/jdbc_drivers"/>
                                        <unzip src="target/tempjars/modeshape-jdbc-local-${project.version}.jar"
                                               dest="target/temploc"/>
                                        <move file="target/temploc/META-INF/services/java.sql.Driver"
                                              tofile="target/tempdrivers/jdbc_local_drivers"/>

                                        <copy todir="target/temploc/org">
                                            <fileset dir="target/tempjars/org"/>
                                        </copy>

                                        <concat destfile="target/temploc/META-INF/services/java.sql.Driver" fixlastline="yes">
                                            <fileset file="target/tempdrivers/jdbc_drivers"/>
                                            <fileset file="target/tempdrivers/jdbc_local_drivers"/>
                                        </concat>

                                        <delete file="target/modeshape-jdbc-${project.version}-jar-with-dependencies.jar"/>
                                        <jar destfile="target/modeshape-jdbc-${project.version}-jar-with-dependencies.jar"
                                             basedir="target/temploc">
                                            <manifest>
                                                <attribute name="Implementation-URL" value="${project.url}"/>
                                                <attribute name="Java-Version" value="${java.version}"/>
                                                <attribute name="Java-Vendor" value="${java.vendor}"/>
                                                <attribute name="Os-Name" value="${os.name}"/>
                                                <attribute name="Os-Arch" value="${os.arch}"/>
                                                <attribute name="Os-Version" value="${os.version}"/>
                                                <attribute name="Scm-Url" value="${project.scm.url}"/>
                                                <attribute name="Build-Timestamp" value="${build.datetime}"/>
                                            </manifest>
                                        </jar>
                                        <delete dir="target/tempjars"/>
                                        <delete dir="target/temploc"/>
                                        <delete dir="target/tempproviders"/>
                                        <delete dir="target/tempdrivers"/>
                                    </tasks>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!--Install the re-packaged assembly file in the repo, so it can be picked up by the distribution module-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-modified-jdbc-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                                <configuration>
                                    <file>${project.build.directory}/modeshape-jdbc-${project.version}-jar-with-dependencies.jar</file>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <version>${project.version}</version>
                                    <classifier>jar-with-dependencies</classifier>
                                    <packaging>jar</packaging>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>