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

Connecting Libraries with Maven

Published in the Random EN group
Linking Libraries with Maven - 1 Problem CodeGym uses some packages that were in JDK8 and then cut them out. 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 using the old school JDK8, then you also have a problem ... In the 4th Collections quest, you will need to connect Jackson to process JSON. Of course, everything can be connected by hand, but it's pretty dull. It is necessary to pump them out by hand, add them to the project ... Solution Actually, to automate this routine, Maven is needed. 0) I strongly recommend making a backup copy of the CodeGym project, otherwise you never know ... 1) First, add to our Maven project. Right-click on the 4.JavaCollections module, select Add Framework Support... Maven, [Ok] 2) Now we find the pom.xml file, add the block in it with our hands
<dependencies>
</dependencies>
Right after </version> 3) And then we start adding the packages we need. If we need Jackson, google "jackson maven dependency" We find an answer like this: http://tutorials.jenkov.com/java-json/jackson-installation.html We see that we need 3 dependencies for Jeckson (artifactId): jackson- core, jackson-annotations, jackson-databind There are other dependencies, but we do not need them yet, we will need them - we will add them. Now we are looking for these 3 dependencies on this site - https://mvnrepository.com/ We drive in the names of our artifacts there, we find those that are used by the majority, or the latest version. And we copy the dependency text to us in pom.xml. 4) In total, our pom.xml has become 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>
Awesome, right click on it and choose Maven/Reimport. 5) Now in our programs Jackson began to be, the red names of objects disappeared. But the code stopped compiling, writes an error:
Error:java: error: release version 5 not supported
For some reason, when introducing Maven into the project, the supported Java version 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 - need to code in Java5... We delete our module in this window. But that's not all, then we 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 to be JDK 8, and that suits us. 5th paragraph did not delete, for general information left. So that there is an understanding of where which versions of the JDK can be changed in IDEA 6) Speaking of Project default. CodeGym 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 count the error for you. So let's go: File\Project Structure\Project Specify your current version of the JDK, and under it, specify the language level - strictly 8 - Lambdas, type annotations etc. What is the benefit - you will see the features of JDK 9+, but if you try to add them - IDEA will swear. For example, "aaa".lines(); - you can write (IDEA will substitute, although it will highlight in red), you can fail - study the insides. But to compile - will not. 7) The last thing we need to do is fix the paths inside the 4.JavaCollections package. Maven remade them for itself, return as it was: a) Go to File\Project Structure\Modules b) Select 4.JavaCollections\Source c) Delete target, rosources, test d) Put Source Folder on src e) Move folder com on disk back to src e) Main folder - delete. In general, we do 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, it can help Maven remade them for itself, return as it was: a) Go to File\Project Structure\Modules b) Select 4.JavaCollections\Source c) Delete target, rosources, test d) Put Source Folder on src e) Move folder com on disk back to src e) Main folder - delete. In general, we do 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, it can help Maven remade them for itself, return as it was: a) Go to File\Project Structure\Modules b) Select 4.JavaCollections\Source c) Delete target, rosources, test d) Put Source Folder on src e) Move folder com on disk back to src e) Main folder - delete. In general, we do 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, it can help for example for JAXB. If you need to add logging, it can help for example for JAXB. If you need to add logging, it can helpmy article . We add to the summary - Maven. You are gorgeous! If it helped - like, subscribe, bell!)))
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION