All about Maven: the beginning I love to practice. You can’t even imagine how tedious it can sometimes be to write about theory. However, you can’t go anywhere without her. Practice is another matter, of course. Now let’s quickly go through the commands that I previously described, and we’ll finish)
- But in order to have something to compile, it needs to be added there! Logical? - Yes captain. bgg Therefore, let's add a class that will have a method and two tests for it. Let's create a regular folder scheme for Maven:
Setting up pom.xml
While we were theorizing, we got a pretty good pom.xml:View code
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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.github.javarushcommunity</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Demo Project</name>
<url>https://github.com/javarushcommunity/maven-demo/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>romankh3</id>
<name>Roman Beskrovnyi</name>
<email>roman.beskrovnyy@gmail.com</email>
</developer>
</developers>
<scm>
<connection>git@github.com:javarushcommunity/maven-demo.git</connection>
<developerConnection>git@github.com:javarushcommunity/maven-demo.git</developerConnection>
<url>https://github.com/javarushcommunity/maven-demo</url>
</scm>
<properties>
<mockito.version>2.26.0</mockito.version>
<junit.version>5.5.2</junit.version>
<image.comparison.version>4.3.0</image.comparison.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadoc.plugin.version>3.1.1</javadoc.plugin.version>
<source.plugin.version>3.2.0</source.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.romankh3</groupId>
<artifactId>image-comparison</artifactId>
<version>${image.comparison.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${source.plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${javadoc.plugin.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src main java resources test java resources
In src/main/java we will create a package in which we will work. To compose it correctly, it would be good to put the name of the organization ( groupId ) at the beginning, and then the name of the project. In our case it will be like this: com.github.javarushcommunity.mavendemo As for me, this is a very good package. In it we will create the ImageComparator class , for which we will write two tests.
Connecting Maven and the project as a whole to IDEA
In order for the idea to be able to recognize our dependencies and offer to use them in the project, we need to do several things:- Tell the idea where the code sources will be and where the tests will be.
- Add a maven project to the idea.
- • src/main/java - Sources
- • src/main/resources - Resources
- • src/test/java - Tests
- • src/test/resources - Test Resources
- • target - Excluded
We write sources and tests for our “project”
So, we added Maven to the idea, now let’s describe the class that will act as sources for us:ImageComparator:
package com.github.javarushcommunity.mavendemo;
import com.github.romankh3.image.comparison.ImageComparison;
import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import com.github.romankh3.image.comparison.model.ImageComparisonState;
import java.awt.image.BufferedImage;
/**
* Class created for answering, does two images the same or not.
*/
public class ImageComparator {
/**
* Method, which says the same images or not.
*
* @param image1 image1 for comparison
* @param image2 image2 for comparison
* @return return true, if images are the same, false - otherwise.
*/
public boolean isTheSameImages(BufferedImage image1, BufferedImage image2) {
//Create ImageComparison object
ImageComparison imageComparison = new ImageComparison(image1, image2);
//Compare images
ImageComparisonResult imageComparisonResult = imageComparison.compareImages();
//Check, that ImageComparisonState is MATCH:
return ImageComparisonState.MATCH == imageComparisonResult.getImageComparisonState();
}
}
Let's create tests for this purpose. The class that tests must be in the same package, only in src/test/java. The idea knows this, and you can take advantage of its capabilities. To do this, click on the class name in the file itself and select Create test on the light bulb that appears: We will be asked to select settings for the future test. We don’t install anything, just click OK: For our test, we’ll take image-comparison from the test images and put them in test resources (src/test/resource). After this we will write two tests. One will check that these two pictures are different. And in the other we will transmit the same picture and expect an answer that they are the same. Then we get the following test:
package com.github.javarushcommunity.mavendemo;
import com.github.romankh3.image.comparison.ImageComparisonUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.awt.image.BufferedImage;
@DisplayName("Unit-level testing for ImageComparator")
class ImageComparatorTest {
private final ImageComparator comparator = new ImageComparator();
@Test
public void shouldConfirmImagesNotTheSame() {
//given
BufferedImage image1 = ImageComparisonUtil.readImageFromResources("image1.png");
BufferedImage image2 = ImageComparisonUtil.readImageFromResources("image2.png");
//when
boolean theSameImages = comparator.isTheSameImages(image1, image2);
//then
Assertions.assertFalse(theSameImages);
}
@Test
public void shouldConfirmImagesTheSame() {
//given
BufferedImage image1 = ImageComparisonUtil.readImageFromResources("image1.png");
//when
boolean theSameImages = comparator.isTheSameImages(image1, image1);
//then
Assertions.assertTrue(theSameImages);
}
}
The tests are very simple, I won’t dwell on them. If you are interested, I will write an article about testing someday (write about it in the comments).
Execute Maven commands
Now, when everything is ready to see the work of the teams, select our project in the Maven plugin and open Lifecycle in it : Now click compile and see what happened: From this we can draw two conclusions:- Our sources were compiled and placed in the specified path.
- The command was successful - BUILD SUCCESS.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
Let's add it to the project, put the version in <properties/> as best we can and restart test : Now it’s a different matter! Two tests ran and were successful. Fire! Now we run our package and expect that we will find the jarnik in the target folder: That is, we see again that the tests have passed, and after this (guarantees that everything in the project is good) we can collect the project into an archive. The figure shows that there is an archive in the maven launch logs and the project structure. Next we have install . Great team. Oh, before I forget. It is considered good practice to clean the project before executing a new command by running clean . This will save you from nonsense in the future - I guarantee it from my own experience))) Therefore, first we run clean , and then install : Where the arrow is, you can clearly see where the local repository is located. Based on what they write, this is on my way: /Users/roman_beskrovnyi/.m2 And the archive itself will be located here: /Users/roman_beskrovnyi/.m2/repository/com/github/javarushcommunity/maven-demo/1.0-SNAPSHOT And if we go to the terminal and try to get into the folder where the archive is located, then it will be there: deploy won’t show us, and you don’t need the others yet...
GO TO FULL VERSION