I try to migrate a Java8 project to Java9. The auto generated module-info.java contains an entry
requires log4j;
and an error is shown:
log4j cannot be resolved to a module
=> How do I correctly include log4j as a module dependency with Java9?
(I have the same issue for following dependencies:
requires hibernate.core;
requires hibernate.jpa.2.1.api;
requires jcommander;
requires junit;
requires reflections;
)
What I did so far:
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>
Updated log4j version in pom.xml file since log4j 1.2 does not seem to work with Java9 (see https://blogs.apache.org/logging/entry/moving_on_to_log4j_2)
<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>
Errors in 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.
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.
=> update the maven project (Alt+F5)
After that update my errors vanished.
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"
Another misleading error in module-info.java might occur due to pom packaging, see below.
Unresolved imports
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:
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)
Use JDeps on the (old) *.jar file to get a list of required jar files, e.g.
jdeps --class-path "lib" -recursive MY-OLD.jar >output.txt
Use jar to find the module for a jar file
jar --describe-module --file REQUIRED-JAR-FILE.jar
Also see:
What are the predefined modules in JDK9 or Which module do I need to fix dependency problems?
https://blog.codefx.org/tools/jdeps-tutorial-analyze-java-project-dependencies/#Getting-To-Know-JDeps
Re-Export dependencies
In order to automatically make log4j visible for a grandparent project
grandparent => parent => log4j
you might want to use
requires transitive log4j.api
in parent instead of
requires log4j.api
in grandparent. Also see:
What's the difference between requires and requires transitive statements in Java 9 module declaration
POM packaging
My main issue seems to be that my Java8 pom.xml file used pom packaging:
<packaging>pom</packaging>
If I remove that line, no errors are shown in module-info.java
Also see this extra question: How to use maven with Java9.0.1 and pom packaging in Eclipse Oxygen 1a Release (4.7.1a)?
New log4j version
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:
private static Logger sysLog = Logger.getLogger(Main.class);
to
private static Logger sysLog = LogManager.getLogger(Main.class);
B. log4j 2 does not have PropertyConfigurator. Also see this related question:
PropertyConfigurator in log4j2
C. log4j 2 does not support log4j.properties files. Previously I used
src/main/resources/META-INF/log4j.properties
for configuration. Now I use
src/main/resources/log4j2.xml
Also see
Log4j 2 doesn't support log4j.properties file anymore?
Converting log4j.properties to log4j.xml
Working example as a reference
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 Log4JWithJava9 {
requires javafx.base;
requires log4j.api;
}
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 plugins
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
Instead of requires log4j.api I had to use requires org.apache.logging.log4j
I was able to make this work somehow.
I downloaded the latest Eclipse Photon Release (4.8.0), I don't know if this fix will work with older versions.
I changed the module-info.java to have the following lines:
requires log4j.api;
requires log4j.core;
I had to switch to an earlier version of log4j, 2.8.2.
I changed all my imports that say import org.apache.logging.log4j.*; to not use the wildcard. So they changed to: import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
That's it, it works with both Maven and Eclipse.
Related
I need to use BaseDetectorTest provided from one of Spotbugs extension library
I added the maven dependency from (FindBugs Test Utility)
But it does not include the BaseDetectorTest class file (Once Maven is updated, the jar file is added to the external libraries - but not the class file).
I am wondering why it happens.
My guess is "the Jar file provided by the repository is still being developed"
Could you teach me how to fix it?
find-sec-bugs/findsecbugs-test-util/src/test/java/com/h3xstream/findbugs/test/BaseDetectorTest.java is a test class. .../src/test/... and ...Test.java are indicators for that. Test classes aren't included in a project's JAR (by the jar:jar goal of the Maven JAR Plugin which is the default binding for the package phase) but in a project's ...-tests.jar which is created by the jar:test-jar goal.
On MvnRepository select a version tag, e.g. 1.9.0, then Files jar (2 KB) View All to find the ...-tests.jar. Use it with:
<dependency>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findbugs-test-util</artifactId>
<version>1.9.0</version>
<classifier>tests</classifier>
</dependency>
On Maven Central you can get a later version (1.11.0), select it and then Browse 📁 to find it. Use it with:
<dependency>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-test-util</artifactId>
<version>1.11.0</version>
<classifier>tests</classifier>
</dependency>
Maven throws a strange warning while builduing our multi-module project. I'm just referencing the jar and test-jar of the same project in another project. Both dependencies have test scope. Im running Maven 3.3.1 and cannot upgrade the version easily.
Does anyone of you have an idea how I could solve the problem without getting this warning from maven?
pom.xml of ProjectA:
<dependency> <!-- This is line 130 -->
<groupId>${project.groupId}</groupId>
<artifactId>projectB</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>projectB</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Warnings from maven (anonymized):
[WARNING] Some problems were encountered while building the effective model for org.group.ProjectA:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: ${project.groupId}:org.group.ProjectB:jar -> duplicate declaration of version ${project.version} # org.group.ProjectA, /var/lib/jenkins/jobs/nicejob/workspace/org.group.ProjectA/pom.xml, line 130, column 15
Looking at test-jar documentation I would say the two artifact are basically the same one, and that the "test-jar" one is not expected to be used aparte from test phase since it contains test classes.
A good approach could be:
Leave the "standard" dependencies in compile scope (if you really need it for non-test classes it provides)
Use the "test-jar" dependencies as an additional dependency declaration (with test scope) of the surefire-plugin only, so that it's used only by the plugin itself
I have my system set up to compile jee6 code from the command line (no IDE). I recently tried to compile some code that uses javax.enterprise.context.RequestedScope. The code compiled fine from the command prompt but when I tried to build the application using Maven I keep getting javax.enterprise.context package not found error.
What gives? I thought Maven was using the same javac I'm using. Why can javac find the package but Maven can't? Do I need to add dependencies for java packages?
Or...is there a way to tell Maven to use the current class files when building or does Maven have to compile when it builds?
Thanks.
Try adding the Java EE 6 jar to your Maven dependencies:
<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/maven-v4_0_0.xsd">
. . .
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
This will add the Java EE classes to your compile-time classpath.
To compile your code with Maven, you need to tell it where to find required components.
Looks like you need to add this to your dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
Note the <scope> element. It tells Maven not to bundle this dependency with your artifact, because at run time it will be provided by the application container.
On the reason why it compiles from the command line.
You probably have the required jar file on the command line -cp option or in your global CLASSPATH variable.
Maven, by design, does not pay attention to the global CLASSPATH.
I am trying to add Hector dependencies to my POM. My IDE is Eclipse, and I am also using m2eclipse. Adding dependencies of JAR type is not a problem, but this dependency is of type POM. I have tried almost everything usual including cleaning, building, and using import scope but nothing seem to have helped. When I try to add import me.prettyprint.hector.api.Serializer;
I get the error "The import cannot be resolved".
Is there anything else I need to do to use POM type dependencies or is there a better way of using dependencies of POM types in the project?
I believe his question is not as obvious as simply including the necessary dependency. I have experienced this problem too and am looking for a solution. The problem can be clearer stated as the following:
Let's say I have two maven projects (project A and project B). Project A is a simple web-app which wants to include dependencies as stated in project B. However, project B packaging type is "pom". This should allow all of project B's dependencies to be included into project A. Here is an example:
Project A (packaging is "war"):
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
</dependencies>
Project B (packaging is "pom")
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
What we'd like to see in Eclipse is when you run maven eclipse:eclipse on Project A, that you can see the commons-lang-2.4.jar file as a dependency under project A such that you can resolve it in your code when imported. This is not happening and I'm still looking for such a solution.
The error indicates that the relevant class is missing in your classpath. A search of this class indicates, it is available in hector-core
This discussion indicates how this dependency can be imported, viz. adding the following entry to your project pom (or choosing this appropriately in m2eclipse).
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>0.7.0-29</version>
</dependency>
I have a maven project with the following POM snippet:
<modelVersion>4.0.0</modelVersion>
<artifactId>Foo-Deploy</artifactId>
<name>Foo-Deploy</name>
<packaging>pom</packaging>
<description>foobar</description>
<dependencies>
<dependency>
<groupId>de.foo.bar</groupId>
<artifactId>some-api</artifactId>
<version>${project.version}</version>
<classifier>doc</classifier>
<type>zip</type>
</dependency>
</dependencies>
The idea is to have a dependency defined in which some sources are (this is created successfully before).
Now I want to run javadoc on exactly THIS dependency. When I call
mvn javadoc:jar -DincludeDependencySources=true -DdependencySourceIncludes=de.foo.bar:some-api:*:doc:zip
it fails with the message
Not executing Javadoc as the project
is not a Java classpath-capable
package
what is wrong ? and would it work anyhow ?
or how can I generate javadoc from a specific dependency (assuming this project has more dependencies) ?
Thanks
To generate javadoc for dependent sources, a sequence of steps needs to be done. These are outlined in this link.
Essentially you need to ensure that the source files of the dependency is generated/available and <includeDependencySources> parameter is enabled.