仅提供build.gradle文件下载依赖项 [英] Download dependencies given only the build.gradle file

查看:119
本文介绍了仅提供build.gradle文件下载依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用 build.gradle 文件下载gradle依赖项?

Is it possible to download gradle dependencies using only build.gradle file?

我要尝试的操作完成如下:

What I am trying to accomplish is the following:

我有一组单元测试,我想在docker容器中执行它们(作为CI流程的一部分)。最初,我使用了来自docker hub的 openjdk:8-jdk 图像作为测试的基础图像。因此,docker compose文件包含以下内容:

I have a set of unit tests and I want to execute them (as part of CI process) inside a docker container. Initially, I used the openjdk:8-jdk image from docker hub as base image for my tests. So, docker compose file contains the following:

version: '3.2'

services:

  unit:
    image: openjdk:8-jdk
    volumes:
      - ..:/usr/test
    working_dir: /usr/test
    command: sh -c "exec ./gradlew junitPlatformTest -Punit -p moduleA/"

整个项目安装在容器内的 / usr / test 目录中。容器启动时,它将对 moduleA 执行 junitPlatformTest 任务。 openjdk:8-jdk 映像的问题在于,每当我运行 unit 服务时,gradle及其依赖项都会下载

Whole project is mounted on /usr/test directory inside the container. When the container starts, it executes the junitPlatformTest task against moduleA. The problem with openjdk:8-jdk image is that gradle and its dependencies are downloaded every time I run the unit service.

为了解决这个问题,我决定创建一个新图像,该图像将具有gradle且我的项目依赖项已下载。 Dockerfile 如下:

To solve this, I decided to create a new image which would have gradle and my project dependencies already downloaded. The Dockerfile is the following:

FROM openjdk:8-jdk
COPY . /usr/test
WORKDIR /usr/test
RUN apt-get -y install wget unzip
RUN wget https://services.gradle.org/distributions/gradle-4.1-bin.zip
RUN mkdir /opt/gradle
RUN unzip -d /opt/gradle gradle-4.1-bin.zip
RUN /opt/gradle/gradle-4.1/bin/gradle dependencies

build.gradle 文件位于同一文件夹中作为Dockerfile,因此命令 COPY。 / usr / test 将其复制到工作目录中。

The build.gradle file is located in same folder as Dockerfile so the command COPY . /usr/test copies it in the working directory.

但是,执行 gradle依赖项命令不会下载库。生成映像后,运行一个容器并进入其中(使用docker exec),看来〜/ .gradle / caches / modules-2 / files-2.1 / 目录

However, executing the gradle dependencies command does not download the libraries. After built the image, ran a container and entered into it (with docker exec), it seems that ~/.gradle/caches/modules-2/files-2.1/ directory contains only pom files, not jars.

如果 gradle依赖性是正确的命令,则不使用。有建议吗?

I'm not use if gradle dependencies is the correct command. Any suggestions?

编辑-Gradle文件

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

ext.versions = new Properties()
file("./versions.properties").withInputStream {
    stream -> ext.versions.load(stream)
}

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:$versions.junitJupiterVersion")
    testCompile("org.junit.jupiter:junit-jupiter-engine:$versions.junitJupiterVersion")
    testCompile("org.junit.jupiter:junit-jupiter-params:$versions.junitJupiterVersion")
    testCompile("org.mockito:mockito-core:'$versions.mockitoCore")
    testCompile("org.junit.platform:junit-platform-launcher:1.0.0-RC3")

    compile("com.google.inject:guice:$versions.guice")
    ....
}


推荐答案

在下方添加到gradle文件中

Add below to your gradle file

 task download (type: Exec) {
     configurations.testCompile.files
     commandLine 'echo', 'Downloaded all dependencies'

  }

也更改

RUN /opt/gradle/gradle-4.1/bin/gradle dependencies

RUN /opt/gradle/gradle-4.1/bin/gradle

在运行gradle命令时,这会将所有依赖项缓存到〜/ .gradle 。它还将下载罐子,而不仅仅是poms。而且,如果您想进一步缓存,甚至可以为gradle文件夹使用命名卷

This will cache all dependencies to ~/.gradle when you run the gradle command. It will download the jars also and not just poms. And if you want to cache further you can even use a named volume for gradle folder

version: '3.2'

services:

  unit:
    image: openjdk:8-jdk
    volumes:
      - ..:/usr/test
      - gradlecache:/root/.gradle/
    working_dir: /usr/test
    command: sh -c "exec ./gradlew junitPlatformTest -Punit -p moduleA/"
volumes:
  gradlecache: {}

这篇关于仅提供build.gradle文件下载依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆