簡體   English   中英

將 log4j 添加到 build.gradle

[英]Adding log4j to build.gradle

嘗試在我的 Eclipse 中創建簡單的 Gradle Java 項目。 我正在使用 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配置未被棄用的時候compile 隨着implementationapi配置的引入,不需要compile配置。

只需將compile切換到實現,棄用警告就會消失。

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'

如果您偶然發現此答案,請在復制和粘貼之前檢查 log4j-core 的非易受攻擊版本。

在撰寫本文時,2.16.0 是唯一一個不受 JNDI 查找攻擊影響的版本,這些攻擊非常容易被利用。 您可以獲得有關此CVE 報告的更多數據。 以及在此 Apache 安全披露頁面上有關受不同安全漏洞影響的所有版本的詳細信息。

版本 1.x 應該可以安全地避免這種特定的漏洞利用,但可能容易受到其他漏洞利用的影響,具體取決於版本 自 2015 年 8 月 5 日起,它也已結束生命周期,使用它可能是個壞主意。

這就是說,在現代 Gradle 上,應該使用“依賴項”部分中的“實現”來引入這種依賴項。

一個例子是:

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

“短”替代方案:

implementation 'org.apache.logging.log4j:log4j-core:2.16.0'

可以在此處找到更多最新版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM