Search code examples
circlecicircleci-2.0

Run CircleCI 2.0 build using several JDKs


I would like to run my Circle CI 2.0 build using Open JDK 8 & 9. Are there any YAML examples available explaining how to build a Java project using multiple JDK versions?

Currently I trying to add an new job java-8 to my build. But I do not want to repeat all the step of my default Java 9 build job. Is there a DRY approach for this?

version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/openjdk:9-jdk

    working_directory: ~/repo

    environment:
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx1g
      TERM: dumb

    steps:
      - checkout

      # Run all tests
      - run: gradle check

  java-8:
    - image: circleci/openjdk:8-jdk

Solution

  • You can use YAML anchors to achieve a reasonable DRY approach. For instance, it might look like:

      version: 2
      shared: &shared
        working_directory: ~/repo
        environment:
          # Customize the JVM maximum heap limit
          JVM_OPTS: -Xmx1g
          TERM: dumb
        steps:
          - checkout
          # Run all tests
          - run: gradle check
    
      jobs:
        java-9:
          docker:
            - image: circleci/openjdk:9-jdk
          <<: *shared
    
        java-8:
          docker:
            - image: circleci/openjdk:8-jdk
          <<: *shared