vic*_*ico 2 java eclipse log4j 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'
}
Run Code Online (Sandbox Code Playgroud)
我在此文件中添加了两行,我希望它们能够下载 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'
Run Code Online (Sandbox Code Playgroud)
但看起来这没有帮助,因为如果我使用 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
Run Code Online (Sandbox Code Playgroud)
我想我以不推荐使用的方式声明了 log4j(我从 log4j 手册中获取了这些行)。但是如何声明才能log4j在build.gradle我的项目中使用它们呢?
构建命令:
./gradlew build --refresh-dependencies --warning-mode all
Run Code Online (Sandbox Code Playgroud)
我的主要课程:
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 !!");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您偶然发现此答案,请在复制和粘贴之前检查 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'
可以在此处找到更新的版本。
| 归档时间: |
|
| 查看次数: |
21338 次 |
| 最近记录: |