Gitlab ant.java.version 是 11 但我想使用另一个版本构建项目

Posted

技术标签:

【中文标题】Gitlab ant.java.version 是 11 但我想使用另一个版本构建项目【英文标题】:Gitlab ant.java.version is 11 but I want to build the project using another version 【发布时间】:2021-12-05 12:04:20 【问题描述】:

所以我正在使用 gitlab,并且我有一个使用 .gitlab-ci-yml 文件在 gitlab VM 上构建的 java 项目。 我有这个问题,我们必须更改构建项目的 java 版本(当前 java 11)。

所以添加build.xml这个:<echo message="ant.java.version = $ant.java.version" />将打印11。 我尝试在javac 中指定sourcetarget 来更改它,但是如果未指定target,它将不会使用另一个java 版本构建它,如果指定了它,它将返回一个错误: 也使用 <property name="ant.build.javac.target" value="17" /> 将返回相同的错误。 所以我不确定是否基本上将我自己的ant 工具和java 版本添加到包中并强制它使用那个会起作用,我不知道该怎么做。

这里是 build.xml 文件和 .gitlab-ci.yml

更新.gitlab-ci.yml 已更新。现在错误似乎如下,具有以下.gilab-ci.yml 文件: Unable to locate package adoptopenjdk-17-hotspotbuild.xml

<project name="ClinicalTrialProcessor" default="all" basedir=".">

    <property name="build" value="$basedir/build"/>
    <property name="source" value="$basedir/source"/>
    <property name="java" value="$source/java"/>
    <property name="files" value="$source/files"/>
    <property name="resources" value="$source/resources"/>
    <property name="config" value="$source/config"/>
    <property name="installer" value="org/rsna/installer"/>
    <property name="launcher" value="org/rsna/launcher"/>
    <property name="runner" value="org/rsna/runner"/>
    <property name="libraries" value="$basedir/libraries"/>
    <property name="products" value="$basedir/products"/>
    <property name="documentation" value="$basedir/documentation"/>
    <property name="title" value="CTP"/>
    <property name="copyright" value="Copyright 2013 Radiological Society of North America"/>
    <property name="ant.build.javac.target" value="17" />

    <path id="classpath">
        <pathelement location="$libraries/util.jar"/>
        <pathelement location="$libraries/dcm4che.jar"/>
        <pathelement location="$libraries/getopt.jar"/>
        <pathelement location="$libraries/log4j.jar"/>
        <pathelement location="$libraries/edtftpj.jar"/>
        <pathelement location="$libraries/jdbm.jar"/>
        <pathelement location="$libraries/commons-compress-1.0.jar"/>
        <pathelement location="$libraries/jai_imageio.jar"/>
        <pathelement location="$libraries/pixelmed_codec.jar"/>
        <pathelement location="$libraries/email/activation.jar"/>
        <pathelement location="$libraries/email/javax.mail.jar"/>
        <pathelement location="$libraries/ftp/commons-logging-1.2.jar"/>
        <pathelement location="$libraries/ftp/commons-net-3.3.jar"/>
        <pathelement location="$libraries/ftp/commons-vfs2-2.0.jar"/>
        <pathelement location="$libraries/ftp/jsch-0.1.53.jar"/>
                <fileset dir="$libraries/aws/third-party" includes="**/*.jar"/>
                <fileset dir="$libraries/aws/lib" includes="**/*.jar"/>
              </path>

    <target name="clean">
        <delete dir="$build" failonerror="false"/>
        <delete dir="$documentation" failonerror="false"/>
    </target>

    <target name="init">
        <tstamp>
            <format property="today" pattern="yyyy.MM.dd"/>
            <format property="now" pattern="HH:mm:ss z"/>
        </tstamp>
        <echo message="Time now $now"/>
        <echo message="ant.java.version = 17" />
        <mkdir dir="$build"/>
        <mkdir dir="$products"/>
    </target>

    <target name="compile" depends="init">
        <javac destdir="$build" optimize="on"
                source="17"
                classpathref="classpath"
                includeantruntime="false"
                debug="true" debuglevel="lines,vars,source">
            <src path="$java"/>
            <!-- <compilerarg value="-Xlint:unchecked"/> -->
            <compilerarg line="--add-exports java.base/com.sun.crypto.provider=ALL-UNNAMED"/>
        </javac>
    </target>


    <target name="jar" depends="compile">
        <mkdir dir="$build/CTP"/>



        <jar jarfile="$build/CTP/Launcher.jar">
            <manifest>
                <attribute name="Main-Class" value="org.rsna.launcher.Launcher"/>
                <attribute name="Date" value="$today at $now"/>
                <attribute name="Java-Version" value="17"/>
                    <attribute name="Class-Path" value="libraries/util.jar libraries/log4j.jar libraries/aws/lib/aws-java-sdk-1.11.483-javadoc.jar libraries/aws/lib/aws-java-sdk-1.11.483-sources.jar libraries/aws/lib/aws-java-sdk-1.11.483.jar libraries/aws/third-party/lib/aspectjrt-1.8.2.jar libraries/aws/third-party/lib/aspectjweaver.jar libraries/aws/third-party/lib/aws-swf-build-tools-1.1.jar libraries/aws/third-party/lib/commons-codec-1.10.jar libraries/aws/third-party/lib/commons-logging-1.1.3.jar libraries/aws/third-party/lib/freemarker-2.3.9.jar libraries/aws/third-party/lib/httpclient-4.5.5.jar libraries/aws/third-party/lib/httpcore-4.4.9.jar libraries/aws/third-party/lib/ion-java-1.0.2.jar libraries/aws/third-party/lib/jackson-annotations-2.6.0.jar libraries/aws/third-party/lib/jackson-core-2.6.7.jar libraries/aws/third-party/lib/jackson-databind-2.6.7.2.jar libraries/aws/third-party/lib/jackson-dataformat-cbor-2.6.7.jar libraries/aws/third-party/lib/javax.mail-api-1.4.6.jar libraries/aws/third-party/lib/jmespath-java-1.11.483.jar libraries/aws/third-party/lib/joda-time-2.8.1.jar libraries/aws/third-party/lib/netty-buffer-4.1.17.Final.jar libraries/aws/third-party/lib/netty-codec-4.1.17.Final.jar libraries/aws/third-party/lib/netty-codec-http-4.1.17.Final.jar libraries/aws/third-party/lib/netty-common-4.1.17.Final.jar libraries/aws/third-party/lib/netty-handler-4.1.17.Final.jar libraries/aws/third-party/lib/netty-resolver-4.1.17.Final.jar libraries/aws/third-party/lib/netty-transport-4.1.17.Final.jar libraries/aws/third-party/lib/spring-beans-3.0.7.RELEASE.jar libraries/aws/third-party/lib/spring-context-3.0.7.RELEASE.jar libraries/aws/third-party/lib/spring-core-3.0.7.RELEASE.jar libraries/aws/third-party/lib/spring-test-3.0.7.RELEASE.jar" />
            </manifest>
            <fileset dir="$build"
                includes="$launcher/**" />
        </jar>

        <jar jarfile="$build/CTP/Runner.jar">
            <manifest>
                <attribute name="Main-Class" value="org.rsna.runner.Runner"/>
                <attribute name="Date" value="$today at $now"/>
                <attribute name="Java-Version" value="17"/>
            </manifest>
            <fileset dir="$build"
                includes="$runner/**" />
        </jar>

        <copy overwrite="true" todir="$build">
            <fileset dir="$resources"/>
        </copy>

        <jar jarfile="$libraries/CTP.jar">
            <manifest>
                <attribute name="Main-Class" value="org.rsna.ctp.ClinicalTrialProcessor"/>
                <attribute name="Date" value="$today at $now"/>
                <attribute name="Java-Version" value="17"/>
                <attribute name="Class-Path" value="util.jar log4j.jar"/>
            </manifest>
            <fileset dir="$build"
                includes="**"
                excludes="$installer/**, $launcher/**, $runner/** CTP/Launcher.jar CTP/Runner.jar" />
        </jar>

    </target>

    <target name="installer" depends="jar">
        <mkdir dir="$build/CTP/libraries"/>
        <mkdir dir="$build/CTP/libraries/email"/>
        <mkdir dir="$build/config"/>

        <copy overwrite="true" todir="$build/CTP">
            <fileset dir="$files" includes="**"/>
        </copy>
        <copy overwrite="true" todir="$build/CTP/libraries">
            <fileset dir="$libraries" includes="*.*" excludes="jai_imageio.jar"/>
        </copy>
        <copy overwrite="true" todir="$build/CTP/libraries/email">
            <fileset dir="$libraries/email" includes="*.*"/>
        </copy>
        <copy overwrite="true" todir="$build/CTP/libraries/aws">
            <fileset dir="$libraries/aws" includes="**/*.jar"/>
        </copy>
        <copy overwrite="true" todir="$build/CTP/libraries/ftp">
            <fileset dir="$libraries/ftp" includes="*.*"/>
        </copy>
        <copy overwrite="true" todir="$build/CTP/libraries/imageio">
            <fileset dir="$libraries/imageio" includes="*.*"/>
        </copy>
        <copy overwrite="true" todir="$build/config">
            <fileset dir="$config" includes="*.*"/>
        </copy>

        <jar destfile="$products/CTP-installer.jar">
            <manifest>
                <attribute name="Main-Class" value="org.rsna.installer.Installer"/>
                <attribute name="Date" value="$today at $now"/>
                <attribute name="Java-Version" value="17"/>
                <attribute name="Description" value="CTP for clinical trials"/>
            </manifest>
            <fileset dir="$build"
                includes="$installer/Installer** CTP/** config/**"/>
        </jar>

    </target>

    <target name="javadocs">
        <mkdir dir="$documentation"/>
        <javadoc destdir="$documentation" sourcepath="$java" classpathref="classpath"
                 doctitle="$title" windowtitle="$title" bottom="$copyright">
            <package name="org.*"/>
        </javadoc>
    </target>

    <target name="all" depends="clean, installer, javadocs">
        <tstamp>
            <format property="end" pattern="HH:mm:ss"/>
        </tstamp>
        <echo message="Time now $end"/>
    </target>

</project>

.gitlab-ci.yml

stages:
  - deploy

default:
  before_script:
    - sudo apt update
    - sudo apt install -y software-properties-common
    - sudo apt-get install ca-certificates
    - sudo add-apt-repository --yes "deb https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ bionic main"
    - sudo apt-get update -qq && sudo apt-get install -y adoptopenjdk-17-hotspot latex209-bin texlive-latex-base texlive-latex-extra ant
    - ant all -f src/build.xml
    - sudo apt-get update -qq && sudo apt-get install -y latex209-bin texlive-latex-base texlive-latex-extra ant
    - ant all -f src/build.xml
    
deploy_Default:
  stage: deploy
  script:
    - sh -x doc.sh "$CTP_NAME" "$(cat CTP_VERSION)"
    - sh -x deploy.sh "$CTP_NAME" "$(cat CTP_VERSION)" "$PROFILE_VERSION" "$PROFILE_NAME"
  artifacts:
    paths:
      - "$CTP_NAME_$(cat CTP_VERSION).zip"
  only:
    - master
    - tags
    - ptname_mapping

【问题讨论】:

您安装了哪个 Java 版本并在您的 $PATH 中? 在我的本地计算机上它是 Java 11,但是这有什么关系,因为我是在 gitlab 虚拟机上使用 .gitlab-ci.yml 构建它的? 我的意思是 Gitlab 的跑步者有什么 Java 版本。您可以添加对java -version 的调用作为script 标签的第一步,以提供此信息 上面写着java version "14.0.1" 【参考方案1】:

根据 cmets 中提供的信息,您的 GitLab 运行器上有 Java 14.0.1。由于此 Java 版本用于启动 ant 及其任务,因此无法编译目标版本为“17”的代码。

长话短说,您的跑步者需要更新的 Java 版本。

执行此操作的最简单方法可能是使用包含它的预构建图像:

deploy_Default:
  stage: deploy
  image: openjdk:17
  script:
    - sh -x doc.sh "$CTP_NAME" "$(cat CTP_VERSION)"
    - sh -x deploy.sh "$CTP_NAME" "$(cat CTP_VERSION)" "$PROFILE_VERSION" "$PROFILE_NAME"

如果不可能,您可以在脚本之前的阶段显式安装它:

default:
  before_script:
    - wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
    - sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
    - sudo apt-get update -qq && sudo apt-get install -y adoptopenjdk-17-hotspot latex209-bin texlive-latex-base texlive-latex-extra ant
    - ant all -f src/build.xml

【讨论】:

以上是关于Gitlab ant.java.version 是 11 但我想使用另一个版本构建项目的主要内容,如果未能解决你的问题,请参考以下文章

gitlab 12.9 的存储库的迁移路径是啥。到 gitlab 14.2?

部署本地gitlab

新人刚接触gitlab。求问怎么使用浏览器访问GitLab,网址是啥?

gitlab 搭建 备份 升级 迁移恢复

怎么配置gitlab gitlab

[后端]gitlab之gitlab-ci自动部署