<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<project default="compile" name="cyclonedx">

  <!-- easy way to override properties -->
  <property file="build.properties"/>

  <target name="ready-to-package" depends="common.ready-to-package">
    <copy todir="${build.classes}/org/apache/ant/cyclonedx">
      <file file="version.properties"/>
    </copy>
  </target>

  <target name="define-cyclonedx-components" depends="antlib">
    <typedef uri="antlib:org.apache.ant.cyclonedx"
      resource="org/apache/ant/cyclonedx/antlib.xml">
      <classpath>
        <path refid="classpath.compile"/>
        <pathelement location="${jarname}"/>
      </classpath>
    </typedef>
    <!-- common definitions for SBOMs -->
    <cdx:organization
        name="Apache Ant Project Management Committee"
        id="ant-pmc"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <url url="https://ant.apache.org/"/>
    </cdx:organization>
    <cdx:license
        licenseId="Apache-2.0"
        id="apache-2"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <url url="https://www.apache.org/licenses/LICENSE-2.0.txt"/>
    </cdx:license>
    <cdx:externalreferenceset
        id="ant-common-refs"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <externalReference
          type="LICENSE"
          url="https://www.apache.org/licenses/LICENSE-2.0.txt"/>
      <externalReference
          type="MAILING_LIST"
          url="https://ant.apache.org/mail.html"/>
      <externalReference
          type="SECURITY_CONTACT"
          url="https://www.apache.org/security/"/>
    </cdx:externalreferenceset>
    <cdx:externalreferenceset
        id="antlib-ext-refs"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <externalReference
          type="VCS"
          url="https://gitbox.apache.org/repos/asf/ant-antlibs-cyclonedx.git"/>
      <externalReference
          type="BUILD_SYSTEM"
          url="https://ci-builds.apache.org/job/Ant/job/CycloneDX%20Antlib/"/>
      <externalReference
          type="ISSUE_TRACKER"
          url="https://bz.apache.org/bugzilla/buglist.cgi?component=CycloneDX%20Antlib&amp;product=Ant"/>
      <externalReference
          type="WEBSITE"
          url="https://ant.apache.org/antlibs/cyclonedx/"/>
      <externalReference
          type="DISTRIBUTION"
          url="https://ant.apache.org/antlibs/bindownload.cgi"/>
      <externalReference
          type="SOURCE_DISTRIBUTION"
          url="https://ant.apache.org/antlibs/srcdownload.cgi"/>
    </cdx:externalreferenceset>
    <cdx:externalreferenceset
        id="ant-ext-refs"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <externalReference
          type="VCS"
          url="https://gitbox.apache.org/repos/asf/ant.git"/>
      <externalReference
          type="BUILD_SYSTEM"
          url="https://ci-builds.apache.org/job/Ant/"/>
      <externalReference
          type="ISSUE_TRACKER"
          url="https://bz.apache.org/bugzilla/buglist.cgi?product=Ant"/>
      <externalReference
          type="WEBSITE"
          url="https://ant.apache.org/"/>
      <externalReference
          type="DISTRIBUTION"
          url="https://ant.apache.org/bindownload.cgi"/>
      <externalReference
          type="SOURCE_DISTRIBUTION"
          url="https://ant.apache.org/srcdownload.cgi"/>
    </cdx:externalreferenceset>
  </target>

  <target name="create-antlib-sbom" depends="define-cyclonedx-components">
    <uptodate property="antlib-sbom-ok"
              targetfile="${build.lib}/${artifact.stub}-cyclonedx.json">
      <srcresources>
        <file file="${jarname}"/>
        <file file="ivy.xml"/>
        <file file="build.xml"/>
      </srcresources>
    </uptodate>
    <cdx:componentbom
        bomName="${artifact.stub}-cyclonedx"
        outputdirectory="${build.lib}"
        format="all"
        useComponentSupplier="true"
        useComponentManufacturer="true"
        unless:set="antlib-sbom-ok"
        xmlns:unless="ant:unless"
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
      <component
          name="${artifact.name}"
          group="org.apache.ant"
          version="${artifact.version}"
          description="Apache CycloneDX Antlib"
          publisher="The Apache Software Foundation"
          supplierIsManufacturer="true">
        <file file="${jarname}"/>
        <supplier refid="ant-pmc"/>
        <license refid="apache-2"/>
        <externalReferenceSet refid="ant-common-refs"/>
        <externalReferenceSet refid="antlib-ext-refs"/>
        <dependency componentRef="ant"/>
        <dependency componentRef="cyclonedx-core"/>
      </component>
      <additionalComponent
          name="ant"
          group="org.apache.ant"
          version="1.10.17"
          isExternal="true"
          id="ant">
        <supplier refid="ant-pmc"/>
        <license refid="apache-2"/>
        <externalReferenceSet refid="ant-common-refs"/>
        <externalReferenceSet refid="ant-ext-refs"/>
        <dependency componentRef="ant-launcher"/>
      </additionalComponent>
      <additionalComponent
          name="ant-launcher"
          group="org.apache.ant"
          version="1.10.17"
          isExternal="true"
          id="ant-launcher">
        <supplier refid="ant-pmc"/>
        <license refid="apache-2"/>
        <externalReferenceSet refid="ant-common-refs"/>
        <externalReferenceSet refid="ant-ext-refs"/>
      </additionalComponent>
      <additionalComponent
          publisher="OWASP Foundation"
          group="org.cyclonedx"
          name="cyclonedx-core-java"
          version="12.2.0"
          description="The CycloneDX core module provides a model representation of the BOM along with utilities to assist in creating, parsing, and validating BOMs."
          id="cyclonedx-core">
        <file file="${lib.dir.compile}/cyclonedx-core-java-12.2.0.jar"/>
        <license refid="apache-2"/>
        <externalReference
            type="website"
            url="https://github.com/CycloneDX/cyclonedx-core-java"/>
        <externalReference
            type="build-system"
            url="https://github.com/CycloneDX/cyclonedx-core-java/actions"/>
        <externalReference
            type="distribution-intake"
            url="https://oss.sonatype.org/service/local/staging/deploy/maven2/"/>
        <externalReference
            type="issue-tracker"
            url="https://github.com/CycloneDX/cyclonedx-core-java/issues"/>
        <externalReference
            type="vcs"
            url="https://github.com/CycloneDX/cyclonedx-core-java.git"/>
        <dependency componentRef="commons-codec"/>
        <dependency componentRef="commons-io"/>
        <dependency componentRef="commons-lang3"/>
        <dependency componentRef="commons-collections4"/>
        <dependency componentRef="packageurl-java"/>
        <dependency componentRef="jackson-dataformat-xml"/>
        <dependency componentRef="json-schema-validator"/>
      </additionalComponent>
      <additionalComponent id="commons-codec">
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/commons-codec/commons-codec/1.21.0/commons-codec-1.21.0-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/commons-codec-1.21.0.jar"/>
      </additionalComponent>
      <additionalComponent id="commons-io">
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/commons-io/commons-io/2.21.0/commons-io-2.21.0-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/commons-io-2.21.0.jar"/>
      </additionalComponent>
      <additionalComponent id="commons-lang3">
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.20.0/commons-lang3-3.20.0-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/commons-lang3-3.20.0.jar"/>
      </additionalComponent>
      <additionalComponent id="commons-collections4">
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/org/apache/commons/commons-collections4/4.5.0/commons-collections4-4.5.0-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/commons-collections4-4.5.0.jar"/>
      </additionalComponent>
      <additionalComponent
          group="com.github.package-url"
          name="packageurl-java"
          version="1.5.0"
          description='The official Java implementation of the PackageURL specification. PackageURL (purl) is a minimal specification for describing a package via a "mostly universal" URL'
          id="packageurl-java">
        <file file="${lib.dir.compile}/packageurl-java-1.5.0.jar"/>
        <license licenseId="MIT">
          <url url="https://opensource.org/licenses/MIT"/>
        </license>
      </additionalComponent>
      <additionalComponent id="jackson-dataformat-xml">
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.21.1/jackson-dataformat-xml-2.21.1-sbom-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/jackson-dataformat-xml-2.21.1.jar"/>
      </additionalComponent>
      <additionalComponent>
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.21.1/jackson-core-2.21.1-sbom-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/jackson-core-2.21.1.jar"/>
      </additionalComponent>
      <additionalComponent>
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.21/jackson-annotations-2.21-sbom-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/jackson-annotations-2.21.jar"/>
      </additionalComponent>
      <additionalComponent>
        <sbomLink>
          <url url="https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.21.1/jackson-databind-2.21.1-sbom-cyclonedx.json"/>
        </sbomLink>
        <file file="${lib.dir.compile}/jackson-databind-2.21.1.jar"/>
      </additionalComponent>
      <additionalComponent
          group="com.fasterxml.woodstox"
          name="woodstox-core"
          version="7.1.1"
          description='Woodstox is a high-performance XML processor that implements Stax (JSR-173), SAX2 and Stax2 APIs'
          unknownDependencies="true">
        <file file="${lib.dir.compile}/woodstox-core-7.1.1.jar"/>
        <license refid="apache-2"/>
      </additionalComponent>
      <additionalComponent
          group="com.networknt"
          name="json-schema-validator"
          version="2.0.1"
          description='A json schema validator that supports draft v4, v6, v7, v2019-09 and v2020-12'
          unknownDependencies="true"
          id="json-schema-validator">
        <file file="${lib.dir.compile}/json-schema-validator-2.0.1.jar"/>
        <license refid="apache-2"/>
      </additionalComponent>
      <license refid="apache-2"/>
    </cdx:componentbom>
  </target>

  <target name="ready-for-bin-dist" depends="create-antlib-sbom,common.ready-for-bin-dist">
    <resources id="additional-bin-patterns">
      <fileset dir="${build.lib}">
        <include name="${artifact.stub}-cyclonedx*"/>
      </fileset>
    </resources>
  </target>

  <target name="after-dist" depends="define-cyclonedx-components">
    <macrodef name="distributionbom" backtrace="false">
      <attribute name="version"/>
      <attribute name="archiveName"/>
      <attribute name="archiveUrl"/>
      <attribute name="archiveLocation" default=""/>
      <attribute name="outputDirectory"/>
      <attribute name="description"/>
      <attribute name="publisher"/>
      <attribute name="componentType" default="file"/>
      <attribute name="format" default="all"/>
      <attribute name="useComponentManufacturer" default="true"/>
      <attribute name="useComponentSupplier" default="true"/>
      <attribute name="supplierIsManufacturer" default="true"/>
      <element name="archiveContent"/>
      <element name="componentChildren" optional="true"/>
      <element name="additionalBomContent" optional="true"/>
      <sequential
        xmlns:cdx="antlib:org.apache.ant.cyclonedx">
        <cdx:componentbom
            bomName="@{archiveName}.cyclonedx"
            outputdirectory="@{outputDirectory}"
            format="@{format}"
            useComponentManufacturer="@{useComponentManufacturer}"
            useComponentSupplier="@{useComponentSupplier}">
          <component
              version="@{version}"
              purl="@{archiveUrl}"
              name="@{archiveUrl}"
              description="@{description}"
              publisher="@{publisher}"
              type="@{componentType}"
              supplierIsManufacturer="@{supplierIsManufacturer}">
            <file file="@{archiveLocation}"
                  unless:blank="@{archiveLocation}" xmlns:unless="ant:unless"/>
            <componentChildren/>
          </component>
          <pureFileComponents>
            <archiveContent/>
          </pureFileComponents>
          <additionalBomContent/>
        </cdx:componentbom>
      </sequential>
    </macrodef>
    <macrodef name="create-tarball-bom">
      <attribute name="distdir"/>
      <attribute name="distbase"/>
      <attribute name="binsrc"/>
      <attribute name="binarysource"/>
      <attribute name="ext"/>
      <element name="moreComponentChildren" implicit="true" optional="true"/>
      <sequential>
        <distributionbom
            outputdirectory="@{distbase}"
            version="${artifact.version}"
            archiveName="${dist.name}-@{binsrc}.@{ext}"
            archiveUrl="https://archive.apache.org/dist/ant/antlibs/cyclonedx/${dist.name}-@{binsrc}.@{ext}"
            archiveLocation="@{distbase}/${dist.name}-@{binsrc}.@{ext}"
            description="Apache CycloneDX Antlib @{binarysource}"
            publisher="The Apache Software Foundation"
            xmlns:cdx="antlib:org.apache.ant.cyclonedx">
          <componentChildren>
            <supplier refid="ant-pmc"/>
            <license refid="apache-2"/>
            <externalReferenceSet refid="ant-common-refs"/>
            <externalReferenceSet refid="antlib-ext-refs"/>
            <moreComponentChildren/>
          </componentChildren>
          <archiveContent>
            <fileset dir="@{distdir}/..">
              <include name="${dist.name}/**"/>
            </fileset>
          </archiveContent>
          <additionalBomContent>
            <license refid="apache-2"/>
          </additionalBomContent>
        </distributionbom>
      </sequential>
    </macrodef>
    <macrodef name="create-tarball-boms">
      <attribute name="distdir"/>
      <attribute name="distbase"/>
      <attribute name="binsrc"/>
      <attribute name="binarysource"/>
      <element name="componentChildren" implicit="true" optional="true"/>
      <sequential>
        <create-tarball-bom
            distdir="@{distdir}"
            distbase="@{distbase}"
            binsrc="@{binsrc}"
            binarysource="@{binarysource}"
            ext="tar.gz">
          <componentChildren/>
        </create-tarball-bom>
        <create-tarball-bom
            distdir="@{distdir}"
            distbase="@{distbase}"
            binsrc="@{binsrc}"
            binarysource="@{binarysource}"
            ext="tar.bz2">
          <componentChildren/>
        </create-tarball-bom>
        <create-tarball-bom
            distdir="@{distdir}"
            distbase="@{distbase}"
            binsrc="@{binsrc}"
            binarysource="@{binarysource}"
            ext="zip">
          <componentChildren/>
        </create-tarball-bom>
      </sequential>
    </macrodef>
  </target>

  <target name="after-src-dist" depends="after-dist">
    <create-tarball-boms
        distdir="${src.dist.dir}"
        distbase="${dist.base}"
        binsrc="src"
        binarysource="Source Distribution"/>
  </target>

  <target name="after-bin-dist" depends="after-dist">
    <create-tarball-boms
        distdir="${bin.dist.dir}"
        distbase="${dist.base}"
        binsrc="bin"
        binarysource="Binary Distribution">
      <component>
        <sbomLink>
          <file file="${bin.dist.dir}/${artifact.stub}-cyclonedx.json"/>
        </sbomLink>
        <file file="${bin.dist.dir}/${artifact.stub}.jar"/>
      </component>
    </create-tarball-boms>
  </target>

  <target name="after-fat-bin-dist" depends="after-dist">
    <create-tarball-boms
        distdir="${bin.dist.dir}"
        distbase="${dist.base}"
        binsrc="bin-withdeps"
        binarysource="Binary Distribution Including Dependencies">
      <component>
        <sbomLink>
          <file file="${bin.dist.dir}/${artifact.stub}-cyclonedx.json"/>
        </sbomLink>
        <file file="${bin.dist.dir}/${artifact.stub}.jar"/>
      </component>
    </create-tarball-boms>
  </target>

  <import file="common/build.xml"/>
</project>
