<?xml version="1.0" encoding="iso-8859-1"?>
<!--
 The contents of this file are subject to the terms
 of the Common Development and Distribution License
 (the "License").  You may not use this file except
 in compliance with the License.

 You can obtain a copy of the license at
 glassfish/bootstrap/legal/CDDLv1.0.txt or
 https://glassfish.dev.java.net/public/CDDLv1.0.html.
 See the License for the specific language governing
 permissions and limitations under the License.

 When distributing Covered Code, include this CDDL
 HEADER in each file and include the License file at
 glassfish/bootstrap/legal/CDDLv1.0.txt.  If applicable,
 add the following below this CDDL HEADER, with the
 fields enclosed by brackets "[]" replaced with your
 own identifying information: Portions Copyright [yyyy]
 [name of copyright owner]
-->
<project name="nucleus-base bundle creation" default="create.bundle" basedir=".">
    <!--
        Perform the part of bundle creation that doesn't involve in module jar staging.
        This part will be reused by other bundles that build on the web bundle.
    -->
    <target name="web.prepare">
        <mkdir dir="target/stage"/>
        <resolveArtifact artifactId="javadb" property="javadb.jar"/>
        <unjar src="${javadb.jar}" dest="target"/>
        <!-- Note: the current version of the javadb jar file contains a single zip file.
             As a result, we currently hard code the <unzip/> task to handle this file. -->
        <unzip dest="target/stage/glassfish">
            <fileset dir="target">
                <include name="*javadb*.zip"/>
            </fileset>
            <patternset>
                <exclude name="javadb/demo/**"/>
                <exclude name="javadb/bin/**"/>
                <exclude name="javadb/3RDPARTY.zip"/>
            </patternset>
        </unzip>

        <resolveArtifact  artifactId="jdbc-ra" groupId="org.glassfish.connectors" property="jdbc-ra.zip"/>

                <!-- System applications like resource-adapters are stored in lib/install/applications -->
                <mkdir dir="target/stage/glassfish/lib/install/applications"/>
                <!-- install jdbc system resource adapters -->
                <unzip src="${jdbc-ra.zip}" dest="target/stage/glassfish/lib/install/applications"/>

        <resolveArtifact artifactId="nucleus-base" property="nucleus-base.zip"/>
    </target>

    <!--
        Macro for defining a module set rooted at the given node.
    -->
    <macrodef name="moduleDef">
        <!--
            The module subgraph will be placed under ID "<name>-modules",
            and the module jars are placed under "modules/<name>" sub directory.
        -->
        <attribute name="name"/>
        <!--
            ArtifactId of the root module of this module set.
        -->
        <attribute name="artifactId" />
        <sequential>
            <!--
              This filtering definition obtains a set of HK2 modules that are retained by webtier
              (that is, set of modules that won't be in the dependency list if webtier is not a part of the dependency;
              IOW, a dependency only reachable through webtier) and all its non-module dependencies
              (that is, normal jars.)

              The formula to compute this is non-trivial. We first compute a subgraph X which represents
              nodes that we *don't* want. That is, all HK2 modules that are reachable without going through
              webtier. I use <packaging not="..."/> so that it includes transitive dependencies
              through distribution zips. Also note that all non-module normal jars need to be excluded from
              X because if a module that ends up in /web depends on normal jars, those jars also need to be
              in /web.

              We then finally compute the set of modules and jars that are not in X by one more subtraction.
            -->
            <graphDef id="@{name}-modules">
                <scope level="compile,runtime">
                    <subtract>
                        <subGraph artifactId="@{artifactId}" />
                        <packaging not="jar"><!-- = subgraph X -->
                            <excludeArtifactsTransitively>
                                <artifact artifactId="@{artifactId}" />
                            </excludeArtifactsTransitively>
                        </packaging>
                    </subtract>
                </scope>
            </graphDef>

            <resolveAll todir="target/stage/glassfish/modules/@{name}">
                <graphRef refid="@{name}-modules"/>
            </resolveAll>
        </sequential>
    </macrodef>

    <!--
        Define a dependency graph for the web modules,
        and place the web modules into the modules/ dir.

        This part will be reused by other bundles that build on the web module.
    -->
    <target name="web.moduleDef">
        <moduleDef name="web" artifactId="webtier" />
    </target>

    <!--
        build the final zip file and attach that to Maven
    -->
    <macrodef name="build-zip">
        <attribute name="zipfile" />
        <attribute name="base" />
        <sequential>
            <zip file="@{zipfile}" basedir="target/stage">
                <zipgroupfileset file="@{base}"/>
            </zip>
            <attachArtifact file="@{zipfile}"/>
        </sequential>
    </macrodef>

    <target name="create.bundle" depends="web.prepare, web.moduleDef">
        <resolveAll todir="target/stage/glassfish/modules">
            <excludeArtifactsTransitively>
                <scope level="compile,runtime"/>
                <artifact artifactId="webtier" />
                <artifact artifactId="gf-ejb-connector" />
            </excludeArtifactsTransitively>
        </resolveAll>
       
        <delete dir="target/stage/glassfish/modules" includes="*.zip, javadb*.*" />

        <graphDef id="generate-picture">
            <visualize file="modules.png">
                <scope level="compile,runtime" />
                <subGraph color="red">
                    <graphRef refid="web-modules"/>
                </subGraph>
            </visualize>
        </graphDef>

        <build-zip zipfile="target/web.zip" base="${nucleus-base.zip}" />

        <!-- this build script will be reused elsewhere -->
        <attachArtifact file="build.xml" classifier="build" />
    </target>
</project>
