简体   繁体   English

如何在不下载的情况下在 Azure Devops Agent 中使用特定版本的 Java?

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

I am trying to run Maven using the Maven wrapper rather than the Maven task.我正在尝试使用 Maven 包装器而不是 Maven 任务运行 Maven 。 However, it's failing because it is using an older version of Java.但是,它失败了,因为它使用的是旧版本的 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. JavaInstaller 任务似乎需要 JDK 的远程源,我宁愿避免这样做并使用与 Maven 任务一起使用的源,但我在任何地方都找不到它的文档。

You can now also use the JavaToolInstaller task to activate one of the pre-installed Java versions, eg现在还可以使用JavaToolInstaller任务来激活预安装的 Java 版本之一,例如

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

See documentation at: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/java-tool-installer?view=azure-devops请参阅以下文档: https://docs.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它还将设置JAVA_HOME并预先添加PATH ,请参阅来源: https://github.com/microsoft/azure-pipelines-tasks/blob/46cca412451ac4418d6332114fca8ef8c3095de1/Tasks/JavaToolInstallerV0/javatoolinstaller.ts#L8

Add the following script before you run Maven for Unix based agents在为基于 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: "Set java version"

For Windows based agents适用于基于 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: "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这部分管道代码显示了如何选择 JAVA_HOME 值: https://github.com/microsoft/azure-pipelines-tasks/blob/master/Tasks/Common/java-common/java-common.ts

The Java version to be used can be set via env field of the task for Linux or macOS:可以通过 Linux 或 macOS 的任务的env字段设置要使用的 Java 版本:

- 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:对于 Windows,将 PATH 中的冒号更改为分号:

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

Alternatives of Java version include: Java 版本的替代品包括:

  • JAVA_HOME_7_X64
    • Available on Windows: vs2017-win2016 , windows-2019适用于 Windows: vs2017-win2016windows-2019
    • Avaiable on macOS: macos-10.14 , macos-10.15适用于 macOS: macos-10.14macos-10.15
    • Available on Linux: ubuntu-16.04 , ubuntu-18.04在 Linux 上可用: ubuntu-16.04ubuntu-18.04
  • JAVA_HOME_8_X64
    • Available on Windows: vs2017-win2016 , windows-2019适用于 Windows: vs2017-win2016windows-2019
    • Available on macOS: macos-10.14 , macos-10.15在 macOS 上可用: macos-10.14macos-10.15
    • Available on Linux: ubuntu-16.04 , ubuntu-18.04 , ubuntu-20.04在 Linux 上可用: ubuntu-16.04ubuntu-18.04ubuntu-20.04
  • JAVA_HOME_11_X64
    • Available on Windows: vs2017-win2016 , windows-2019适用于 Windows: vs2017-win2016windows-2019
    • Available on macOS: macos-10.14 , macos-10.15在 macOS 上可用: macos-10.14macos-10.15
    • Available on Linux: ubuntu-16.04 , ubuntu-18.04 , ubuntu-20.04在 Linux 上可用: ubuntu-16.04ubuntu-18.04ubuntu-20.04
  • JAVA_HOME_12_X64
    • Available on macOS: macos-10.14 , macos-10.15在 macOS 上可用: macos-10.14macos-10.15
    • Available on Linux: ubuntu-16.04 , ubuntu-18.04在 Linux 上可用: ubuntu-16.04ubuntu-18.04
  • JAVA_HOME_13_X64
    • Available on Windows: vs2017-win2016 , windows-2019适用于 Windows: vs2017-win2016windows-2019
    • Available on macOS: macos-10.14 , macos-10.15在 macOS 上可用: macos-10.14macos-10.15
  • JAVA_HOME_14_X64
    • Available on macOS: macos-10.14 , macos-10.15在 macOS 上可用: macos-10.14macos-10.15

Just like already mentioned by Martin Kreidenweis , JavaToolInstaller can be used.就像Martin Kreidenweis已经提到的那样,可以使用JavaToolInstaller
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.但是,当在自托管代理上使用此功能时,需要在代理上安装 Java,并且需要将所需的环境变量设置为指向安装目录。

JavaToolInstaller uses an environment variable derived from its configuration. JavaToolInstaller 使用从其配置派生的环境变量。 Convention:习俗:

JAVA_HOME_${versionSpec}_${jdkArchitectureOption}

The environment variable can we set in the agent's home directory in the .env file like this:我们可以在.env文件中的代理主目录中设置环境变量,如下所示:

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.编辑.env后,需要重新启动代理以使环境变量可用于管道。 This can be done via (agent home) :这可以通过(agent home)完成:

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

See Azure documentation .请参阅Azure 文档

After that a step can be added like:之后可以添加一个步骤,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Azure 管道 Microsoft 托管代理上升级 java 版本? - How to upgrade java version on an Azure pipeline Microsoft-hosted agent? Azure 用于 Java 项目的 DevOps 管道,带有自托管代理 - Azure DevOps Pipeline for Java Project with self hosted agent 如何在IE或Chrome中使用特定版本的Java - How to use a specific version of java in IE or Chrome 如何在不使用Java下载到本地计算机的情况下使用HTTPS wsdl文件 - How to use an HTTPS wsdl file without downloading to local computer in java 如何在 Azure DevOps 上部署 Java Web 应用程序 - How to deploy Java Web app on Azure DevOps 如何在 Azure DevOps 管道中安装 Java? - How to install Java in an Azure DevOps pipeline? Azure Devops 管道:访问 xml 文件中的代理 JAVA_HOME 变量 - Azure Devops Pipeline : Accessing agent JAVA_HOME variable in xml file 如何在 Azure 管道 Ubuntu 代理上安装 java 13 并在 Maven 构建期间使用它? - How to install java 13 on Azure-pipeline Ubuntu agent and use it during Maven build? Cloud Foundry:如何使用 Java Buildpack 特定版本 - Cloud Foundry : How use Java Buildpack specific version Maven 下载特定版本,如何强制下载其他版本? - Maven downloading a specific version, how can I force to download an other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM