Maven isn't working with specifying Java JDK? - java

I am trying to specify which Java JDK version to use in Maven/IntelliJ IDEA. I want to use JDK 7, but the source always seems to compile in JDK 8. I have done quite a few things, this is the screenshot of my Project Structure settings of IntelliJ IDEA:
http://i.stack.imgur.com/wabk2.png
This is my pom.xml. As you can see, I am using properties, and the maven compiler plugin.
<?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>me.staticjava</groupId>
<artifactId>VillagerMerchants</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>http://repo.md-5.net/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.7.9-R0.3-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.7.9-R0.3-SNAPSHOT</version>
</dependency>
</dependencies>
I am not sure why, but the code ALWAYS compiles in 1.8...
Any help?
Thanks,
~StaticJava

You need to go into Settings -> Project Settings -> Maven -> Runner and check the JRE setting.
Mine is set to Use Project JDK (1.7) but it sounds like yours is set to something different.
By the way, I guess you are aware that the "language level" in your pom (1.7) is a different setting to the actual JDK you compile with. For example you can well use JDK 1.8 but compile to language level java 7... :)
Good luck.

Related

import own .jar file as dependency via Maven and have install discover and retrieve it's dependencies

So this is probably a stupid question, but I have created an own library "testlib" which I want to include in other of my own Maven projects.
This is my own librarys .pom
<?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.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>testlibrary</description>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
In order to include my library in another new project, I have created a local repository in my new projects .pom
...
<repositories>
<repository>
<id>data-local</id>
<name>data</name>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
...
Installing my own library "testlib" via mvn install is working fine for testlib itself. The problem is, that Maven will not detect that "testlib" requires org.slf4j, hence won't get it when I run install.
I have checked other dependencies in my .m2 folder and saw they have a .pom file with the same name as the .jar (for log4j that'd be log4j-1.2.17.pom). I tried copying testlibs .pom next to its .jar and changed the name accordinly, but that doesn't do it.
What do I have to do in order to get the same functionality as any other library from maven central? In other words, I don't want a fat .jar that has all dependencies included. I want a Maven project that adds my library as a dependency to discover that it needs sl4j and include it when mvn install is run.

How to build a project and load the SDK with maven

I have to create a pom.xml, which builds the entire project. But it won't load the SDK.
I tried to include dependencies, but it didn't work.
<modelVersion>4.0.0</modelVersion>
<groupId>de.swp18gi.zugumzugeuropa</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ZugUmZug</name>
<description>Zug um Zug Europa</description>
</dependencies>
<properties>
<java.version>10</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<debug>false</debug>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>client</module>
<module>common</module>
<module>server</module>
</modules>
It is given like the below :
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Please reformat your pom.xml . Reference URL : https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Forking out the java compiler does not mean that Maven does any work in locating an appropriate JVM for you.
My guess is that you want Maven to use a JDK including the compiler instead of the standard JRE the java command installed by default under Windows belong to.
The correct way to do this is to tell Maven which JDK-JVM to use (or - for advanced users - to tell it to use the Eclipse compiler)
You can either do it by setting the JAVA_HOME environment variable to point to an appropriate JDK you have downloaded and installed (these days Oracle only wants paying users so the Zulu version of OpenJDK is a suitable alternative - https://www.azul.com/downloads/zulu/) or by providing the full path to the javac you want to use in the <executable> tag. See https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#executable for full details.

Lambda error during maven build

I am using Maven build to build my project(using Eclipse IDE).
I have put jdk1.8 as default jdk, but still, I am getting an error as the source is 1.6.
Is there a configuration problem I am missing out? Please let me know?
(use -source 8 or higher to enable lambda expressions)
1. "clean install" as goals in "Run Configurations" in Eclipse.
2.pom.xml file
<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.mastercard.ayman</groupId>
<artifactId>Spring_MVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-libs-release</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Compiler is JavaSE-1.8
Either set the maven-compiler-plugin as #Horst says or set the maven.compiler.source and maven.compiler.target properties like this.
<project ...>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
...
</dependencies>
<build>
...
</build>
</project>
This is something I have dealt with too many times.
Could you post the following things as an answer?
Your current build command/query.
Your pom.xml code.
Your compiler version.
With these file(s) I could help you further to resolve your problem!
Setting the JDK in eclipse is only half the story, as maven manages the compiler itself.
Try adding the maven compiler plugin to you build configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

Eclipse Maven Build Error invalid target release

I am using Maven for the first time (in Eclipse). I would like to include
HakariCP to my project. Every time I try to build my project I get following error message:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project WindowPanel: Fatal error compiling: invalid target release: 1.8 -> [Help 1]
Here is my 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>me.gustavwww</groupId>
<artifactId>WindowPanel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WindowPanel</name>
<description>A Windows Server Panel</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>LATEST</version>
<scope>compile</scope>
</dependency>
</dependencies>
Thanks in advance!
EDIT: All I did was update my JDK to the latest version.
If you have JRE set to 1.7 by default in Eclipse you need to specify 1.8 in the Run Configurations. Go to "Run > Run Configurations..." and select the Maven Build you have problem with. Then click the JRE tab and choose Execution Environment Java 1.8.
If you really need to compile it against Java 8, read this question and answer.
They provide more than one to way to force Maven to use a specific version of Java.

maven build dependencies in Eclipse, multiple projects

I have two projects in eclipse, both are maven controlled. A references B and they are on a flat system. What we currently have setup is that we have to a build on B - generate a jar on the system, and A references that jar.
I'd like to change this to be we just have to build A and it will go automatically build B? Can I do this in Maven/eclipse such that I don't have to create some higher project?
I have looked into some of the maven docs - but they just really confuse me :). Thanks for your help.
The pom's look like this
(B)
<project xmlns="..." xmlns:xsi="..."
xsi:schemaLocation="...">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thetus</groupId>
<artifactId>irisMDK</artifactId>
<packaging>jar</packaging>
<name>irisMDK</name>
<version>0.1</version>
<url>...maven.apache...</url>
<build>
<sourceDirectory>java/src/main</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<testExcludes>
<exclude>**/*.java</exclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>java/src/main</directory>
</resource>
</resources>
</build>
<dependencies>
...
</dependencies>
</project>
------ And (A):
<project xmlns="..." xmlns:xsi="..."
xsi:schemaLocation="...">
<modelVersion>4.0.0</modelVersion>
<groupId>local.b</groupId>
<artifactId>projectB</artifactId>
<packaging>jar</packaging>
<name>B</name>
<version>RELEASE</version>
<url>...</url>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>...download.java.net/maven...</url>
</repository>
</repositories>
<build>
<plugins>
...
</plugins>
<sourceDirectory>java/src/main</sourceDirectory>
<testSourceDirectory>java/src/test</testSourceDirectory>
<resources>
</resources>
</build>
<dependencies>
<dependency>
<groupId>local.b</groupId>
<artifactId>projectB</artifactId>
<scope>system</scope>
<version>RELEASE</version>
<systemPath>${basedir}/../B/target/B-0.1.jar</systemPath>
</dependency>
...
</dependencies>
<reporting>
...
</reporting>
</project>
If you could show me an example of how to change ^^^ I would be most grateful!!
Original Answer:
The M2Eclipse plugin, if you are not using it already, allows you to do this. Once installed, remove
<systemPath>${basedir}/../B/target/B-0.1.jar</systemPath>
from A's pom.xml and make sure the groupid, artifactid, version match up with what is defined in B's pom.xml
Then right-click on the project, Maven-> Enable Dependency Resolution. The build should now look at B's local project
Edit:
If B's pom.xml looks like this (from your example):
<project xmlns="..." xmlns:xsi="..."
xsi:schemaLocation="...">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thetus</groupId>
<artifactId>irisMDK</artifactId>
<packaging>jar</packaging>
<name>irisMDK</name>
<version>0.1</version>
...
</project>
In A's pom (which depends on Project B) your dependency should look like:
<project>
...
<dependencies>
<!--Attributes of project that this project is dependent upon, as defined in that projects POM -->
<dependency>
<groupId>com.thetus</groupId>
<artifactId>irisMDK</artifactId>
<version>0.1</version>
</dependency>
...
</dependencies>
....
</project>
This will tell maven and eclipse that project A explicitly depends on that version of Project B. With M2Eclipse, if you have a matching groupId, artifactId, and version in your workspace and you have "Dependency Resolution" enabled, Project B's contents will automatically be built and included into Project A.
Also, opening the Maven console in eclipse (console view->new console dropdown->new maven console) could help in debugging why project B isn't be picked up by Project A.
Sounds like you want to use a multi-module project, here is a simple tutorial. You would create a parent POM, and have both A and B as children, with A keeping its dependency on B.

Categories

Resources