Gradle没有找到依赖关系

Gradle没有找到依赖关系

本文介绍了Gradle没有找到依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //使用java插件添加对Java的支持
apply plugin:'java'

//在本节中,您声明从哪里可以找到项目的依赖关系
repositories {
//使用'jcenter'解决你的依赖关系。
//你可以在这里声明任何Maven / Ivy /文件库。
mavenCentral()
jcenter()
}

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

//在本节中,将声明生产和测试代码的依赖项
dependencies {
//生产代码使用编译时SLF4J日志API
编译'org.slf4j:slf4j-api:1.7.12'
编译'org.apache.logging.log4j:log4j:2.3'

//声明你想用在你的测试中的你最喜欢的测试框架的依赖关系。
// Gradle Test任务也支持TestNG。只需将
// testCompile依赖项更改为testCompile'org.testng:testng:6.8.1'并将
//'test.useTestNG()'添加到您的构建脚本中。
testCompile'junit:junit:4.12'
}

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

  package execute; 

导入消息。*;
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);


$ b $ / code $ / pre

当我执行语句<$

  D:\ c $ c $> Java\entrypoint\src\main\java\execute\Entry.java:4:错误:程序包org.apache.log4j不存在
import org.apache.log4j.Logger;
^
D:\Java\entrypoint\src\main\java\execute\Entry.java:12:error:can not find symbol
LOGGER.info( 收到消息:+味精);
^
符号:变量LOGGER
位置:类入口
2错误
:compileJava失败

失败:构建失败,出现异常。

*出错:
任务'compileJava'的执行失败。
>编译失败;详细信息请参阅编译器错误输出。

*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。

构建失败

总时间:6.261秒
编译失败;详细信息请参阅编译器错误输出。

我做错了什么?

更新
我将我的代码更改为:

  package execute; 

导入消息。*;
导入org.apache.logging.log4j.Logger;
导入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!);


$ b

编译器抱怨:

 线程main中的异常java.lang.NoClassDefFoundError:org / apache / logging / log4j / LogManager 
at execute.Entry 。< clinit>(Entry.java:9)
导致:java.lang.ClassNotFoundException:org.apache.logging.log4j.LogManager $ b $ 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

更新2

执行后:

 > gradle依赖关系
:依赖关系

- -------------------------------------------------- ---------
根项目
------------------------ ------------------------------------

archives - 配置存档工件。
没有依赖项

compile - 为源集main编译类路径。
\ --- org.apache.logging.log4j:log4j-api:2.3

default - 默认工件的配置。
\ --- org.apache.logging.log4j:log4j-api:2.3

runtime - 源代码集main的运行时类路径。
\ --- org.apache.logging.log4j:log4j-api:2.3

testCompile - 为源集test编译classpath。
+ --- org.apache.logging.log4j:log4j-api:2.3
\ --- junit:junit:4.12
\ --- org.hamcrest:hamcrest-核心:1.3

testRuntime - 源代码集test的运行时类路径。
+ --- org.apache.logging.log4j:log4j-api:2.3
\ --- junit:junit:4.12
\ --- org.hamcrest:hamcrest-核心:1.3

建立成功


解决方案

而不是

  import org.apache.log4j.Logger; 

使用

  import org.apache.logging.log4j.Logger; 

并定义LOGGER的实例

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

查看样本



这与Gradle


没有任何关系

I am trying to use Gradle and the file looks like:

// 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'
}

As you can see, I've added two dependencies and want to use in a class

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);

    }
}

When I execute the statement gradle assemble, I've got the compiler error.

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.

What am I doing wrong?

UpdateI changed my code to this:

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!");

    }
}

The compiler complain:

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
After execute:

>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
解决方案

Instead of

import org.apache.log4j.Logger;

use

import org.apache.logging.log4j.Logger;

and define the instance of LOGGER

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

See this samplehttp://logging.apache.org/log4j/2.x/manual/api.html

this has nothing todo with Gradle

这篇关于Gradle没有找到依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 16:59