[英]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
vs2017-win2016
, windows-2019
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
ubuntu-16.04
, ubuntu-18.04
JAVA_HOME_8_X64
vs2017-win2016
, windows-2019
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
, ubuntu-20.04
ubuntu-16.04
、 ubuntu-18.04
、 ubuntu-20.04
JAVA_HOME_11_X64
vs2017-win2016
, windows-2019
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
, ubuntu-20.04
ubuntu-16.04
、 ubuntu-18.04
、 ubuntu-20.04
JAVA_HOME_12_X64
macos-10.14
, macos-10.15
macos-10.14
, macos-10.15
ubuntu-16.04
, ubuntu-18.04
ubuntu-16.04
, ubuntu-18.04
JAVA_HOME_13_X64
vs2017-win2016
, windows-2019
vs2017-win2016
, windows-2019
macos-10.14
, macos-10.15
macos-10.14
, macos-10.15
JAVA_HOME_14_X64
macos-10.14
, macos-10.15
macos-10.14
, macos-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.