Debugging Annotation processors in eclipse - java

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.

Related

Is it possible to configure maven to compile generated sources wouthout the use of a plugin?

I know this question is not new. But it seems that there is no definite answer. This answer from 2012 states that if generated sources are placed under target/generated-sources/<tool> they will be compiled. ANTLR 4 maven plugin follows this paradigm. Per documentation, the default value of outputDirectory is: ${project.build.directory}/generated-sources/antlr4.
Now in my case I have a custom tool that generates sources. I've set its output directory to be at ${project.build.directory}/generated-sources/whatever and it didn't work. Regarding the whateverpart, I've tried to use the id of the goal that generates the sources and even tried to hijack antlr4 name. No result though.
When I try this solution that suggests using mojo build-helper-maven-plugin it compiles as expected. But according to maven guide to generating sources it should be working without any helper plugin, shouldn't it? Am I missing something?
Here is the POM (fragment) configuration that I use to generate the sources.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>generate-code</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>com.company.product</groupId>
<artifactId>CodeGenerator</artifactId>
</executableDependency>
<arguments>
<argument>${basedir}/</argument>
<argument>${project.build.directory}/generated-sources/generate-code/</argument>
</arguments>
<mainClass>com.company.codegeneration.CodeGenerator</mainClass>
</configuration>
<dependencies>
<dependency>
<groupId>com.company.product</groupId>
<artifactId>CodeGenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
Your understanding is just a bit incorrect.
Nothing automatic, plugins generating source code typically handle that by adding their output directory (something like target/generated-sources/ by convention) as source directory to the POM so that it will be included later during the compile phase.
Some less well implemented plugins don't do that for you and you have
to add the directory yourself, for example using the Build Helper
Maven Plugin.
As the other answer noted, most plugins typically add the generated code as new source path.
Ex: See antlr4's Antlr4Mojo.java class. Here, the plugin is adding the generated classes to project source by calling addSourceRoot method in execute method.
// Omitted some code
void addSourceRoot(File outputDir) {
if (generateTestSources) {
project.addTestCompileSourceRoot(outputDir.getPath());
}
else {
project.addCompileSourceRoot(outputDir.getPath());
}
}
// Omitted some code
#Override
public void execute() throws MojoExecutionException, MojoFailureException {
// Omitted code
if(project!=null)
{
// Tell Maven that there are some new source files underneath the output
// directory.
addSourceRoot(this.getOutputDirectory());
}
}
// Omitted some code
So, you can either do this in your custom plugin or use the build-helper-maven-plugin.

How to debug Spring Boot with Netbeans via Maven

After fiddling around for way too long till I got proper debuging setup in Netbeans 8.2 with Spring Boot 1.4.3 I figured I write down my findings as Q&A for others.
The problem is that the default configuration for Netbeans fails to properly launch Spring in debug mode and when you search the internet you only find the outdated information in the Spring docs that won't work.
The solution is simple if you know how. Please find the correct setup instructions below.
Tested and works with Netbeans 8.2 and Spring-Boot 1.4.3:
First of all make sure you have the Spring Maven plugin included (this should be already included when making a new Netbeans Spring project):
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
Also it is a good idea to include the Spring Devtools like this:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
...
</dependencies>
Now navigate to your project settings -> Actions -> Debug project and set the following:
Execute goals:
spring-boot:run
Set properties:
run.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}
jpda.listen=true
Now run your application via the usual debug button and Spring should properly connect to the JVM debugger.
Spring Boot 2.x
To enable Netbeans debugging for a Spring Boot 2.x project (and more specifically version 2.x of the spring-boot-maven-plugin) the procedure is exactly the same, except the run.jvmArguments property name has changed to spring-boot.run.jvmArguments:
spring-boot.run.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}
jpda.listen=true
Testing NetBeans 8.2 and Spring Boot 2.0.1, I was not able to make things work following #TwoThe's instructions. First, I encountered an issue where all I saw was "JPDA Listening Start..." in the output window. To resolve that problem, I added Spring Devtools as an optional dependency. Second, even though debugging appeared to be running okay, the "Debugging" window, which normally displays the list of active threads, was empty and breakpoints that I set were not triggered. Third, attempting to stop the debugging session by pressing the red "Finish Debugger Session" button would not stop the Tomcat server.
Instead of changing the execute goals to "spring-boot:run", I found that it was sufficient to use the default "Debug project" action execute goals:
process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
.. and properties:
exec.args=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}
exec.executable=java
jpda.listen=true
(As a sidenote, debugging as a regular Java application is apparently the recommended approach to debugging Spring Boot applications in Eclipse; see How to debug Spring Boot application with Eclipse?)
One helpful tip is that if you want to debug using a certain Spring Boot profile, say "debug", you can prepend "-Dspring.profiles.active=debug " to the "exec.args" property. See also: Spring boot running a fully executable JAR and specify -D properties
Tested on NetBeans9
Action: Add any name
Set Properties: select Add> button, select Debug Maven Build
And debug as always -> IDE debug button
If you are still having the problem after applying all above mentioned fixes, remove all your breakpoints and try again.
Window -> Debugging -> Breakpoints -> Delete All Breakpoints
POW
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
buld
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Debug Project
Excute Goals : package
Set Properties:netbeans.deploy.debugmode=true netbeans.deploy=true
Change
Excute Goals : spring-boot:run
Set Properties: spring-boot.run.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address}
jpda.listen=true
and Netbeans Press debug project -- not navigator--> spring-boo-run ... What was the difference? spring-boot.run.jvmArguments:

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

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

GWT CodeServer incremental compilation issue with maven and the new packaging gwt-app and gwt-lib

I've been toying around with the latest changes in the gwt-maven-plugin. Most notably, the new packagings gwt-app and gwt-lib.
To my understanding, if I have some code that I'd like to reuse between different GWT apps, gwt-lib packages all needed sources and *.gwt.xml files in the jar right next to all classes. It works like a charm.
If I opt for a multi-module maven reactor build, everything is detected on compile time and I'm able to build and deploy successfully without any hassle. If I try to develop however, the shiny GWT 2.7 SuperDevMode is unable to detect changes in the gwt-lib projects, obviously because they are referenced from the jars and not the actual sources directory where they were changed.
To illustrate, I used the modular-requestfactory archetype by Thomas Broyer.
mvn archetype:generate \
-DarchetypeCatalog=https://oss.sonatype.org/content/repositories/snapshots/ \
-DarchetypeGroupId=net.ltgt.gwt.archetypes \
-DarchetypeArtifactId=modular-requestfactorcom.testy \
-DarchetypeVersion=1.0-SNAPSHOT
and I entered the following information:
Define value for property 'artifactId': : mvngwt
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.test: :
Define value for property 'module': App: : MvngwtApp
Define value for property 'module-short-name': mvngwtapp: :
Afterwards I created an additional maven module called "mvn-gwt-client-api", which contains a single class that is to be used by the mvn-gwt-client. The end structure looks like this:
mvngwt/
--mvngwt-client/
--mvngwt-client-api/
--mvngwt-server/
--mvngwt-shared/
--pom.xml
The goal is to be able to edit the files in mvngwt-client-api (e. g. the only class currently: MvngwtApi.java), then recompile in SuperDevMode and actually see the changes immediately without restarting the CodeServer.
A working copy of the project can be found here: https://github.com/elnicko/maven-gwt-test
PS: I tried to work it out with the build-helper-maven-plugin:
<profiles>
<profile>
<!-- elnicko: add to support CodeServer hot compile for referenced libraries -->
<id>env-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-shared-sources-to-classpath</id>
<!--
<phase>process-classes</phase>
<phase>compile</phase>
-->
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../mvngwt-client-api/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
However it didn't improve the situation.
Any hints/pointers/ideas greatly appreciated.
Edit:
I am able to use the SuperDevMode incremental compilation by using the aforementioned build-helper-maven-plugin configuration, changing the mvngwt-client-api packaging from "gwt-lib" to "jar", and adding a "maven-source-plugin". That way maven compilation and deployment work the same way, but the CodeServer is made aware of the changes in the source directory of mvngwt-client-api. Nevertheless, the question remains open, how one can use the new "gwt-lib" without losing the CodeServer incremental compilation. The diff may be seen here: https://github.com/elnicko/maven-gwt-test/compare/master...working_wihtout_gwt-lib_but_with_jar_packaging
You have to use <type>gwt-lib</type> in your dependency.
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mvngwt-client-api</artifactId>
<version>${project.version}</version>
<type>gwt-lib</type>
</dependency>
Actually, if you run Maven with -X you'll see in the logs:
[DEBUG] Adding sources for com.test:mvngwt-client:gwt-app:1.0-SNAPSHOT
[DEBUG] Ignoring com.test:mvngwt-shared:jar:1.0-SNAPSHOT; neither a java-source, gwt-lib or jar:sources.
[DEBUG] Adding sources for com.test:mvngwt-shared:jar:1.0-SNAPSHOT
[DEBUG] Ignoring com.test:mvngwt-client-api:jar:1.0-SNAPSHOT; neither a java-source, gwt-lib or jar:sources.
[DEBUG] Ignoring com.google.gwt:gwt-user:jar:2.7.0; neither a java-source, gwt-lib or jar:sources.
[DEBUG] Ignoring com.google.gwt:gwt-dev:jar:2.7.0; neither a java-source, gwt-lib or jar:sources.
[DEBUG] Ignoring com.google.gwt:gwt-codeserver:jar:2.7.0; neither a java-source, gwt-lib or jar:sources.
Maybe those should be emitted at INFO level rather than DEBUG…
BTW, instead of the build-helper-maven-plugin, you could have just used a <type>java-source</type> or <classifier>sources</classifier> dependency, like it's done for the mvngwt-shared module.

How to use older version of Java (not the default) with Maven project on NetBeans 8

I need to use Java 6 but NetBeans is not using it even after all the configurations I made. It keeps running Maven with the Default that is Java 8 for my IDE. The same configuration runs well on Eclipse so I don't get what I am doing wrong.
I don't want to define the compiler in the nb-configuration.xml since I would have to commit the file. I would expect NetBeans to get it right from the POM.
General information:
I am using NetBeans 8.0.1 (fully updated) running on Java 8
NetBeans 8 needs JDK 7+ in order to work
Using Maven 3.2.3, but the embedded (Maven 3.0.5 didn't work as well)
All Maven plugins are up to date
To reproduce simply create a Maven project of any type in NetBeans. In my case I tried with Java, Web and EJB but none worked.
The following image shows that the JDK is properly added to the IDE.
Tools > Java Platforms
JDK 1.6 is added.
POM configurations that I have tried:
Properties
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
Compiler Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Enforcer Plugin
Enforcing Java 6 gives the following error:
Detected JDK Version: 1.8.0-25 is not in the allowed range [1.6,1.7).
------------------------------------------------------------------------
BUILD FAILURE
Configuration for the enforcer:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.6,1.7)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Test code for Java 6
The following code should not compile:
import java.nio.file.Files;
import java.time.LocalTime;
public class JavaVersionChecker {
public static void main(String[] args) {
System.out.println(Files.class); // Java 7 API
System.out.println("Time: " + LocalTime.now()); // Java 8 API
// Print Java version
System.out.println(System.getProperty("java.version"));
}
}
Test Output
The test code compile with Java 7 and 8 API, but only Java 6 should be accepted.
------------------------------------------------------------------------
Building java6_project 1.0-SNAPSHOT
------------------------------------------------------------------------
--- exec-maven-plugin:1.2.1:exec (default-cli) # java6_project ---
class java.nio.file.Files
Time: 19:24:05.997
1.8.0_25
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 0.610 s
Finished at: 2014-11-21T19:24:06-02:00
Final Memory: 6M/123M
------------------------------------------------------------------------
JDK 6 should be detected by the IDE
As suggested by the comments I made the configuration work using a hint property, which is also very well documented in the generated nb-configuration.xml.
Configuration added to the pom.xml:
<properties>
<netbeans.hint.jdkPlatform>JDK_1.6</netbeans.hint.jdkPlatform>
</properties>
As I understand the compile version for NetBeans needs to be set by a proprietary parameter, which is very simple to set up.
Useful nb-configuration.xml details:
Properties that influence various parts of the IDE, especially code
formatting and the like. You can copy and paste the single
properties, into the pom.xml file and the IDE will pick them up. That
way multiple projects can share the same settings (useful for
formatting rules for example). Any value defined here will override
the pom.xml file value but is only applicable to the current project.
This error was caused by an old entry in nb-configuration.xml
Just adding this property to pom.xml as suggested by #BonanzaOne did not worked for me.
By changing
<netbeans.hint.jdkPlatform>JDK_1.6.0_34</netbeans.hint.jdkPlatform>
to
<netbeans.hint.jdkPlatform>JDK_1.6</netbeans.hint.jdkPlatform>
directly in nb-configuration.xml, Netbeans picks the existing JDK 1.6 automaticaly.

Categories

Resources