如何在Azure DevOps代理中使用特定版本的Java,而无需下载?

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

How to you use a specific version of Java in Azure Devops Agent without downloading?

问题

我试图使用Maven包装器而不是Maven任务来运行Maven。但是,它失败了,因为它正在使用较旧版本的Java。JavaInstaller任务似乎需要一个远程源来获取JDK,我宁愿避免这样做,并使用适用于Maven任务的那个,但我无法在任何地方找到它的文档。

英文:

I am trying to run Maven using the Maven wrapper rather than the Maven task. However, it's failing because it is using an older version of Java. The JavaInstaller task seems to require a remote source for the JDK, I would rather avoid doing that and use the one that works with Maven task, but I can't find it documented anywhere.

答案1

得分: 59

你现在还可以使用 JavaToolInstaller 任务来激活预安装的 Java 版本之一,例如:

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '11'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'PreInstalled'

请参阅文档:
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/java-tool-installer?view=azure-devops

它还会设置 JAVA_HOME 并在 PATH 前添加相应路径,查看源代码:
https://github.com/microsoft/azure-pipelines-tasks/blob/46cca412451ac4418d6332114fca8ef8c3095de1/Tasks/JavaToolInstallerV0/javatoolinstaller.ts#L80

英文:

You can now also use the JavaToolInstaller task to activate one of the pre-installed Java versions, e.g.

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '11'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'PreInstalled'

See documentation at:
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/java-tool-installer?view=azure-devops

It will also set JAVA_HOME and prepend the PATH, see source:
https://github.com/microsoft/azure-pipelines-tasks/blob/46cca412451ac4418d6332114fca8ef8c3095de1/Tasks/JavaToolInstallerV0/javatoolinstaller.ts#L80

答案2

得分: 20

以下是已翻译的内容:

Linux或macOS任务的env字段可以设置要使用的Java版本:

- script: |
        java -version
  env:
    JAVA_HOME: $(JAVA_HOME_8_X64)
    PATH: $(JAVA_HOME_8_X64)/bin:$(PATH)

而对于Windows,将PATH中的冒号更改为分号:

- script: |
        java -version
  env:
    JAVA_HOME: $(JAVA_HOME_8_X64)
    PATH: $(JAVA_HOME_8_X64)/bin;$(PATH)

Java版本的替代选项包括:

  • JAVA_HOME_7_X64
    • 适用于Windows:vs2017-win2016windows-2019
    • 适用于macOS:macos-10.14macos-10.15
    • 适用于Linux:ubuntu-16.04ubuntu-18.04
  • JAVA_HOME_8_X64
    • 适用于Windows:vs2017-win2016windows-2019
    • 适用于macOS:macos-10.14macos-10.15
    • 适用于Linux:ubuntu-16.04ubuntu-18.04ubuntu-20.04
  • JAVA_HOME_11_X64
    • 适用于Windows:vs2017-win2016windows-2019
    • 适用于macOS:macos-10.14macos-10.15
    • 适用于Linux:ubuntu-16.04ubuntu-18.04ubuntu-20.04
  • JAVA_HOME_12_X64
    • 适用于macOS:macos-10.14macos-10.15
    • 适用于Linux:ubuntu-16.04ubuntu-18.04
  • JAVA_HOME_13_X64
    • 适用于Windows:vs2017-win2016windows-2019
    • 适用于macOS:macos-10.14macos-10.15
  • JAVA_HOME_14_X64
    • 适用于macOS:macos-10.14macos-10.15
英文:

The Java version to be used can be set via env field of the task for Linux or macOS:

- script: |
        java -version
  env:
    JAVA_HOME: $(JAVA_HOME_8_X64)
    PATH: $(JAVA_HOME_8_X64)/bin:$(PATH)

and for Windows, change the colon in PATH to semicolon:

- script: |
        java -version
  env:
    JAVA_HOME: $(JAVA_HOME_8_X64)
    PATH: $(JAVA_HOME_8_X64)/bin;$(PATH)

Alternatives of Java version include:

  • JAVA_HOME_7_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Avaiable on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04
  • JAVA_HOME_8_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04, ubuntu-20.04
  • JAVA_HOME_11_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04, ubuntu-20.04
  • JAVA_HOME_12_X64
    • Available on macOS: macos-10.14, macos-10.15
    • Available on Linux: ubuntu-16.04, ubuntu-18.04
  • JAVA_HOME_13_X64
    • Available on Windows: vs2017-win2016, windows-2019
    • Available on macOS: macos-10.14, macos-10.15
  • JAVA_HOME_14_X64
    • Available on macOS: macos-10.14, macos-10.15

答案3

得分: 14

在运行适用于基于 Unix 的代理的 Maven 之前,添加以下 script

- script: |
    echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
    echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)/bin:$(PATH)"    
  displayName: "设置 Java 版本"

对于基于 Windows 的代理:

- script: |
    echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
    echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)\\bin;$(PATH)"    
  displayName: "设置 Java 版本"

此部分流水线代码展示了如何选择 JAVA_HOME 值:
https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/Common/java-common/java-common.ts

英文:

Add the following script before you run Maven for Unix based agents

- script: |
    echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
    echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)/bin:$(PATH)"    
  displayName: "Set java version"

For Windows based agents

- script: |
    echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
    echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)\bin;$(PATH)"    
  displayName: "Set java version"

This part of the pipeline code shows how the JAVA_HOME value is selected:
https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/Common/java-common/java-common.ts

答案4

得分: 3

就像马丁·克雷登维斯已经提到的那样,可以使用JavaToolInstaller。然而,当在自托管代理上使用时,需要在代理上安装Java,并且需要设置所需的环境变量,以指向安装目录。

JavaToolInstaller使用从其配置派生的环境变量。约定如下:

JAVA_HOME_${versionSpec}_${jdkArchitectureOption}

可以在代理的主目录中的.env文件中设置环境变量,如下所示:

JAVA_HOME_17_x64=/usr/lib/jvm/temurin-17-jdk-amd64

在编辑完.env文件之后,需要重新启动代理,以使环境变量对流水线可用。可以通过以下方式进行操作(代理主目录):

./svc.sh stop
./svc.sh start

参见Azure文档

在此之后,可以添加一个步骤,如下所示:

- task: JavaToolInstaller@0
 inputs:
   versionSpec: '17'
   jdkArchitectureOption: 'x64'
   jdkSourceOption: 'PreInstalled'
英文:

Just like already mentioned by Martin Kreidenweis, JavaToolInstaller can be used.
When this is used however on self-hosted agent, Java needs to be installed on the agent(s) and the required environment variable needs to be set to point to the installation directory.

JavaToolInstaller uses an environment variable derived from its configuration. Convention:

JAVA_HOME_${versionSpec}_${jdkArchitectureOption}

The environment variable can we set in the agent's home directory in the .env file like this:

JAVA_HOME_17_x64=/usr/lib/jvm/temurin-17-jdk-amd64

After editing .env, the agent needs to be restarted to make the environment variable available for the pipeline. This can be done via (agent home):

./svc.sh stop
./svc.sh start

See Azure documentation.

After that a step can be added like:

- task: JavaToolInstaller@0
 inputs:
   versionSpec: '17'
   jdkArchitectureOption: 'x64'
   jdkSourceOption: 'PreInstalled'

答案5

得分: 0

这是我在Azure管道脚本中用于包含Java 17的内容:

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '17'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'PreInstalled'
  displayName: '安装Java 17'
英文:

Just FYI, this is what I used for including java 17 in my azure pipeline script:
- task: JavaToolInstaller@0
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
displayName: 'Installing Java 17'

huangapple
  • 本文由 发表于 2020年4月9日 23:44:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61124946.html
匿名

发表评论

匿名网友

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

确定