<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andres Sacco</title>
    <description>The latest articles on DEV Community by Andres Sacco (@andressacco).</description>
    <link>https://dev.to/andressacco</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F546514%2Ff6ed8def-ce1b-461f-97b6-92a5c6bf8132.png</url>
      <title>DEV Community: Andres Sacco</title>
      <link>https://dev.to/andressacco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andressacco"/>
    <language>en</language>
    <item>
      <title>JXLS: How to generate Excel documents using templates?</title>
      <dc:creator>Andres Sacco</dc:creator>
      <pubDate>Wed, 03 Feb 2021 12:32:48 +0000</pubDate>
      <link>https://dev.to/andressacco/jxls-how-to-generate-excel-documents-using-templates-1ind</link>
      <guid>https://dev.to/andressacco/jxls-how-to-generate-excel-documents-using-templates-1ind</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Sometimes you need to export some information into an Excel document with a particular format (some specific fonts for the content or some colors for column headers). One option to solve this problem is to use &lt;a href="https://community.jaspersoft.com/project/jasperreports-library" rel="noopener noreferrer"&gt;JasperReport&lt;/a&gt; which provides a good interface and a lot of features that will allow you to design a report. A static report doesn't change a lot you don't have any problem but imagine that the users want some changes once a week because they don't like the format, structure nor content.&lt;/p&gt;

&lt;p&gt;To solve this situation you have 2 alternatives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spend a lot of time each week to make the changes.&lt;/li&gt;
&lt;li&gt;Use a library that allows you to modify the format of the report just calling it from your project.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How can you solve this problem?
&lt;/h1&gt;

&lt;p&gt;In order to solve the problem that anyone can modify it without knowing a lot of some specific technology appears JXLS. &lt;a href="http://jxls.sourceforge.net/" rel="noopener noreferrer"&gt;JXLS&lt;/a&gt; is a library that provides you the possibility of creating an Excel template with some specific logic inside i.e. you can define the color of the cells depending on the content of one attribute or hide one row if one attribute is null or empty.&lt;/p&gt;

&lt;p&gt;Behind the scenes, JXLS uses Apache POI or JExcel as core and other libraries to translate some tags in the template into a new document with all the information.&lt;/p&gt;

&lt;p&gt;To start using JXLS you will need to add the dependency in your pom file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.jxls&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;jxls&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;2.9.0&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After that, you need to choose which implementation of JXLS you will use to generate the Excel document. You have two possible options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apache POI is the most common library which most developers use to generate Excel documents in an artisanal way.&lt;/li&gt;
&lt;li&gt;JExcel is an old library that provides common features. The main problem with this library is the last version was launched in 2019.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!-- Apache POI --&amp;gt;
&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.jxls&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;jxls-poi&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;2.9.0&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;

&amp;lt;!-- JExcel --&amp;gt;
&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.jxls&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;jxls-jexcel&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.0.9&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Above you will see both dependencies just to show you the name of them, but you need to add only one of them.&lt;/p&gt;
&lt;h3&gt;
  
  
  Populating the template
&lt;/h3&gt;

&lt;p&gt;Now you have dependencies in your project, the next thing to do is to send the information to JXLS. You need to have a class that transforms all the information in a way that JXLS can understand it. The code below is an example of how you can implement it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let’s explain the code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Load the template&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Context&lt;/strong&gt; is a map with all the attributes you will send to the JXLS to generate the document. For this specific example, the method &lt;strong&gt;“createDocument”&lt;/strong&gt; receives a Map so you can reuse this code on any template and send a dynamic number of attributes to it.&lt;/li&gt;
&lt;li&gt;JXLS process all the information and generate a new report.&lt;/li&gt;
&lt;li&gt;Close the outputStream.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The objective of this example is to reduce the lines of code to create multiples reports so for that reason the method receives the name of the template and the attributes to send to JXLS. Also, the method receives the outputStream so you can save the document in a file for a desktop application or download it in a web application in a simple way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generating a template
&lt;/h3&gt;

&lt;p&gt;Now you need to create the template. Imagine that someone in your team sends this document and tells you he/she needs this particular format.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhi70ibhuogbpj5grc7cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhi70ibhuogbpj5grc7cw.png" alt="The rows in orange are inactive clients"&gt;&lt;/a&gt;&lt;br&gt;
First, you need to clear the document, the focus is on the format of the template.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcxtqrscwwi56wbfrxye2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcxtqrscwwi56wbfrxye2.png" alt="Empty document"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The second step is to add the tags to provide some logic in the document, there are two ways to do it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As comments in the cells&lt;/li&gt;
&lt;li&gt;Explicit in the document
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0zngoe2lc0u3t9m4kzej.png" alt="jx:each in an explicit way
"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most common tags of JXLS are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;jx:area&lt;/strong&gt; this tag tells JXLS is the area to process.&lt;/p&gt;

&lt;p&gt;— &lt;strong&gt;lastCell&lt;/strong&gt;: in this attribute, you need to add which is the last cell to process. This is important because JXLS not process anything after that cell.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;jx:each&lt;/strong&gt; the idea behind this tag is to iterate a collection of elements.&lt;/p&gt;

&lt;p&gt;— &lt;strong&gt;items&lt;/strong&gt;: is a name of the variable in the context which is a collection.&lt;br&gt;
— &lt;strong&gt;var&lt;/strong&gt;: is the name of the variable to use&lt;br&gt;
— &lt;strong&gt;lastCell&lt;/strong&gt;: is the last cell to process for this particular tag&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;jx:if&lt;/strong&gt; you can show the content with one format or another depending on the condition.&lt;br&gt;
— &lt;strong&gt;condition&lt;/strong&gt;: is the condition to validate&lt;br&gt;
— &lt;strong&gt;lastCell&lt;/strong&gt;: is the last cell to process for this particular tag&lt;br&gt;
— &lt;strong&gt;areas&lt;/strong&gt;: is a reference to an area to show in the case of the condition is valid or not.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post only mentions some of the tags but there are more of them to do different things i.e. creating graphics or a multi-sheet. You can check all the available tags in the &lt;a href="http://jxls.sourceforge.net/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The third step is access to each element in the &lt;strong&gt;“Context”&lt;/strong&gt; to populate the document, the way to do it is with the name of the object that you previously inserted in the &lt;strong&gt;“Context”&lt;/strong&gt;. You can show any value of the objects using &lt;strong&gt;${object}&lt;/strong&gt; in any cell. One thing to mention is if you have an object with attributes you can access in this way &lt;strong&gt;“${object.attribute}”&lt;/strong&gt; (i.e. &lt;strong&gt;${client.firstName}&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Now it’s time to implement all the concepts in the post, after modifying the document you can have something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffiqv50z448oknbyk3st2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffiqv50z448oknbyk3st2.png" alt="Final document"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Some comments about the document:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The cell “Created At” have the tag jx:area&lt;/li&gt;
&lt;li&gt;The row below the headers has two tags: jx:each to iterate all the clients and jx:if to show the row with one format or another depending on if the client is active or not&lt;/li&gt;
&lt;li&gt;The formula has two elements because depending on if the condition is valid or not, JXLS uses one row or another to apply the formula.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The last step is to create a class that invokes the “Report” class and sends the location of the generated document. For simplicity in this post, the code of that class is in the repository of Github which appears after the conclusion.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;JXLS is a library with a lot of features that allows you to generate an Excel document in a simple way. The most important thing to consider how easy is for anyone to change the structure, format, or content of the template.&lt;/p&gt;

&lt;p&gt;For curious ones, here is the code on Github and the official documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/andres-sacco/example-jxls" rel="noopener noreferrer"&gt;Examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jxls.sourceforge.net/index.html" rel="noopener noreferrer"&gt;Official documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>excel</category>
      <category>report</category>
      <category>library</category>
    </item>
    <item>
      <title>Archunit: Validate the architecture of our projects</title>
      <dc:creator>Andres Sacco</dc:creator>
      <pubDate>Sat, 30 Jan 2021 17:53:56 +0000</pubDate>
      <link>https://dev.to/andressacco/archunit-validate-the-architecture-of-our-projects-3hc9</link>
      <guid>https://dev.to/andressacco/archunit-validate-the-architecture-of-our-projects-3hc9</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;When you create a microservice or a library, you start defining the location of each group of elements (enums, classes, interfaces) in one or more packages and the best practices, i.e. if you use Spring Boot the controllers should be annotated with &lt;strong&gt;@RestController&lt;/strong&gt;. At the beginning, everything will be fine because a little group of developers works on the project. However, when you start adding developers of your team or from another team in the company add new functionalities is when the error appears.&lt;/p&gt;

&lt;p&gt;To prevent any error in the code of the project, someone in your team needs to review all the changes before merging in master. This approach looks good at the beginning but there are some problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Really time-consuming tasks&lt;/li&gt;
&lt;li&gt;The rules could change in the future so everyone needs to know the new rules before checking any code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How can you resolve this problem in a simple way?
&lt;/h1&gt;

&lt;p&gt;At the back of your mind, the first solution would be tools like &lt;strong&gt;Checkstyle&lt;/strong&gt;, &lt;strong&gt;PMD&lt;/strong&gt;, or &lt;strong&gt;Findbugs&lt;/strong&gt; which does a statical analysis of the code but these tools do not have the possibility to check the structure of the project.&lt;/p&gt;

&lt;p&gt;Some years ago &lt;strong&gt;Archunit&lt;/strong&gt; appeared which is a free and extensible library for checking the architecture of your project using just a unit test. This library gives you the chance to check the layer’s structure (which package/class depends on another), valid annotations in each class, check for cyclic dependencies, and more.&lt;/p&gt;

&lt;p&gt;To start your Archunit tests you will need to add the dependency related to the version of &lt;a href="https://junit.org/junit5/"&gt;Junit&lt;/a&gt; that you use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;com.tngtech.archunit&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;archunit-junitX&amp;lt;/artifactId&amp;gt; //"X" could be 4/5
    &amp;lt;version&amp;gt;0.15.0&amp;lt;/version&amp;gt;
    &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After adding the dependency your first Archunit test should look like the example below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let me explain in detail each block of code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;With this annotation, you tell Archunit which package needs to validate all the rules, a good practice is to put the package which contains all the objects of your project. Also, practice is telling to Archunit not to validate the test just the code that does something.&lt;/li&gt;
&lt;li&gt;You need to indicate the field or method is an Archunit Test.&lt;/li&gt;
&lt;li&gt;This is the test with all the conditions to validate. The example checks that all the fields inside of any class in this particular package aren’t public. Just to clarify, &lt;em&gt;“fields()”&lt;/em&gt; is a static import of &lt;em&gt;“ com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields”&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this post, you can see some examples of which things you can validate with &lt;strong&gt;Archunit&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  General rules of coding
&lt;/h2&gt;

&lt;p&gt;Archunit defines some general rules of coding you can use to validate your project, most of them are defined in the class “GeneralCodingRules”. To use these rules you need to add a test to your project, some examples of these rules are:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Naming conventions
&lt;/h3&gt;

&lt;p&gt;You can validate all the objects in one particular package contain a suffix, i.e. xxxController or xxxService.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Layered architecture
&lt;/h3&gt;

&lt;p&gt;Good architecture needs to have different layers and each of them will only be able to access some other particular layers. You can define the layers and the relation between them, i.e. the controllers in one microservices can not be accessed for any other layer.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Return type
&lt;/h3&gt;

&lt;p&gt;In the controller’s endpoints, you can validate the type of response. For example in the case of Spring Boot, a good practice is to return a “ReponseEntity”.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Annotations
&lt;/h3&gt;

&lt;p&gt;Sometimes you need to validate that some classes/interfaces/methods or, fields use a particular annotation.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Custom rules
&lt;/h1&gt;

&lt;p&gt;There are some cases when the methods to validate some rules are not cover with Archunit by default so you can create custom conditions like validating that your entities/model objects contain “equals” and “hashcode” methods.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
To use this condition in one particular test you can do this:&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Best practices
&lt;/h1&gt;

&lt;p&gt;There are some good practices in order to reduce the lines of code and organize the test depending on the types of rules. Here are some of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define the package of the classes to analyze, the name of the packages or any constant in one class to have a place with all the constants you use in all the tests.&lt;/li&gt;
&lt;li&gt;Define the rules in a way you can reuse them in different places, i.e. you can create a rule that checks that private methods are not allowed and call these methods from different places.
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Try to put all the rules related to one validation group in one class, i.e. create a class that contains all the rules related to the controller validations in the case of one microservice.&lt;/li&gt;
&lt;li&gt;Split each test class to find all the validations related to classes, fields, constructors, and methods in a simpler way. The important thing at this point is to be able to identify each group easier and to be able to prevent the same tests to be checked in a different way.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Archunit&lt;/strong&gt; is a powerful library that gives you the chance to validate every rule you can imagine in your architecture and reduce the number of errors in your projects.&lt;/p&gt;

&lt;p&gt;You need to understand if you add Archunit into an existing project, you would find some issues because most of your projects have some mistakes related to the structure of the names of the classes. It’s key to add the general rules at the beginning and then add the specific ones.&lt;/p&gt;

&lt;p&gt;For curious ones, here is the code on Github:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/andres-sacco/api-example-archunit"&gt;My examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/TNG/ArchUnit-Examples"&gt;Other examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>architecture</category>
      <category>archunit</category>
      <category>design</category>
    </item>
    <item>
      <title>Solve the problems with dependencies in Java</title>
      <dc:creator>Andres Sacco</dc:creator>
      <pubDate>Fri, 29 Jan 2021 11:48:18 +0000</pubDate>
      <link>https://dev.to/andressacco/solve-the-problems-with-dependencies-in-java-352i</link>
      <guid>https://dev.to/andressacco/solve-the-problems-with-dependencies-in-java-352i</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;When we work with projects which use Java, the first thing that we do is add dependencies, libraries or frameworks, that we need to use. To do this, we use a dependency management tool like Maven or Gradle, depending on the benefits that we consider are the best of our projects.&lt;/p&gt;

&lt;p&gt;Each dependency that we add for our project contains other dependencies; most of the developers know this concept under the name of transitive dependencies. In many cases, this situation doesn’t present any problem because our dependency manager resolves in a good way the conflicts of versions between different libraries. Still, there are other cases when these conflicts produce some errors when we use our API/library.&lt;/p&gt;

&lt;p&gt;Please let me clarify the stated above with the following examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmap77pj6szz94fp1bimq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmap77pj6szz94fp1bimq.png" alt="Figure 1 — Our API/library imports some libraries which use a particular library with the same version.&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;br&gt;
Figure 1 — Our API/library imports some libraries which use a particular library with the same version.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F71j9j1veyzjwgihmwk94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F71j9j1veyzjwgihmwk94.png" alt="Figure 2 — Our API/library imports some libraries which use a particular library with different versions."&gt;&lt;/a&gt;&lt;br&gt;
Figure 2 — Our API/library imports some libraries which use a particular library with different versions.&lt;/p&gt;

&lt;p&gt;In the case of figure 1, there isn’t a problem because all the libraries use the same version, so our dependency manager doesn’t need to do anything to resolve it. The problem appears in figure 2 because the libraries use different versions of the same library so Maven/Gradle tries to resolve the conflict between versions in the best way. In some cases, the resolution of the conflict with the version produces an error, the most common are: &lt;strong&gt;“ClassNotFoundException”&lt;/strong&gt; — &lt;strong&gt;“MethodNotSupportedException”&lt;/strong&gt; — &lt;strong&gt;“NoClassDefNotFound”&lt;/strong&gt;, which appears when our dependency management tool takes a version without this particular class or method.&lt;/p&gt;

&lt;h1&gt;
  
  
  How can I resolve this problem?
&lt;/h1&gt;

&lt;p&gt;To resolve this problem, you have different alternatives to find and fix it. Here are the steps that I follow to find the solution:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to find which libraries produce the error. In most cases, the error in the console or the IDE (Eclipse/IntelliJ) shows the entire package of the class so you can find the jar which contains this class. For example: &lt;code&gt;java.lang.NoClassDefFoundError:

ch/qos/logback/core/status/WarnStatus

&lt;/code&gt;
2. In the case that the error is related to some class which not exists, you can use the entire package and use &lt;a href="http://www.findjar.com" rel="noopener noreferrer"&gt;Findjar&lt;/a&gt;. This online search engine helps you to find a library that contains one particular Class.
3. With the name of the dependency which contains the class, you can find all the imports of this library. To do this there are some options depending on your dependency management tool:
&lt;strong&gt;Maven&lt;/strong&gt;
To see all the tree of the dependencies in our project, you can run the following command: &lt;strong&gt;mvn dependency:tree&lt;/strong&gt;
If you want to find just the libraries which include a certain dependency you can run: &lt;strong&gt;mvn dependency:tree&lt;/strong&gt;
&lt;strong&gt;Gradle&lt;/strong&gt;
To see all the tree of the dependencies in our project you can run the following command: &lt;strong&gt;gradle dependencies — scan&lt;/strong&gt;
4. When you detect which libraries contain the dependency that causes the problem, you need to exclude from our dependency management file (pom.xml or build.gradle) all the libraries which contain a different version of the dependency. In some cases, the library which produces the error not exists in the dependency tree because some of the libraries that you added have dependency marks as “provided” so you need to have this library in your pom to fix the problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can use this solution when you run your application/library and the problem appears, but you have another way to detect this problem before that happens. There is a plugin which we can use in our dependency manager (Maven or Gradle) to check the problem with the dependencies; the following are the implementation for each dependency manager:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://maven.apache.org/enforcer/maven-enforcer-plugin/index.html" rel="noopener noreferrer"&gt;Maven Enforce&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kordamp.org/enforcer-gradle-plugin/" rel="noopener noreferrer"&gt;Gradle Enforce&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These plugins have a lot of features to use but the important in our case is &lt;strong&gt;DependencyConvergence&lt;/strong&gt;, which shows all the possible problems with the libraries.&lt;/p&gt;

&lt;p&gt;When you add in your project one of these plugins and run the validation if something is wrong the message that will appear in the console looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvjxqt7rl089v3ppf1wqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvjxqt7rl089v3ppf1wqr.png" alt="Enfoce"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This output means that the project has two dependencies “junit-jupiter-engine” but with different versions. As I mentioned before you can resolve the problem exclude the dependency in “spring-boot-start-test” or tell to dependency management tool which version need to use it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;If you have a very old project and add the enforce plugin maybe you will discover a lot of conflicts to resolve which in some cases it’s impossible to resolve in a very short period but if you create a new project you can add the plugin from the beginning and prevent possible problems in the future.&lt;br&gt;
You can see there are different alternatives to solve this problem. Now it’s up to you to decide which solution is the best one.&lt;/p&gt;

</description>
      <category>java</category>
      <category>maven</category>
      <category>gradle</category>
      <category>dependencies</category>
    </item>
  </channel>
</rss>
