简体   繁体   English

如何在 Azure 管道 Ubuntu 代理上安装 java 13 并在 Maven 构建期间使用它?

[英]How to install java 13 on Azure-pipeline Ubuntu agent and use it during Maven build?

I am trying to build a Java Maven application and run some tests against a Postgres db.我正在尝试构建一个 Java Maven 应用程序并针对 Postgres 数据库运行一些测试。

However, the java app uses JDK 13 and the Azure hosted agent ubuntu doesn't have this default installed.但是,java 应用程序使用 JDK 13,而 Azure 托管代理 ubuntu 没有安装此默认值。 As such, I use the script task to install it, then use it during the Maven build.因此,我使用脚本任务来安装它,然后在 Maven 构建期间使用它。

However, I tried several config's and in all Maven keeps complaining that it can not found any JDK 13 installed.但是,我尝试了几个配置,并且在所有 Maven 中一直抱怨找不到安装的任何 JDK 13。

The last config I tried is listed below, in which I install it through a script and then use the JavaToolInstaller task to make it available (ensuring the Java_home is set and the java can be found in the path. I then get the error下面列出了我尝试的最后一个配置,我通过脚本安装它,然后使用 JavaToolInstaller 任务使其可用(确保设置了 Java_home 并且可以在路径中找到 java。然后我得到错误

##[error]Java 13 is not preinstalled on this agent

I also tried it without the JavaToolInstaller task and then export JAVA_HOME and modify the PATH in the script, but then Maven complaints it can not find JDK 13...我也试过没有JavaToolInstaller任务然后导出JAVA_HOME并修改脚本中的PATH,但是Maven抱怨找不到JDK 13 ...

Please some help how to use JDK 13 on an ubuntu agent during the maven build?请帮助如何在 maven 构建期间在 ubuntu 代理上使用 JDK 13?

Azure pipeline snippet: Azure 管道片段:

    variables:
    MAVEN_CACHE_FOLDER: $(Pipeline.Workspace)/.m2/repository
    MAVEN_OPTS: "-Dmaven.repo.local=$(MAVEN_CACHE_FOLDER)"
    JAVA_HOME : "/usr/lib/jvm/openjdk-13-jdk"
    PATH: $(JAVA_HOME)/bin:$(PATH)
    service_name: backend
    mygetUsername: myUserName
    mygetPassword: myPassword

resources:
    containers:
        - container: postgres
          image: postgres:11.6-alpine
          ports:
              - 5432:5432
          env:
              POSTGRES_DB: default
              POSTGRES_USER: default
              POSTGRES_PASSWORD: default
              POSTGRES_HOST_AUTH_METHOD: trust


stages:
    - stage: create_artifact
      displayName: Create artifact
      jobs:
          - job: build
            displayName: Build, test and publish artifact
            services:
                postgres: postgres
            steps:
                - script: |
                    sudo apt-get install openjdk-13-jdk  
                  displayName: Installing JDK 13

                - task: JavaToolInstaller@0
                  displayName: Using JDK 13
                  inputs:
                    versionSpec: "13"
                    jdkArchitectureOption: x64
                    jdkSourceOption: "PreInstalled"

                - task: Cache@2
                  displayName: Cache Maven local repo
                  inputs:
                      key: 'maven | "$(Agent.OS)" | backend/pom.xml'
                      restoreKeys: |
                          maven | "$(Agent.OS)"
                          maven
                      path: $(MAVEN_CACHE_FOLDER)
                - task: Maven@3
                  name: maven_package
                  displayName: Maven package
                  inputs:
                      goals: "package"
                      mavenPomFile: "backend/pom.xml"
                      options: '--settings backend/.mvn/settings.xml -DmygetUsername=$(mygetUsername) -DmygetPassword=$(mygetPassword)'
                      mavenOptions: "-Xmx3072m $(MAVEN_OPTS)"
                      javaHomeOption: "JDKVersion"
                      jdkVersionOption: "1.13"
                      mavenAuthenticateFeed: true

"PreInstalled" feature allows you to use Java versions that are pre-installed on the Microsoft-hosted agent. “预安装”功能允许您使用预安装在 Microsoft 托管代理上的 Java 版本。 You can find available pre-installed versions of Java in Software section :您可以在软件部分找到 Java 的可用预安装版本:

在此处输入图像描述

I think your script does not work on hosted machine somehow.我认为您的脚本无法以某种方式在托管计算机上运行。 And, the JDK version you specify is not present on your host machine.而且,您指定的 JDK 版本不在您的主机上。 I would suggest you to set your Host machine as per the above table (as per pre-defined JDK installed).我建议您根据上表设置您的主机(根据安装的预定义 JDK)。

Consequently, the other two options can be leveraged:因此,可以利用其他两个选项:

Here's an example of getting the archive file from a local directory on Linux.这是从 Linux 上的本地目录获取存档文件的示例。 The file should be an archive ( .zip , .gz ) of the JAVA_HOME directory so that it includes the bin , lib , include , jre , etc. directories.该文件应该是JAVA_HOME目录的存档文件( .zip.gz ),以便它包含binlibincludejre等目录。

 - task: JavaToolInstaller@0
    inputs:
      versionSpec: "11"
      jdkArchitectureOption: x64
      jdkSourceOption: LocalDirectory
      jdkFile: "/builds/openjdk-11.0.2_linux-x64_bin.tar.gz"
      jdkDestinationDirectory: "/builds/binaries/externals"
      cleanDestinationDirectory: true

Here's an example of downloading the archive file from Azure Storage.这是从 Azure 存储下载存档文件的示例。 The file should be an archive ( .zip , .gz ) of the JAVA_HOME directory so that it includes the bin , lib , include , jre , etc. directories.该文件应该是JAVA_HOME目录的存档文件( .zip.gz ),以便它包含binlibincludejre等目录。

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '6'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: AzureStorage
    azureResourceManagerEndpoint: myARMServiceConnection
    azureStorageAccountName: myAzureStorageAccountName
    azureContainerName: myAzureStorageContainerName
    azureCommonVirtualFile: 'jdk1.6.0_45.zip'
    jdkDestinationDirectory: '$(agent.toolsDirectory)/jdk6'
    cleanDestinationDirectory: false

The problem was that I was pointing to the wrong installed directory.问题是我指向了错误的安装目录。 The above location of JAVA_HOME I found on internet, after ouputing the tree content of the /usrl/lib/jvm I changed it and added this to the variable section and maven found the jdk and uses it我在互联网上找到的 JAVA_HOME 的上述位置,在输出 /usrl/lib/jvm 的树内容后,我对其进行了更改并将其添加到变量部分,maven 找到了 jdk 并使用它

JAVA_HOME_13_X64 : "/usr/lib/jvm/java-13-openjdk-amd64"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM