Gradle 找不到依赖项

2024-04-11

我正在尝试使用 Gradle,文件如下所示:

// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    jcenter()
}

jar {
    manifest {
        attributes 'Main-Class': 'execute.Entry'
    }
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.12'
    compile 'org.apache.logging.log4j:log4j:2.3'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

如您所见,我添加了两个依赖项并希望在类中使用

package execute;

import message.*;
import org.apache.log4j.Logger;

public class Entry {

    public static void main(String[] args) {

        Service s = new Service();
        String msg = s.GetMessage();
        LOGGER.info("Received msg: " + msg);

    }
}

当我执行该语句时gradle assemble,我遇到编译器错误。

D:\Java\entrypoint\src\main\java\execute\Entry.java:4: error: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
                       ^
D:\Java\entrypoint\src\main\java\execute\Entry.java:12: error: cannot find symbol
        LOGGER.info("Received msg: " + msg);
        ^
  symbol:   variable LOGGER
  location: class Entry
2 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 6.261 secs
Compilation failed; see the compiler error output for details.

我究竟做错了什么?

Update我将我的代码更改为:

package execute;

import message.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Entry {

    private static final Logger logger = LogManager.getLogger("HelloWorld");

    public static void main(String[] args) {

        Service s = new Service();
        String msg = s.GetMessage();
        logger.info("Hello, World!");

    }
}

编译器抱怨:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
        at execute.Entry.<clinit>(Entry.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Update 2
执行后:

>gradle dependencies
:dependencies                                                                    

------------------------------------------------------------
Root project                 
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies                                                   

compile - Compile classpath for source set 'main'.
\--- org.apache.logging.log4j:log4j-api:2.3                      

default - Configuration for default artifacts.
\--- org.apache.logging.log4j:log4j-api:2.3                      

runtime - Runtime classpath for source set 'main'.
\--- org.apache.logging.log4j:log4j-api:2.3                      

testCompile - Compile classpath for source set 'test'.
+--- org.apache.logging.log4j:log4j-api:2.3                          
\--- junit:junit:4.12        
     \--- org.hamcrest:hamcrest-core:1.3

testRuntime - Runtime classpath for source set 'test'.
+--- org.apache.logging.log4j:log4j-api:2.3                          
\--- junit:junit:4.12        
     \--- org.hamcrest:hamcrest-core:1.3

BUILD SUCCESSFUL

代替

import org.apache.log4j.Logger;

use

import org.apache.logging.log4j.Logger;

并定义 LOGGER 的实例

private static final Logger LOGGER = LogManager.getLogger("HelloWorld");

请参阅此示例http://logging.apache.org/log4j/2.x/manual/api.html http://logging.apache.org/log4j/2.x/manual/api.html

这与 Gradle 无关

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gradle 找不到依赖项 的相关文章

随机推荐

  • constexpr 默认默认构造函数

    如果我想声明我的 Clang 3 8 和 GCC 5 3 我会收到编译器错误default ed 默认构造函数为constexpr 根据this https stackoverflow com questions 20810378 shou
  • google action包如何定义自定义槽类型?

    您好 这是一个关于 google home action sdk gactions exe 的问题 at https developers google com actions reference rest Shared Types Que
  • Angular Material - 设置调色板

    我正在尝试结合材料设计建立一个 Angular 项目 我的 package json 的一部分如下所示 dependencies angular2 material button 2 0 0 alpha 3 angular2 materia
  • 如何在两台电脑之间共享svn仓库

    我的工作站上有 SVN 存储库 我在工作站和笔记本电脑上都使用它 在工作站上 我可以在本地访问存储库 但在笔记本电脑上 我必须连接互联网才能访问存储库 这不方便 因为我无法在飞机 火车和其他无互联网的地方工作 我想比较修订等 在笔记本电脑上
  • 如何获取mysql中两个日期之间的月份列表

    我必须获取 mysql 中两个日期之间的月份列表 For Example My Input is From date 23 01 2013 To Date 01 04 2014 Output Should be Jan 2013 Feb 2
  • 验证错误:ul 不允许作为元素 span 的子元素

    我不明白为什么 WC3 验证器将此 HTML 标记为无效 它报告的错误是 在此上下文中 不允许元素 ul 作为元素 span 的子元素 抑制该子树中的更多错误 我正在使用 HTML5 此代码用于面包屑 span class bread ul
  • 如何以编程方式显示“清除默认值”?

    现在我正在开发一个家庭启动器应用程序 我想清除默认家庭启动器的默认设置 例如 三星主页 即 我想展示Settings gt Applications gt Manage Application gt Samsung Home gt clea
  • Swift:未定义的符号:iTunesApplication

    我现在正在尝试创建 Swift OS X 应用程序 但发现使用 ScriptingBridge 很困难 我包含了正确的 iTunes h 文件 并且当我将 iTunesApplication 写入类型时 Xcode 没有给出任何错误 但是
  • eclipse:链接资源的位置无效

    我尝试链接一个项目文件夹 该文件夹是特殊的 vista 文件夹 appdata roaming user myproj 它显示了上面的错误消息 什么阻止这样做 我尝试以管理员身份运行 但它给出了相同的消息 当然 从appdata文件夹中出来
  • 具有多个约束的 PostgreSQL 更新插入

    我正在尝试在有两个约束的表上进行更新插入 一种是a列是唯一的 另一种是b c d和e列一起是唯一的 我不想要的是 a b c d 和 e 一起是唯一的 因为这将允许两行在 a 列中具有相同的值 如果违反第二个约束 唯一的 b c d e 则
  • MSSQL 和 PHP:“传递给 sqlsrv_query 的参数无效。”

    我正在尝试通过 PHP 运行一个非常简单的 MSSQL 更新语句 但收到一条错误消息 无效的参数已传递给 sqlsrv query 这是连接等级 http pastebin com 3j4zmHJY我正在使用 最相关的是查询功能 funct
  • 控制 Android NDK 中的编译器标志?

    我知道我可以使用LOCAL CFLAGS将参数传递给编译器 然而 ndk build正在我的后面插入选项LOCAL CFLAGS 因此它们优先 例如我想指定 Ofast but ndk build adds O2在我自己的标志之后 并且由于
  • 在Python中处理大型密集矩阵

    基本上 在 python 中存储和使用密集矩阵的最佳方法是什么 我有一个项目 可以生成数组中每个项目之间的相似性度量 每个项目都是一个自定义类 并存储一个指向另一个类的指针和一个表示它与该类的 接近度 的数字 目前 它在处理约 8000 个
  • 为什么 Linq Contains 会生成此 SQL?

    背景 我正在开发一个系统 用于清理内部客户列表并找出联系人的电子邮件地址 其中我们已经拥有该公司其他人的电子邮件地址 为了做到这一点 我有 简化的 3 个表 Contacts ID CompanyId Email Domain 电子邮件域名
  • Fragment getArguments(在 onResume 中)返回 null

    我正在尝试将一些数据从活动发送到片段 我需要获取数据onResume片段的方法 但我想这是不可能的 参数只能在 onCreate 中接收吗 活动 public void someMethod String someString test B
  • 禁用触发的 Azure WebJob

    我有一个 webhook 触发的 Azure WebJob 由于 原因 我无法控制发送网络钩子的系统 每天发送一个 Webhook 它是唯一由 WebApp 托管的 WebJob 在测试过程中 我想禁用此 WebJob 因此我停止了 Web
  • “/”应用程序中的服务器错误。不提供此类页面

    我有一个主机 我用它托管了一个网页 cshtml扩大 我的主人是arvixe com它提供 ASP 和 NET 托管 但是当我尝试加载网页时 我收到此错误消息 应用程序中的服务器错误 不提供此类页面 说明 您请求的页面类型未被提供 因为它已
  • Microsoft Edge 不会在 Vagrant VM 中加载本地 nginx 网站

    我遇到了一个奇怪的问题 Microsoft Edge 无法加载托管在 vagrant 虚拟机内的本地 Craft CMS 网站 任何加载本地配置的主机名或 IP 的尝试都会返回 嗯 我们无法访问此页面 错误 我的主机文件中有一行 192 1
  • 使用“window.location.href”的函数中的两个 URL

    我正在使用 Marketo Embed 它允许我在使用时添加它 我的目标是使用 window location href 函数打开第一个 URL 一个 zip 文件 然后打开一个新 URL 一个页面 它仅使用其中一个 URL 如果它们是唯一
  • Gradle 找不到依赖项

    我正在尝试使用 Gradle 文件如下所示 Apply the java plugin to add support for Java apply plugin java In this section you declare where