JavaRush /Java Blog /Random EN /Connecting libraries using Maven
Дмитрий Яковенко
Level 35
Москва

Connecting libraries using Maven

Published in the Random EN group
Connecting libraries using Maven - 1 Problem JavaRush uses some packages that were in JDK8, and then they were cut. For example, JavaFX for games, or JAXB for XML processing. Therefore, if you are using a fresh version of the JDK, then you have a problem... But even if you are on the old school JDK8, then you also have a problem... In quest 4 Collections you will need to connect Jackson to process JSON. Of course, everything can be connected manually, but it’s pretty dull. You have to download them manually, add them to the project... Solution Actually, Maven is needed to automate this routine. 0) I strongly recommend making a backup copy of the JavaRush project, otherwise you never know... 1) First, let's add Maven to our project. Right-click on the 4.JavaCollections module, select Add Framework Support... Maven, [Ok] 2) Now we find the pom.xml file, add a block in it manually
<dependencies>
</dependencies>
Right after </version> 3) And then we start adding the packages we need. If we need Jeckson, we google “jackson maven dependency” We find an answer like this: http://tutorials.jenkov.com/java-json/jackson-installation.html We see that for Jeckson we need 3 dependencies (artifactId): jackson- core, jackson-annotations, jackson-databind There are other dependencies, but we don’t need them yet; we’ll add them when we need them. Now we look for these 3 dependencies on this site - https://mvnrepository.com/ We enter the names of our artifacts there, find those that are used by the majority, or the latest version. And copy the dependency text to our pom.xml. 4) In total, our pom.xml became like this:
<?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</groupId>
    <artifactId>4.JavaCollections</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
    </dependencies>


</project>
Wonderful, right-click on it and select Maven/Reimport. 5) Now Jeckson is in our programs, the red names of objects have disappeared. But the code stopped compiling, it writes an error:
Error:java: error: release version 5 not supported
For some reason, when introducing Maven into a project, the version of supported Java drops to 5... We need to fix this: File\Settings\Build, Execution, Deployment\Compiler\Java Compiler There in the module it is indicated that for our module 4.JavaCollections - you need to code in Java5... We delete our module in this window. But that’s not all, then open File\Project Structure\Modules There we select our module 4.JavaCollections. Next, select the Language level - Project default tab. 5.1) I found how to solve the problem from point 5. Just add the following to pom.xml:
<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>
Now Maven sets the bytecode version for our module equal to JDK 8 and we are happy with this. I didn’t delete the 5th point, I left it for general information. So that there is an understanding of where which JDK versions can be changed in IDEA 6) Speaking of Project default. JavaRush runs on JDK 8. And if you have version 11 (or newer), then you will regularly swear at the Validator, which does not pass your code. Because you will write the code that you entered in JDK 9+, everything will work for you, and the Validator will give you an error. Therefore, go: File\Project Structure\Project We indicate your current version of the JDK, and under it, indicate the language level - strictly 8 - Lambdas, type annotations etc. What is the benefit - you will see the capabilities of JDK 9+, but if you try to add them, IDEA will curse. For example, "aaa".lines(); - you can write (IDEA will substitute it, although it will highlight it in red), you can fail - study the insides. But it won’t let you compile. 7) The last thing we need to do is correct the paths inside the 4.JavaCollections package. Maven remade them for itself, we return them as they were: a) Go to File\Project Structure\Modules b) Select 4.JavaCollections\Source c) Delete target, rosources, test d) Place Source Folder on src e) Move the com folder to the disk back to src e) The main folder is deleted. In general, we do it as it was before Maven, by analogy with 3.JavaMultithreading 8) Now we add the remaining dependencies ourselves, for example for JAXB. If you need to add logging, my article can help . We add it to the resume - Maven. You are gorgeous! If it helped - like, subscribe, bell!)))
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION