如何在Eclipse Oxygen中将Log4j与Maven和Java9结合使用? [英] How to use log4j with maven and java9 in Eclipse Oxygen?
问题描述
我尝试将Java8项目迁移到Java9.自动生成的module-info.java包含一个条目
I try to migrate a Java8 project to Java9. The auto generated module-info.java contains an entry
requires log4j;
并显示错误:
log4j cannot be resolved to a module
=>如何正确地将log4j作为Java9的模块依赖项包括在内?
=> How do I correctly include log4j as a module dependency with Java9?
(对于以下依赖项,我也有同样的问题: 需要hibernate.core; 需要hibernate.jpa.2.1.api; 需要jcommander; 需要junit; 需要反思; )
(I have the same issue for following dependencies: requires hibernate.core; requires hibernate.jpa.2.1.api; requires jcommander; requires junit; requires reflections; )
我到目前为止所做的事情:
- 已安装Java 9.0.1
- 将Eclipse升级到Oxygen.1a版本(4.7.1a)
- 将我的Java项目的合规性级别更改为9
-
生成的module-info.java,右键单击project => Configure => Generate module-info.java
- Installed Java 9.0.1
- Upgraded Eclipse to Oxygen.1a Release (4.7.1a)
- Changed Compliance level of my Java project to 9
Generated module-info.java with Right click on project=>Configure=>Generate module-info.java
Updated the plugins in my pom.xml file (also see https://cwiki.apache.org/confluence/display/MAVEN/Java+9+-+Jigsaw) and set java version to 9:
<!-- plugin for compile phase (and test-compile phase) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<!-- specify current java version here: -->
<source>9</source>
<target>9</target>
</configuration>
</plugin>
推荐答案
module-info.java中的错误
即使项目的遵从级别已设置为Java9,Eclipse中的module-info.java也会显示误导性错误.
Even if the compliance level of the project has been set to Java9, there might be shown misleading errors for module-info.java in Eclipse.
我希望仅当我更改pom.xml文件时才需要更新maven项目.现在我了解到,更改module-info.java还需要手动更新maven项目.
I expected that an update of the maven project would only be required if I change the pom.xml file. Now I learned that changing the module-info.java also requires a manual update of the maven project.
=> 更新Maven项目(Alt + F5)
在更新之后,我的错误消失了.
After that update my errors vanished.
我还了解到,最好先更新pom.xml文件中的版本,然后生成module-info.java.否则,module-info.java将包含不存在的模块,例如"requires log4j",而不是"requires log4j.api"
I also learned that it is better to first update the versions in the pom.xml file and then generate the module-info.java. Otherwise the module-info.java will include non-existing modules like "requires log4j" instead of "requires log4j.api"
module-info.java中的另一个误导性错误可能是由于pom打包引起的,请参见下文.
Another misleading error in module-info.java might occur due to pom packaging, see below.
未解决的进口
对于无法解决导入的问题,问题可能是该(旧)导入需要哪个对应的(新)模块?".可能有帮助的地方:
For the case that an import can not be resolved the question might be "Which corresponding (new) module do I need for that (old) import?". What might help here:
-
在下一页搜索所需的页面: http://cr.openjdk.java.net/~ mr/jigsaw/ea/module-summary.html (列出所有导出的JDK模块的软件包)
Search at following page for the required page: http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html (lists all exported packages of the JDK-modules)
在旧的* .jar文件上使用JDeps获取所需的jar文件的列表,例如
Use JDeps on the (old) *.jar file to get a list of required jar files, e.g.
jdeps --class-path"lib"-递归MY-OLD.jar> output.txt
jdeps --class-path "lib" -recursive MY-OLD.jar >output.txt
使用jar查找jar文件的模块
Use jar to find the module for a jar file
jar --describe-module --file REQUIRED-JAR-FILE.jar
jar --describe-module --file REQUIRED-JAR-FILE.jar
另请参阅:
-
https://blog.codefx.org/tools/jdeps-tutorial-analyze-java-project-dependencies/#Getting-To-Know-JDeps
重新导出依赖项
为了对祖父母项目自动显示log4j
In order to automatically make log4j visible for a grandparent project
grandparent => parent => log4j
您可能想使用
requires transitive log4j.api
在父级而不是
requires log4j.api
在祖父母中.另请参阅:
in grandparent. Also see:
> Required和require之间有什么区别Java 9模块声明中的传递语句
POM包装
我的主要问题似乎是我的Java8 pom.xml文件使用了pom封装:
My main issue seems to be that my Java8 pom.xml file used pom packaging:
<packaging>pom</packaging>
如果我删除该行,则module-info.java中不会显示任何错误
If I remove that line, no errors are shown in module-info.java
也请参见以下额外问题:如何在Java9.0.1和Eclipse Oxygen 1a发行版(4.7.1a)中的pom打包?
Also see this extra question: How to use maven with Java9.0.1 and pom packaging in Eclipse Oxygen 1a Release (4.7.1a)?
新的log4j版本
A.除了更改"requires log4j" =>"requires log4j.api"外,我还必须针对与Java9兼容的新log4j版本修改调用代码:
A. In addition to the change "requires log4j" => "requires log4j.api" I had to adapt the calling code for the new log4j version that is compatible to Java9:
私有静态Logger sysLog = Logger.getLogger(Main.class);
private static Logger sysLog = Logger.getLogger(Main.class);
到
私有静态Logger sysLog = LogManager.getLogger(Main.class);
private static Logger sysLog = LogManager.getLogger(Main.class);
B. log4j 2没有PropertyConfigurator.另请参阅以下相关问题:
B. log4j 2 does not have PropertyConfigurator. Also see this related question:
C. log4j 2不支持log4j.properties文件.以前我用
C. log4j 2 does not support log4j.properties files. Previously I used
src/main/resources/META-INF/log4j.properties
src/main/resources/META-INF/log4j.properties
进行配置.现在我用
src/main/resources/log4j2.xml
src/main/resources/log4j2.xml
另请参见
工作示例作为参考
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Log4JWithJava9</groupId> <artifactId>Log4JWithJava9</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <release>9</release> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.9.1</version> </dependency> </dependencies> </project>
module-info.java:
module-info.java:
module Log4JWithJava9 { requires javafx.base; requires log4j.api; }
Main.java:
Main.java:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Main { private static Logger sysLog = LogManager.getLogger(Main.class); public static void main(String[] args) { } }
Eclipse插件
为了将log4j添加到eclipse插件中,我将文件log4j-api-2.10.0.jar复制到文件夹"lib"中,并将jar文件添加到Java Build Path => Libraries => ModulePath
In order to add log4j to an eclipse plugin, I copied the file log4j-api-2.10.0.jar in a folder "lib" and added the jar file to Java Build Path=>Libraries=>ModulePath
我不得不使用
requires org.apache.logging.log4j
这篇关于如何在Eclipse Oxygen中将Log4j与Maven和Java9结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!