Error:java: javacTask: source release 8 requires target release 1.8 - java

Using IntelliJ IDE can't compile any projects. Screenshots of settings below:
Used JDK:
Project SDK and Language level:
Language Level:
Anybody have any ideas?

Go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler If on a Mac, it's under Intellij IDEA > Preferences... > Build, Execution, Deployment > Java Compiler
Change Target bytecode version to 1.8 of the module that you are working for.
If you are using Maven
Add the compiler plugin to pom.xml under the top-level project node:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
(Hoisted from the comments.)
Note: If you don't mind reimporting your project, then the only thing you really need to do is change the pom and reimport the project, then IntelliJ will pick up the correct settings and you don't have to manually change them.

You need to go to Settings and set under the Java compiler the following:
also check the Project Settings

This looks like the kind of error that Maven generates when you don't have the compiler plugin configured correctly. Here's an example of a Java 8 compiler config.
<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">
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- ... -->
</project>

The quickest way I found:
press:CTRL + SHIFT + A (For Mac ⌘ + SHIFT + A)
type: java compiler
press: ENTER
In the Settings window, set the Target bytecode to 1.8
(or 9 for java9)

There are two ways to solve this problem:
Set settings (File -> Settings -> Build, Execution, Deployment -> Java Compiler):
Add a build section to your pom.xml:

Many answers regarding Maven are right but you don't have to configure the plugin directly.
Like described on the wiki page of the Apache Maven Compiler Plugin you can just set the 2 properties used by the plugin.
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>

I fixed this by going to Project Structure -> Modules, find the module in question, click on Dependencies tab, change Module SDK to Project SDK.

I fixed it just by changing target compile version to 1.8. Its in:
File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler

You need to go to the /.idea/compiler.xml and change target to required jdk level.

In my case I fixed this issue by opening .iml file of project (it is located in project root folder and have name same as the name of project) and changing line <orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" /> to <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
I had everything configured as in others answers here but by some reason Idea updated .iml file incorrectly.

I fixed it by modify my POM file. Notice the last comment under the highest voted answer.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
The source must matches the target.

I just re-import maven button, then the error disappeared.

In your Gradle app level file >> compileOptions add this two lines
android {
compileOptions {
...
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
...
}
}

If you are working with Android-studio 1.3, Follow the below steps -
Go to File - Project Structure
Under modules- app-Properties tab, choose
Source Compatibility -1.8 and
Target Compatibility - 1.8.
And you are good to go.

Under compiler.xml file you will find :
<bytecodeTargetLevel>
<module name="your_project_name_main" target="1.8" />
<module name="your_project_name_test" target="1.8" />
</bytecodeTargetLevel>
and you can change the target value from your old to the new for me i needed to change it from 1.5 to 1.8

With Intellij, using Maven, you must check that Intellij has auto-imported your project.
You can check by clicking on the Maven tab on the right of your Editor.
If your Project is not here, then add the pom.xml file by clicking on +.
Obviously, the project must also have the relevant <build/> :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

I've just spent a while struggling with the same problem.
The only thing that worked for me was not using the built mvn (3.3.9) but pointing it to an external downloaded version (3.5.0). Finally the project opened and everything was good.

Don't forget to set dependencies for your module:

This issue occurs if your module is configured with Annotation processor and other module is not.Set the same configuration for all the modules as it wold be cyclic dependency.

the below code working fine by my side. I just add it in the pom.xml file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

For me, the problem was about Maven not able to find proper configurations, since these items were specified in parent pom.
Changing File -> Settings -> Build, Excecution, Deployment -> Maven -> User Settings file to point to my custom settings with proper repositories fixed the problem that was otherwise hiding.
Found out about the problem through Help -> Show log in explorer -> clicking the log file, when previously only got the error in the title and "java.lang.NullPointerException" in the console.

If none of the other answers work, check your Module SDK.
I had this error pop up for me after I had updated the project SDK to 1.8, the Javac compiler to 1.8, etc. The setting that was the problem for me was the "Module SDK".
(on Linux) Go to File > Project Structure... then in the window that opens, select Modules under Project Settings. Select the module in question from the list and then the Dependencies tab and make sure that Module SDK is set appropriately.

I have checked all of the above but the error still occurs.
But reimport all maven Projects (reload button inside Maven Projects panel) works in my case.

The only thing that helped me was to delete .idea/compiler.xml file.

Solution of the problem is very simple.You have to open .idea/compiler.xml file on your İdea Project and
You should write appropriate target version

Related

org.json cannot be resolved to a module?

I'm learning Java. To read JSON in my application, I downloaded this JSON library; which is an automatic module.
I included that library in my module descriptor like:
module art
{
exports art.anixt;
exports art.coartl;
exports art.runeape;
requires org.json; // org.json cannot be resolved to a moduleJava(8389908)
}
My settings.json in vscode:
{
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"java.format.settings.url": "Format.xml",
"java.format.settings.profile": "style",
"java.project.referencedLibraries": [
"lib/**/*.jar" // jar file showing in Referenced library(see screenshot)
]
}
How do I include the jar file in my module and import it into my Java file?
Screenshot:
TL;DR — As this unresolved 'Cannot be resolved' errors in projects with module-info.java issue reports, vscode is brain dead when it comes to JPMS and module-info.java.
The long-winded version
From my own experience, I can personally vouch for what the reporter of the above-linked vscode issue reports…
„…I've tried both Gradle and Maven…“
…
„…I find that Gradle and Maven will automatically refresh the classpath file and remove my modifications to it, which will bring back the errors…“
…
„…there needs to be module path information set in the classpath file in order for Eclipse to be happy, but there is no good way to do with that from Gradle or Maven…“
Proof that it's a vscode issue is that the exact same project — unchanged except for the removal of your comment — compiles perfectly fine in IntelliJ…
Since your project uses neither Maven nor Gradle — opting instead to use file-based dependency mgt with the jar in the lib folder — you're in even worse shape because you've eliminated the option of applying any JPMS-enabling plugins that could resolve the issue.
For example, by adding the following pom.xml with the appropriate configuration for the maven-compiler-plugin to my experimental version of your project…
…
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>--add-modules</arg>
<arg>org.json</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
…
…Maven does its magic and processes the module-info.java successfully…
I've successfully resolved other Stackers' JPMS woes by helping them apply that mrJar plugin mentioned in that vscode bug report. So if you're open to using Gradle instead of Maven, I could likewise advise you on how to configure that plugin too.

Unable to initialize main class selenium.Test [duplicate]

I have added the most updated Selenium dependency in my pom.xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
I ran
mvn clean install
inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
However when i try and run my main method, I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.
Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.
Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.
So when I changed maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>your_main_class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
to maven-shade-plugin plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your_main_class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
The issue was gone.
The difference between plugins you can find here.
In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.
Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.
To summarize:
try to change versions of Selenium, if it contains all dependencies;
try to add necessary dependencies if you don't have it;
try to check folder of maven if it has or not what says specific error;
try to play with plugins if nothing helps above.
NoClassDefFoundError
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.
The error you are seeing is :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
This clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available.
As you mentioned of looking into ~/.m2/repository folder, the maven folder structure for Selenium v3.7.1 (on Windows) is as follows :
C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java\3.7.1
So when you see a seleniumhq folder, it is pretty much expected.
What went wrong :
From all the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.
This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK / Maven / Gradle.
Solution :
Here are a few steps to solve NoClassDefFoundError :
While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
Remove the unwanted other <dependency> from pom.xml
Clean you Project Workspac within your IDE periodically only to build your project with required dependencies.
Use CCleane tool to wipe away the OS chores periodically.
While you execute a Maven Project always do maven clean, maven install and then maven test.
Encountered this error in Eclipse IDE. In Eclipse go to Project properties and in Java Build Path just add selenium jars in Classpath instead of Modulepath. Then under the Project tab on the top do a Clean to remove earlier buiid and then do a Run.
Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.
If you are opening from command line do:
mvn clean install -U
from your project path.
This is happening because you are selecting jar files under modulepath, you should add them under class path.
org.openqa.selenium is the package in the selenium-api-{version}.jar under the seleniumhq\selenium\selenium-api folder.
org.openqa.selenium.firefox is the package in the selenium-firefox-driver-{version}.jar under the seleniumhq\selenium\selenium-firefox-driver folder.
So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.
It's hard to say what caused NoClassDefFoundError exception without project structure and code detail. The exception is not the same as ClassNotFoundException. Maybe this answer https://stackoverflow.com/a/5756989/5374508 would be helpful.
What worked for me was to add this dependency to pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
I was getting below error from past 2 days and what helped me was to remove all the selenium extra dependencies like selenium-support, selenium-chrome-driver etc and only keeping the below dependencies in POM file.
Error:-
java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
Dependencies in the pom file after removing all other:-
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Have encountered this issue while running selenium test in eclipse IDE.
Navigate to following path:
1.Properties >> Java build path >> Libraries.
2.Add all selenium jars in Classpath instead of Modulepath.
3.Apply and close modal.
4.Now go to build path and click on "Configure Build Path".
5.Now run the selenium test.

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

I have added the most updated Selenium dependency in my pom.xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
I ran
mvn clean install
inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
However when i try and run my main method, I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.
Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.
Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.
So when I changed maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>your_main_class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
to maven-shade-plugin plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your_main_class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
The issue was gone.
The difference between plugins you can find here.
In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.
Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.
To summarize:
try to change versions of Selenium, if it contains all dependencies;
try to add necessary dependencies if you don't have it;
try to check folder of maven if it has or not what says specific error;
try to play with plugins if nothing helps above.
NoClassDefFoundError
NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.
The error you are seeing is :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
This clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available.
As you mentioned of looking into ~/.m2/repository folder, the maven folder structure for Selenium v3.7.1 (on Windows) is as follows :
C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java\3.7.1
So when you see a seleniumhq folder, it is pretty much expected.
What went wrong :
From all the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.
This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK / Maven / Gradle.
Solution :
Here are a few steps to solve NoClassDefFoundError :
While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
Remove the unwanted other <dependency> from pom.xml
Clean you Project Workspac within your IDE periodically only to build your project with required dependencies.
Use CCleane tool to wipe away the OS chores periodically.
While you execute a Maven Project always do maven clean, maven install and then maven test.
Encountered this error in Eclipse IDE. In Eclipse go to Project properties and in Java Build Path just add selenium jars in Classpath instead of Modulepath. Then under the Project tab on the top do a Clean to remove earlier buiid and then do a Run.
Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.
If you are opening from command line do:
mvn clean install -U
from your project path.
This is happening because you are selecting jar files under modulepath, you should add them under class path.
org.openqa.selenium is the package in the selenium-api-{version}.jar under the seleniumhq\selenium\selenium-api folder.
org.openqa.selenium.firefox is the package in the selenium-firefox-driver-{version}.jar under the seleniumhq\selenium\selenium-firefox-driver folder.
So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.
It's hard to say what caused NoClassDefFoundError exception without project structure and code detail. The exception is not the same as ClassNotFoundException. Maybe this answer https://stackoverflow.com/a/5756989/5374508 would be helpful.
What worked for me was to add this dependency to pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
I was getting below error from past 2 days and what helped me was to remove all the selenium extra dependencies like selenium-support, selenium-chrome-driver etc and only keeping the below dependencies in POM file.
Error:-
java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
Dependencies in the pom file after removing all other:-
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Have encountered this issue while running selenium test in eclipse IDE.
Navigate to following path:
1.Properties >> Java build path >> Libraries.
2.Add all selenium jars in Classpath instead of Modulepath.
3.Apply and close modal.
4.Now go to build path and click on "Configure Build Path".
5.Now run the selenium test.

List actually used maven plugins in XML format for pom

It is an often helpful practice to declare the versions of all used maven plugins in the pom.xml, e.g. to make old builds reproducible after a new plugin version comes out. For a fresh project: is there a way to output the plugins currently used in the build in the XML-format I could easily put into the pluginManagement section of the pom.xml?
I found that versions:display-plugin-updates does output the plugins with versions that are not yet fixed, and help:effective-pom does display the actually used versions implicitly in <build><plugins>, but garbled with the configuration, executions and other information not needed for the pluginManagement section. So I'm looking for something easier - just like mvn -DoutputXML=true dependency:analyze does for dependencies.
How about parsing the output of help:effective-pom ?
With grep for a quick-and-dirty solution :
$ mvn help:effective-pom -Doutput=effective-pom.xml
$ grep "<plugin>" -A 3 effective-pom.xml
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
[...]
Or with an XML Parser and the use of XSLT or XQuery for a more robust solution.
For example, the following XQuery code
declare default element namespace "http://maven.apache.org/POM/4.0.0";
string-join(
for $plugin in //plugin
return string-join(($plugin/groupId/text(), $plugin/artifactId/text(), $plugin/version/text()), ":"),
"
"
)
Will produce this kind of output :
<?xml version="1.0" encoding="UTF-8"?>maven-antrun-plugin:1.3
maven-assembly-plugin:2.2-beta-5
maven-dependency-plugin:2.8
maven-release-plugin:2.3.2
maven-source-plugin:3.0.1
maven-clean-plugin:2.5
maven-install-plugin:2.4
maven-deploy-plugin:2.7
maven-site-plugin:3.3
Note that the xml header could be removed with the appropriate configuration.

Debugging Annotation processors in eclipse

I am writing a simple annotation processor and trying to debug it using eclipse. I created a new project for annotation processor and configured javax.annotation.processing.Processor under META-INF as needed and it processes annotations fine.
Then, I added some more code and tried debugging, but could never make the execution stop at the breakpoints added in the annotation processor. I am compiling using ant and I am using the following ANT options.
export ANT_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
After triggering ant build, i go create a remote debug configuration and the debugger starts fine. Ant build also starts successfully. But the execution never stops at any break point added in the annotation processor.
This is a problem I just ran into, and the eclipse plugin solution seems super cumbersome to me. I found a simpler solution using javax.tools.JavaCompiler to invoke the compilation process. Using the code below, you can just Right-Click > Debug As > JUnit Test in eclipse and debug you annotation processor directly from there
#Test
public void runAnnoationProcessor() throws Exception {
String source = "my.project/src";
Iterable<JavaFileObject> files = getSourceFiles(source);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
CompilationTask task = compiler.getTask(new PrintWriter(System.out), null, null, null, null, files);
task.setProcessors(Arrays.asList(new MyAnnotationProcessorClass()));
task.call();
}
private Iterable<JavaFileObject> getSourceFiles(String p_path) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager files = compiler.getStandardFileManager(null, null, null);
files.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(new File(p_path)));
Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);
return files.list(StandardLocation.SOURCE_PATH, "", fileKinds, true);
}
This question has been posted over 6 years ago, however, I ran into the same problem now and still couldn't find a good answer on the Internet.
I was finally able to work out a good setup that allows me to develop an Annotation Processor, use it in compilation of another project, and debug it as needed.
The setup is like this:
Annotation Processor developed in a project with GAV:
<groupId>infra</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0-SNAPSHOT</version>
In the annotation-processor POM file I specified the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Notice the <compilerArgument>-proc:none</compilerArgument> specification.
In the project where the annotation-processor is used, it is used during the compilation of the project. I.e. the annotation-processor is invoked during the execution of the compiler, javac. I found that in order to debug the annotation-processor execution while running javac directly, I can use the following command line:
javac -J-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044 -d target/classes -proc:only -processor infra.annotation.CustomizationAnnotationProcessor -cp ../annotation-processor/target/annotation-processor-1.0-SNAPSHOT.jar src\main\java\org\digital\annotationtest\MyTestClass.java
Notice the suspend=y part in the command line of javac. This tells the JVM to suspend execution until the debugger attaches to it.
In this situation, I can start the eclipse debugger by starting a Remote Java Application Debug Configuration. Configure it to use the annotation-processor project, and attach to the process on localhost and port 1044. this allows you to debug the annotation processor code. If you set a breakpoint in the init or process methods, the debugger will break.
In order to enable the same debug experience while compiling using Maven, I setup the POM file as follows:
Add a dependency to the POM where the annotation-processor is used:
<dependency>
<groupId>infra</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In the same project using the annotation-processor define the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerArgs>
<compilerArg>-J-verbose</compilerArg>
<compilerArg>${enableDebugAnnotationCompilerArg}</compilerArg>
</compilerArgs>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>infra</groupId>
<artifactId>annotation-processor</artifactId>
<version>1.0-SNAPSHOT</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>infra.annotation.CustomizationAnnotationProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>debugAnnotation</id>
<properties>
<enableDebugAnnotationCompilerArg>-J-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044</enableDebugAnnotationCompilerArg>
</properties>
</profile>
</profiles>
Notice the use of <fork>true</fork>,
and <compilerArg>${enableDebugAnnotationCompilerArg}</compilerArg>.
Also, notice the profile deinition of debugAnnotation and the definition of
the <enableDebugAnnotationCompilerArg> property.
This allows us to start a debugging session of the annotation-processor
by running mvn -P debugAnnotation package and attaching the eclipse debugger to the compiler
process the same way as described in 4 above.
The easiest way is to create an eclipse plugin and then debug it directly from eclipse.
It sound a lot harder then it is - this: https://www.youtube.com/watch?v=PjUaHkUsgzo is a 7 minute guide in youtube that can get you started.

Categories

Resources