Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将log4j添加到build.gradle_Java_Eclipse_Gradle_Log4j - Fatal编程技术网

Java 将log4j添加到build.gradle

Java 将log4j添加到build.gradle,java,eclipse,gradle,log4j,Java,Eclipse,Gradle,Log4j,尝试在我的Eclipse中创建简单的GradleJava项目。我使用的是LOG4J库,因此我的build.gradle看起来: plugins { // Apply the java-library plugin to add support for Java Library id 'java-library' } repositories { // Use jcenter for resolving dependencies. // You can decla

尝试在我的Eclipse中创建简单的GradleJava项目。我使用的是LOG4J库,因此我的build.gradle看起来:

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
    
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:28.2-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

我在该文件中添加了两行代码,希望可以下载log4j库:

compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'
但这看起来没什么帮助,因为若我用gradle构建项目,我会有编译错误

The compile configuration has been deprecated for dependency declaration. This will fail with an error in Gradle 7.0. Please use the implementation or api configuration instead. Consult the upgrading guide for further information: https://docs.gradle.org/6.3/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations
        at build_d9ael385qeshfiepcaw3797hi$_run_closure2.doCall(/home/a/Documents/workspace-sts/l4j/build.gradle:21)
        (Run with --stacktrace to get the full stack trace of this deprecation warning.)

> Task :compileJava FAILED
我想我以不推荐的方式声明了log4j(我从log4j手册中获取了这些行)。但是如何在
build.gradle
中声明
log4j
,以便在我的项目中使用它们呢

生成命令:

./gradlew build --refresh-dependencies --warning-mode all
我的主要课程是:

package l4j;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class aa {

    static Logger logger = Logger.getLogger(Log4jPropertiesConfigurationExample.class);
    public static void main(String[] args)
    {
        //PropertiesConfigurator is used to configure logger from properties file
        PropertyConfigurator.configure("log4j.properties");
 
        //Log in console in and log file
        logger.debug("Log4j appender configuration is successful !!");
    }

}
我想我以不推荐的方式声明了log4j(我从log4j手册中获取了这些行)

仅仅因为手册/文档中说用一种方法来做并不一定意味着它是准确的或正确的。文档可能是在
compile
配置未被弃用的时候编写的。随着
实现
api
配置的引入,不需要
编译
配置

只需将
compile
切换到implementation,弃用警告就会消失

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'