<?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"/>

        <!-- TODO: Talk to JDBC RA guys to use distribution-fragment packaging. - Kohsuke -->
	<!--resolveArtifact artifactId="jdbc-ra" groupId="org.glassfish.connectors.jdbc-ra.jdbc-ra-distribution" 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 -->
                            <scope level="compile,runtime">
                                <excludeArtifactsTransitively>
                                    <artifact artifactId="@{artifactId}" />
                                </excludeArtifactsTransitively>
                            </scope>
                        </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-all" />
    </target>

    <!--
        build the final zip file and attach that to Maven
    -->
    <macrodef name="build-zip">
        <attribute name="zipfile" />
        <attribute name="base" />
        <sequential>
            <!-- find all fragments -->
            <resolveAll todir="target/fragments">
                <fullGraph/>
                <!-- now treat the full graph as the list and then do simple list filtering -->
                <packaging value="distribution-fragment"/>
            </resolveAll>
            <unzip dest="target/stage">
                <patternset>
                    <exclude name="META-INF/**"/>
                </patternset>
                <fileset dir="target/fragments" includes="*.zip"/>
            </unzip>

            <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">
            <!-- first filter out all the distribution fragments and their transitive dependencies -->
            <packaging not="distribution-fragment" />
            <!-- then get rid of all the dependencies that are in the webtier -->
            <removeSpecificArtifacts>
                <scope level="compile,runtime"/>
                <artifact artifactId="webtier-all"/>
            </removeSpecificArtifacts>
        </resolveAll>

        <delete dir="target/stage/glassfish/modules" includes="*.zip"/>

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