Github action: 使用多个JDK和相应的环境变量进行java配置

huangapple go评论88阅读模式
英文:

Github action: setup-java with multiple JDKs and corresponding environment variables

问题

背景:
我有一个使用反应式驱动程序用于Cassandra的Spring Boot 2.3项目,构建于Java 11之上。然而,对于集成测试,当我启动嵌入式Cassandra数据库时,我依赖于机器上存在Java 8,并伴随有环境变量JAVA8_HOME。

问题:
我如何配置GitHub操作 setup-java 来为我的构建使用多个JDK,并使JAVA_HOME指向Java 11,但JAVA8_HOME指向Java 8?

英文:

Background:
I've a spring-boot 2.3 project using reactive driver for cassandra that is built on Java 11. For integration test though, when I spin up an embedded Cassandra database, I rely on presence of Java 8 on the machine with accompanying environment variable JAVA8_HOME.

Question:
How can I configure GitHub action setup-java to utilise multiple JDKs for my build and let JAVA_HOME point to Java 11 but JAVA8_HOME point to Java8?

答案1

得分: 5

在GitHub Actions中,使用多个JDK是可以实现的。一个很好且整洁的方式是利用strategy.matrix作业配置,就像在你的.github/workflows/maven.yml中所示:

name: github

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [ 8, 11, 15 ]

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v1
      with:
        java-version: ${{ matrix.java-version }}
    - run: mvn -B install --no-transfer-progress --file pom.xml

我没有检查这是否包括你需要的JAVA_HOME环境变量配置,但它明确地将构建环境隔离为不同的Java版本。这里有一个使用此设置的完整示例项目:https://github.com/codecentric/cxf-spring-boot-starter,以及一个绿色构建日志:https://github.com/codecentric/cxf-spring-boot-starter/actions/runs/468949251。

此外,GitHub Actions的矩阵构建GUI界面非常不错:
Github action: 使用多个JDK和相应的环境变量进行java配置

英文:

Using multiple JDKs with GitHub actions is already possible today. One great and neat way is to leverage the strategy.matrix job configuration like that in your .github/workflows/maven.yml:

name: github

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [ 8, 11, 15 ]

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v1
      with:
        java-version: ${{ matrix.java-version }}
    - run: mvn -B install --no-transfer-progress --file pom.xml

I didn't check if this does include the environment variable configuration for JAVA_HOME you need - but it clearly isolates the build environments for the separate Java versions. Here's a full example project using this setup: https://github.com/codecentric/cxf-spring-boot-starter and here's a green build log.

Also the GitHub actions GUI for matrix builds is quite nice:
Github action: 使用多个JDK和相应的环境变量进行java配置

答案2

得分: 2

不完全是 setup-java,但你可以尝试另一个 java-install 操作:https://github.com/AdoptOpenJDK/install-jdk#multiple-jdks。使用 target 属性设置环境变量 JAVA_HOME

英文:

Not exactly setup-java, but you can try another java-install action: https://github.com/AdoptOpenJDK/install-jdk#multiple-jdks. Use target property for setting up env var to use as JAVA_HOME.

huangapple
  • 本文由 发表于 2020年10月9日 04:25:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64270132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定