IntelliJ target bytecode reverting - java

For a project that I'm currently working on, IntelliJ gave me the compile error Error:java: javacTask: source release 8 requires target release 1.8. I went into Settings>Build, Execution, Deployment>Compiler>Java and saw that the target bytecode version for one of my modules was set to 1.5, so I changed it to 1.8, compiled, and it worked. But then the next day, I got the same error. I went into settings and the target bytecode for that one module was back at 1.5. I changed it to 1.8 and it compiled/ran just fine. This has now happened multiple times and I am frustrated by the number of times I have to go into settings to manually change the target bytecode version.
Why does the target bytecode version keep reverting? I don't have 1.5 specified in the pom or anywhere else, so I am baffled as to why the bytecode version keeps getting set to 1.5.

You need to specify the source and target versions in your pom.xml file, but since the maven-compiler-plugin is added by default, an easier way to do that would be to set the following properties:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

You need to dd this to your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

FYI.
I still had this problem despite setting the pom.xml with both the settings above from the answers as mentioned by #isapir and #carlos-a-ibarra respectively.
I discovered this was due to an Intellij setting:
This config can be found in Build,Execution,Deployment > Build Tools > Maven > Importing
By default, it is set to the 'Internal JRE' setting, which in the latest version of Idea is 11. I had to select it to use the 1.8 JDK the project was configured to use.
This is annoying, because if you have a mix of 11 and 8 projects, you'll have to manually toggle this setting back and forth.
It caused the module output to always fall back to 11 every time the pom.xml was reimported.
It's a really annoying bug in IntelliJ.

Please try below option, it worked for me.
step 01: Change the version under Module and project.
step 02: build the module.
Hope it should resolve the issue.

I have the same issue with "IntelliJ IDEA 2022.2.3 (Community Edition)".
This is my "pom.xml"
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
</project>
I can manually change the "Target bytecode version" to 17. (Preference => Build, Execution, Deployment) => Compiler => Java Compiler).
However, if I reload maven project (Right click "pom.xml" => Maven => Reload project), the "Target bytecode version" will be changed back to 1.5.
To prevent that, I added "maven-compiler-plugin" to pom.xml. Then I reload maven project (Right click "pom.xml" => Maven => Reload project). Now, the "Target bytecode version" is 17.
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
</project>

Related

Maven project compile error: cannot find symbol (which is java.lang.String)

It's a very simple web scraping program that I built in intellij idea with the maven build system. It worked fine before today but when I tried to build it today it gave me object not found errors for base java objects like String and List. When I tried to reload the maven project I got another error below. Idea also highlights the basic java object in red and gives no advice. The only thing I did was slightly modify the java source code. So, I tested with the stock hello world code. It did compile and run, but Intellij still highlights String and System in red and the maven reload produces the same error. Help is greatly appreciated as I need to get some data for a research project.
Error with web scraping source code compilation error
Error:(24, 13) java: cannot find symbol
symbol: class List
location: class Main
Maven reload error
Cannot resolve plugin org.apache.maven.plugins:maven-compiler-plugin:${maven.compiler.version}
This is the pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>htmlunittest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Update:
After resolving the maven errors, since you are facing IDE related problems, I would suggest you to do this:
Go to File > Project Structure
Add the JDK in Project SDK (If it is not there, under Platform Settings > SDKs, you need to add the JDK)
Also ensure that maven home is set correctly in Preferences > build tools > maven:
Then do a maven rebuild from the side panel:
You have forgot to define the property ${maven.compiler.version} in your POM.
Define a properties section in your POM like this:
<properties>
<java.version>1.11</java.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
</properties>

How to add a maven project as a dependency to another one

I've just started a java project, in which I'd like to use the classes of another project.
My pom.xml looks like this so far:
<?xml version="1.0" encoding="UTF-8"?>
<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>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
What would be a good way to add that other project as a dependency in mine? Should I download all of its compiled jar files and add them one by one in my pom.xml as dependencies? Or is there a better option?
I was thinking of downloading all the jars, putting them into a directory (e.g. lib) and somehow referencing that entire directory in the pom.xml, so if there's a new version of the project mine depends on, I only have to change the contents of that lib folder for the new jars, and don't have to edit the pom.xml. Is it an option? If so, how to do that?
Or most importantly, what is the proper way you suggest doing it?
If you want to use the latest version of this project, I suggest you build it yourself. Because it seems they are releasing to Sourceforge and maintaining the code actively.
Each time you want to upgrade the version, you have to get the latest source code (via git) and use mvn install command on this projects root pom.xml to install it to your local maven repo. This project is configured as multi module maven project, using install on the root pom.xml will install all the sub modules.
On your projects pom.xml you can use mvn versions:use-latest-releases to update all your dependencies to the newest version. This command will automatically upgrade dependency versions for you.
To add a project as dependency follow Marvins link.

How to configure Eclipse to automatically resolve and build multiple maven modules?

Here is my situation:
I'm trying to migrate from Ant to Maven
My project has 3 artifacts: shared api (jar), web app (war), desktop swing app (jar). Latter 2 depends on shared api.
At this moment I'm trying to make web app part work. So I've created 4 poms: eftracker (root pom), eftracker-parent, eftracker-shared, eftracker-web.
If I run mvn package on eftracker all works just perfect -- I have eftracker-shared.jar and eftracker-web.war created as expected
I added tomcat7-maven-pluginto run web app with maven goal tomcat7:run to test changes made during development
I also added eftracker-shared as a project to eftracker-web build path.
My goal:
Now I want to work comfortably in Eclipse, meaning I want to change files, hit Run and in couple seconds be able to test my changes.
During development I will change both: shared and web projects.
My problem:
If I never run mvn install than an attempt to invoke tomcat7:run will lead to error: Failed to execute goal on project eftracker-web: Could not resolve dependencies for project com.skarpushin:eftracker-web:war:1.503.0: Could not find artifact com.skarpushin:eftracker-shared:jar:1.503.0 in central (https://repo.maven.apache.org/maven2)
It appears I have to mvn clean install shared project (or even on root module) each time I change it before I can execute tomcat7:run on web app and see recent changes.
Question is:
Is it possible to make this process automatic?
...OR maybe there is other way how to minimize "maven overhead" during development?
eftracker.pom
<?xml version="1.0"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker</artifactId>
<version>1.503.0</version>
<packaging>pom</packaging>
<name>eftracker</name>
<modules>
<module>eftracker-parent</module>
<module>eftracker-shared</module>
<module>eftracker-web</module>
</modules>
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
</project>
eftracker-parent/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.skarpushin</groupId>
<version>1.503.0</version>
<artifactId>eftracker-parent</artifactId>
<packaging>pom</packaging>
<name>eftracker-parent</name>
<!-- ...some common properties, dependencies, build plugins... -->
</project>
eftracker-web/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker-parent</artifactId>
<version>1.503.0</version>
<relativePath>../eftracker-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eftracker-web</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>ROOT##${project.version}</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.skarpushin</groupId>
<artifactId>eftracker-shared</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ...other deps -->
</dependencies>
</project>
Try to use M2Eclipse
https://www.eclipse.org/m2e/
M2Eclipse provides tight integration for Apache Maven into the IDE
with the following features:
Launching Maven builds from within Eclipse
Dependency management for Eclipse build path based on Maven's pom.xml
Resolving Maven dependencies from the Eclipse workspace without installing to local Maven repository
Automatic downloading of the required dependencies from the remote Maven repositories
Wizards for creating new Maven projects, pom.xml and to enable Maven support on plain Java project
Quick search for dependencies in Maven remote repositories
So it appears there are 2 things needs to be done:
run mvn compile on parent project in that way all classes will appear in ../parent/target/classes folder. Note that they'll be automatically updated by Eclipse if you change source code
edit Eclipse run configuration and put this checkbox "Resolve Workspace artifacts"
Now I was able to run project as Maven build... with goal tomcat7:run and it worked without the need of parent project to be installed

Setting JAVA_HOME for Maven on Windows 10

I recently upgraded to Windows 10. I'm trying to create a new maven project in eclipse, but it doesn't seem to be picking up my JAVA_HOME setting.
Here is the JDK installation directory:
And here is my JAVA_HOME environment variable setting:
If I run mvn -version in a command prompt, maven does seem to get the correct value for JAVA_HOME:
I then try to create a new maven project in eclipse, using a barebones 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>StaticVoidGames</groupId>
<artifactId>DatabaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DatabaseTest</name>
</project>
But the project's JRE is 1.5 for some reason:
For what it's worth, my eclipse workspace default JRE is the same as my JAVA_HOME setting.
I've tried manually changing the build path to use the correct jdk, but it simply reverts. I've tried cleaning the project, doing a maven update, restarting eclipse, restarting my computer, and creating a new project. Maven still chooses Java 1.5, which isn't even installed on my machine.
All of my googling says that maven should pick up the JAVA_HOME setting to figure out which Java to use, but that doesn't seem to be the case here. Is there another setting that I'm missing somewhere?
As per screenshoot, Maven is correctly picking up your JAVA_HOME, however what you see in Eclipse is not the version of Java installed (1.5) but the version Maven will use by default to compile.
In your case, Maven will use JDK 1.8 and compile settings source and target to 1.5 (version by default).
To set Maven to the same level as the Java version installed, you need to configure the Maven Compiler Plugin. Once done that and after an Eclipse update (right click on the project, Maven, Update Project), Eclipse will update its view accordingly.
You can add to your POM the following Maven Compiler Plugin configuration:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Alternatively, you can use well-known Maven properties to achieve the same by only settings:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)

Using IntelliJ 12, I have a java project and I use maven with a pom.xml.
My project is using java8, but it seems the default project language level has been set to 6 while importing the project.
I can change the language level to 8.0 (F4 -> Modules -> Language level) however every time I edit my pom.xml the project level is switched back to "use project language level", and I have to edit this settings again and again.
Is there something I need to add to the pom.xml to set the default language level to 8.0?
As per Mark's comment, here is how to do it:
<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>
A shorter version of vikingsteve's answer is:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I'm upgrading a project from JDK 8 to JDK 10+. I had the compiler properties specified correctly as follows:
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
However the Idea project would keep resetting the language level to 8.
Eventually I figured out that Idea's Maven import process was using JDK 8 to import the project which limited the language level to <= 8.
To fix I updated the 'JDK for importer' property under Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing to use JDK 11.
I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea. Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/). So if the compiler plugin is used at all in a project, and the compiler level is not explicitly set in the pom.xml, whenever the POM is re-processed the level will revert to the default.
So there is a conceptual conflict which is ignored by Intellij IDEA. The IDE still allows one to set the project and module settings, but provides no warning or feedback that this setting is controlled by pom.xml. Solutions would either be to explicitly allow overriding the POM compiler plugin setting (perhaps not wise because what then happens when you use maven on the command line), or to deactivate the controls in the IDE when this setting from the POM is in effect.
The solution at the present time is to set the desired compiler level in the compiler plugin in the pom, the re-import, rather than trying to set it in module settings.
There are two ways of doing this, add either one of them in your pom.xml file:
First- Add Properties
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
second- Add Plugin
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Let me know if it helps.
None of the solutions helped in my case. I didn’t need to specify any Java version in my pom.xml.
I needed to open the <project-name>.iml file and change the JDK version there.
Original:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<!-- ... ^ -->
<!-- ... | -->
Updated:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<!-- ... ^ -->
<!-- ... | -->
This makes no sense at all. At no point have I specified a JDK version for Java 1.5.
What solved the issue for me was a list of updates:
In Project Structure > modules: changes the language level Java version to 8
In Project Structure > Project: Java version should be 1.8
In pom file, same changes specified in the responses above
In Settings > Java compiler > changed the bytecode versions to 8
In Settings > maven > importing > JDK for importer should be 1.8
I struggled a lot with this problem, due to building microservices with Dropwizard. Eventually I found out that I had my build properties in the wrong pom file (The main service's pom.xml).
So, even though the other packages are more like libraries, I were not able to use the Java 8 syntax.
When I refactored the build plugin into the "global" .pom.xml" file, all child containers were able to use the new syntax.
May help someone having issues with multi-container projects
thank you it works.
be careful not to make the same mistake I did.
if you have profiles, add the plugin in the right profile.
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
.......
</profile>
<profiles>
For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked.
However as the .iml file for each project was generated with original pom (with default compiler version of 1_5 as explained by someone above) has a JDK version of 1_5, this still overrides the pom version.
I deleted the .idea folder manually and imported the module into IntelliJ with a reimport from the updated pom. When I reimported the Module from updated POM,I could see that the iml files had the updated JDK version (1_7 in my case) .
There was one additional step I had to follow, in addition to setting the maven build properties, adding the maven-compiler-plugin, and modifying the Java version in the .iml file. (each documented already in the other answers). You also have to set the compiler version in the settings.
Problem: Expected Java version: 11 but Stuck at version: 8
I tried almost all the answers, still I was stuck with language_level 8 and nowhere in my project or in the Intellij IDE I found any trace that could relate to Java version 8.
Then I explored the pom.xml of the parent with no success but in the pom.xml of the grand-parent, I found that the release version is 8.
Solution:
Adding the following 3 lines in the properties made the difference.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>

Categories

Resources