简体   繁体   English

如何使用本地 JUnit Jar 从 Gradle 而不是 Maven 中央?

[英]How to use local JUnit Jar from Gradle instead of Maven Central?

I'm trying to run my JUnit tests in an environment where I have no inte.net access but I have SSH access and can sync my local files etc.我正在尝试在没有 inte.net 访问权限但有 SSH 访问权限并且可以同步我的本地文件等的环境中运行我的 JUnit 测试。

I thought I could just add my JUnit jar to the project but that doesn't seem to work (and it doesn't seem to get downloaded to my Maven cache).我想我可以将我的 JUnit jar 添加到项目中,但这似乎不起作用(而且它似乎没有下载到我的 Maven 缓存中)。

Ideally I could do this on my dev machine with Inte.net access - to compile everything and just sync it over理想情况下,我可以在具有 Inte.net 访问权限的开发机器上执行此操作——编译所有内容并同步它

gradle assemble testClasses

Simple example简单例子

Start with this https://github.com/spring-guides/gs-rest-service Edit build.gradle to add lines to grab Jars locally从这个https://github.com/spring-guides/gs-rest-service开始编辑 build.gradle 添加行以在本地抓取 Jars

Add JUnit jar to "libs" folder ie mkdir libs;将 JUnit jar 添加到“libs”文件夹,即 mkdir libs; cd libs;光盘库; wget https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar wget https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar

plugins {
    id 'org.springframework.boot' version '2.5.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    testCompile fileTree(dir: 'libs', include: '*.jar')
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

test {
    useJUnitPlatform()
}

But it doesn't look for JUnit jar in "libs" - it's still trying to grab from Maven Central:但它不会在“libs”中寻找 JUnit jar - 它仍在尝试从 Maven Central 获取:

gradle clean test gradle 清洁测试

Task:test FAILED任务:测试失败

FAILURE: Build failed with an exception.失败:构建失败,出现异常。

  • What went wrong: Execution failed for task ':test'.出了什么问题:任务 ':test' 执行失败。

Could not download junit-jupiter-engine-5.7.2.jar (org.junit.jupiter:junit-jupiter-engine:5.7.2) Could not get resource 'https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar'.无法下载 junit-jupiter-engine-5.7.2.jar (org.junit.jupiter:junit-jupiter-engine:5.7.2) 无法获取资源 'https://repo.maven.apache.org/maven2/org/junit/木星/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar'。 > Could not GET 'https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar'. > 无法获取“https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter-engine/5.7.2/junit-jupiter-engine-5.7.2.jar”。 > repo.maven.apache.org: Temporary failure in name resolution > repo.maven.apache.org:名称解析暂时失败

  • Try: Run with --stacktrace option to get the stack trace.尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。 Run with --info or --debug option to get more log output. Run with --scan to get full insights.使用 --info 或 --debug 选项运行以获取更多日志 output。使用 --scan 运行以获取完整的见解。

  • Get more help at https://help.gradle.orghttps://help.gradle.org获得更多帮助

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 7.0 不兼容。 Use '--warning-mode all' to show the individual deprecation warnings.使用“--warning-mode all”来显示各个弃用警告。 See https://docs.gradle.org/6.8.3/userguide/command_line_interface.html#sec:command_line_warnings请参阅https://docs.gradle.org/6.8.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 4s 4 actionable tasks: 4 executed在 4 秒内构建失败 4 个可操作任务:4 个已执行

UPDATE更新

What does Gradle expect as the folder format? Gradle 期望文件夹格式是什么?

Here's what I tried这是我试过的

Changed my settings.gradle to this将我的 settings.gradle 更改为此

pluginManagement {
    repositories {
        maven {
            url './libs'
        }
    }
}

Copying all the jars to my libs folder将所有 jars 复制到我的 libs 文件夹

find.寻找。 -name " spring 2.5.2*jar" | -名称“ spring 2.5.2*jar”| xargs cp -t ~/complete/libs/ xargs cp -t ~/complete/libs/

and for fun copied the whole folder cp -rf ~/.gradle/caches/modules-2/files-2.1/ ~/complete/libs/为了好玩,复制了整个文件夹 cp -rf ~/.gradle/caches/modules-2/files-2.1/ ~/complete/libs/

But still it can't find the Spring plugin?但是还是找不到Spring插件?

My libs folder now looks like this我的 libs 文件夹现在看起来像这样

$ ls libs
6.8.3                       org.assertj
ch.qos.logback                  org.codehaus.groovy
com.datastax.oss                org.eclipse.ee4j
com.fasterxml                   org.eclipse.jetty
com.fasterxml.jackson               org.glassfish.jersey
com.fasterxml.jackson.core          org.hamcrest
com.fasterxml.jackson.datatype          org.infinispan
com.fasterxml.jackson.module            org.jboss
com.google.code.findbugs            org.jetbrains.kotlin
com.jayway.jsonpath             org.jetbrains.kotlinx
com.oracle.database.jdbc            org.junit
com.sun.activation              org.junit.jupiter
com.vaadin.external.google          org.junit.platform
commons-codec                   org.mockito
commons-logging                 org.objenesis
dependency-management-plugin-1.0.11.RELEASE.jar org.opentest4j
files-2.1                   org.ow2
io.dropwizard.metrics               org.ow2.asm
io.micrometer                   org.skyscreamer
io.netty                    org.slf4j
io.projectreactor               org.sonatype.oss
io.prometheus                   org.springframework
io.r2dbc                    org.springframework.boot
io.rsocket                  org.springframework.data
io.spring.dependency-management         org.springframework.integration
io.spring.gradle                org.springframework.security
jakarta.activation              org.springframework.session
jakarta.annotation              org.tomlj
jakarta.xml.bind                org.xmlunit
jars-8                      org.yaml
journal-1                   spring-boot-2.5.2.jar
junit-jupiter-engine-5.7.2.jar          spring-boot-autoconfigure-2.5.2.jar
modules-2                   spring-boot-buildpack-platform-2.5.2.jar
net.bytebuddy                   spring-boot-gradle-plugin-2.5.2.jar
net.java.dev.jna                spring-boot-loader-tools-2.5.2.jar
net.minidev                 spring-boot-starter-2.5.2.jar
org.antlr                   spring-boot-starter-json-2.5.2.jar
org.apache                  spring-boot-starter-logging-2.5.2.jar
org.apache.commons              spring-boot-starter-test-2.5.2.jar
org.apache.httpcomponents           spring-boot-starter-tomcat-2.5.2.jar
org.apache.logging              spring-boot-starter-web-2.5.2.jar
org.apache.logging.log4j            spring-boot-test-2.5.2.jar
org.apache.tomcat.embed             spring-boot-test-autoconfigure-2.5.2.jar
org.apiguardian                 transforms-3

$ gradle test $ gradle 测试

FAILURE: Build failed with an exception.失败:构建失败,出现异常。

  • Where: Build file '/Users/dennch3/tsmec/modules/e2etests/complete/build.gradle' line: 2其中:构建文件“/Users/dennch3/tsmec/modules/e2etests/complete/build.gradle”行:2

  • What went wrong: Plugin [id: 'org.springframework.boot', version: '2.5.2'] was not found in any of the following sources:出了什么问题:在以下任何来源中都找不到插件 [id: 'org.springframework.boot', version: '2.5.2']:

  • Gradle Core Plugins (plugin is not in 'org.gradle' namespace) Gradle 核心插件(插件不在“org.gradle”命名空间中)
  • Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.5.2') Searched in the following repositories: maven(file:~/complete/libs/)插件存储库(无法解析插件工件'org.springframework.boot:org.springframework.boot.gradle.plugin:2.5.2')在以下存储库中搜索:maven(文件:~/complete/libs/)

the issue for you is not junit, to run gradle you need access to a maven repository for dependencies and gradle plugin repository (you have spring boot)您的问题不是 junit,要运行 gradle,您需要访问依赖项的 maven 存储库和 gradle 插件存储库(您有 spring 启动)

you selected mavencentral(), but with out access to inte.net this won't work, you should get access to local repositories for maven and Gradle-plugins.您选择了 mavencentral(),但无法访问 inte.net 这将不起作用,您应该可以访问 maven 和 Gradle 插件的本地存储库。

if your libs already had the spring jars, then remove the spring related dependencies (lines 3,4)如果你的libs已经有spring jars,那么去掉spring相关的依赖(第3,4行)

also change from testCompile to testImplementation (line2)也从 testCompile 更改为 testImplementation(第 2 行)

PS - you may also need for the plugins to add local repository see https://docs.gradle.org/current/userguide/plugins.html#sec:custom_plugin_repositories PS - 您可能还需要插件来添加本地存储库,请参阅https://docs.gradle.org/current/userguide/plugins.html#sec:custom_plugin_repositories

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

相关问题 如何在Local Maven存储库中使用gradle jar - How to use in gradle a jar from the Local Maven repository 如何将 Gradle jar 编译的安装到本地 Gradle Repository files-2.1 而不是 Maven 存储库中? - How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository? 如何使用 maven 中央仓库中的特定(非默认)jar 与 maven? (蜂巢执行) - How to use a specific (not default) jar from maven central repo with maven? (hive-exec) 如何使用自定义url而不是Maven Central来获取依赖关系 - How to use custom url instead of Maven Central for sourcing dependencies Maven:如何检查jar是否未在中央仓库中上传 - Maven: How to check if jar is not uploaded in central repo 如何手动将JAR发布到maven central? - How to manually publish JAR to maven central? 如何迁移gradle发布脚本以将OSS库发布到Bintray的JCenter而不是Sonatype的Maven Central - How to migrate gradle publication script to publish OSS library to Bintray's JCenter instead of Sonatype's Maven Central 独立的jar和Maven Central - Standalone jar and maven central Gradle依赖性未列出本地Maven Jar的依赖性 - Gradle dependencies not listing dependencies of local maven jar 如何从命令行启动JUnit 5(平台)(没有Maven / Gradle)? - How to launch JUnit 5 (Platform) from the command line (without Maven/Gradle)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM