parameters:
- name: JavaPackagingandPublishing
  displayName: Package and Publish Java Package
  type: boolean
  default: true
stages:
- stage: Java_Packaging_and_Publishing
  jobs:

  - job: Windows_CPU_Java_Packaging
    workspace:
      clean: all
    pool: {name: 'onnxruntime-Win-CPU-2022'}

    steps:
    - script: |
        call .\build.bat -DOCOS_BUILD_JAVA=ON
      displayName: build the extensions java package

    - task: CopyFiles@2
      displayName: 'Copy Java Files for Windows Arch to Artifact Staging Directory'
      inputs:
        SourceFolder: 'out\Windows\java\build\libs'
        TargetFolder: '$(Build.ArtifactStagingDirectory)'

    - script: |
        cd $(Build.ArtifactStagingDirectory)
        jar xvf onnxruntime-extensions-0.11.0.jar
        tree $(Build.ArtifactStagingDirectory) /f
      displayName: Unpack JAR to sign DLL

    - template: templates/win-esrp-dll.yml
      parameters:
        FolderPath: '$(Build.ArtifactStagingDirectory)/ai/onnxruntime/extensions/native/win-x64'
        DisplayName: 'Sign DLL'
        DoEsrp: 'true'

    - script: |
        cd $(Build.ArtifactStagingDirectory)
        rm onnxruntime-extensions-0.11.0.jar
        jar cmf0 META-INF/MANIFEST.MF onnxruntime-extensions-0.11.0.jar  *
        tree $(Build.ArtifactStagingDirectory) /f
      displayName: Repack JAR with signed DLL

    - task: PublishPipelineArtifact@1
      displayName: 'Publish Windows Artifact'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifact: 'drop-signed-windows-onnxruntime-extensions-java-cpu'

    - template: templates/component-governance-component-detection-steps.yml
      parameters :
        condition : 'succeeded'

    - task: mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3
      displayName: 'Clean Agent Directories'
      condition: always()

  - job: Linux_CPU_Java_Packaging
    workspace:
      clean: all
    pool: {name: 'Azure-Pipelines-EO-Ubuntu-2004-aiinfra'}

    steps:
    - script: |
        sh ./build.sh -DOCOS_BUILD_JAVA=ON
      displayName: build the extensions java package

    - task: CopyFiles@2
      displayName: 'Copy Java Files for $(Agent.OS) Arch to Artifact Staging Directory'
      inputs:
        SourceFolder: 'out/$(Agent.OS)/RelWithDebInfo/java/build/libs'
        TargetFolder: '$(Build.ArtifactStagingDirectory)'

    - task: PublishPipelineArtifact@1
      displayName: 'Publish Linux Artifact'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifact: 'drop-linux-onnxruntime-extensions-java-cpu'

    - template: templates/component-governance-component-detection-steps.yml
      parameters :
        condition : 'succeeded'

    - task: mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3
      displayName: 'Clean Agent Directories'
      condition: always()

  - job: MacOS_CPU_Java_Packaging
    workspace:
      clean: all
    pool:
      vmImage: 'macOS-13'

    steps:
    - script: |
        sh ./build.sh -DOCOS_BUILD_JAVA=ON
      displayName: build the extensions java package

    - task: CopyFiles@2
      displayName: 'Copy Java Files for $(Agent.OS) Arch to Artifact Staging Directory'
      inputs:
        SourceFolder: 'out/$(Agent.OS)/RelWithDebInfo/java/build/libs'
        TargetFolder: '$(Build.ArtifactStagingDirectory)'

    - task: PublishPipelineArtifact@1
      displayName: 'Publish MacOS Artifact'
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifact: 'drop-macos-onnxruntime-extensions-java-cpu'

    - template: templates/component-governance-component-detection-steps.yml
      parameters :
        condition : 'succeeded'

    - task: mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3
      displayName: 'Clean Agent Directories'
      condition: always()
