1

The Windows 10 system environment variables JAVA_HOME and PATH are pointing to JDK 18 which is needed to launch VS Code and needed by other applications on the laptop.

I configured Maven Project in VS Code with JRE/Java Runtime pointing to JDK 1.8 which is required for the project to compile and build correctly. All is working fine.

When I run Maven Goals such as mvn clean install either using ctrl-shift-p and execute maven command, or by running a build from Maven View/Lifecycle entry. A terminal is launched and it will inherit the system environment variables. In order to avoid this problem, I always cancel the launched maven command in the PowerShell terminal by pressing ctrl-c and change the environment variables using:

$ENV:Path="C:\apps\jdk1.8.0_351\bin;"+$ENV:Path
$env:JAVA_HOME="C:\apps\jdk1.8.0_351"

... then run the maven goal again.

I also added a favorite in Maven View/Favorites to launch the maven goal as follows:

    "maven.terminal.favorites": [
        {
            "command": "clean install \"-Dmaven.test.skip=true\"",
            "debug": false
        }
    ]

When I run the above goal, it will always inherit the system values, and I have to change them again as mentioned earlier.

In addition, if I try to run a goal from Mave View/Plugins/Install by right-clicking and clicking debug, it will always launch a new PowerShell terminal which will inherit the system environment variables, and I didn't find a way to change this.

I am thinking that VS Code should set the environment correctly when running Maven Goals based on the JRE configured in VS Code Workspace setting.json file, but this is not happening.

When I configured Debug Request in launch.json, it is respecting the JDK defined for the project, so why it is not respecting this setting when running maven goals?

I think I can solve this problem by configuring a task that will run the maven goal in PowerShell script by I think this is a bit complex for such a simple requirement.

How I can solve this problem without configuring a new task? I am thinking there is a config somewhere to set JRE for Maven Goals for a given project.

tarekahf
  • 738
  • 1
  • 16
  • 42
  • I suggest using a project-specific `settings.xml` (see [this question](https://stackoverflow.com/questions/43156870/create-project-specific-maven-settings)), and in this `settings.xml` define a property with name `maven.compiler.executable`, pointing to the home of the JDK needed (see [this question](https://stackoverflow.com/questions/2503658/specify-jdk-for-maven-to-use)). – Turing85 Dec 06 '22 at 19:35
  • Thanks... I will check it out. Is this file `settings.xml` respected by Eclipse also? Will this file be generated if I run `mvn eclipse:eclipse`? If I set JRE in Eclipse, will it be effective in VS Code? I notice if I configure the build path and add the correct JRE version, then maven goals will run properly in Eclipse. – tarekahf Dec 06 '22 at 19:50
  • It should be respect by everything that adheres to how maven works (unless - of course - the settings are explicitly overriden). For the rest: just try it out. I myself are not an eclipse user. – Turing85 Dec 06 '22 at 19:53
  • There was a solution but now I don't see it. I think that answer was removed because it was not from a trusted source or something like that. – tarekahf Dec 08 '22 at 00:09
  • https://stackoverflow.com/a/35463606 use toolchain settings – life888888 Dec 08 '22 at 06:45
  • Thanks, it looks this is what I am looking for. I followed the steps to activate the toolchain and I can see the effect showing in the output of `mvn clean install`, but when I unzip the resulting JAR and check the file `MANIFEST.MF` I always see the JDK defined by the system `PATH` and `JAVA_HOME`. – tarekahf Dec 08 '22 at 19:25
  • I think it won't work for generating a JAR file and must set `PATH` and `JAVA_HOME` correctly before running the maven goal to generate the JAR file using the intended JDK version. According to the documentation here, https://maven.apache.org/guides/mini/guide-using-toolchains.html `maven-jar-plugin` is not included. I checked the JDK version in the generated `.class` files using the `javap` command and found out that the java version is affected by setting `maven-compiler-plugin` accordingly. – tarekahf Dec 09 '22 at 00:17
  • 1
    Sure will do... In the meantime, I wanted to know if generating the JAR file with a newer JDK version will work fine if deployed in a JRE with an older version, keeping in mind that the classes are compiled using the correct JDK. Is this OK? – tarekahf Dec 14 '22 at 15:38

1 Answers1

0

There are various ways to set the target jdk version required to generate the jar file but all such methods will affect the class files only. You can set the target jdk using toolchain, compiler plugin, and a couple of other methods. But to generate the jar using a certain jdk you must set the PATH environment variable before running the maven goal to generate the JAR file using the intended JDK version. For example, ccording to the documentation here, maven.apache.org/guides/mini/guide-using-toolchains.html, the maven-jar-plugin is not included. I checked the JDK version in the generated .class files using the javap command and found out that the java version is affected by setting maven-compiler-plugin accordingly but the jar was generated using the javac.exe that was visible as per the PATH environment variable.

tarekahf
  • 738
  • 1
  • 16
  • 42