<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Purushothaman S on Medium]]></title>
        <description><![CDATA[Stories by Purushothaman S on Medium]]></description>
        <link>https://medium.com/@thepurushoths?source=rss-14d19520a608------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0spjZsD4FuWuRjqotcNwdg.jpeg</url>
            <title>Stories by Purushothaman S on Medium</title>
            <link>https://medium.com/@thepurushoths?source=rss-14d19520a608------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:15:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thepurushoths/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Test a Spring Boot Common Library in Local for Polyrepo and Monorepo Setups]]></title>
            <link>https://thepurushoths.medium.com/how-to-create-and-test-a-spring-boot-common-library-in-polyrepo-and-monorepo-setups-4309fcf20436?source=rss-14d19520a608------2</link>
            <guid isPermaLink="false">https://medium.com/p/4309fcf20436</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Purushothaman S]]></dc:creator>
            <pubDate>Tue, 28 Oct 2025 16:03:28 GMT</pubDate>
            <atom:updated>2025-10-28T16:41:49.917Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79iBahoyNCqa_MKeMC84jA.png" /></figure><h4>Introduction</h4><p>If you’ve ever worked with multiple Spring Boot microservices, you’ve probably faced this: copy-pasting the same configuration files, logging setup, or Kafka consumer logic across services.</p><p>This kind of duplication not only bloats your codebase but also creates a <strong>maintenance nightmare</strong> — especially when you need to update a shared logic everywhere.</p><p>In this guide, let’s explore how to <strong>create a Spring Boot common library</strong>, reuse it across services in <strong>monorepo</strong> and <strong>polyrepo</strong> setups, and test it locally like a pro.</p><h4>1. Monorepo Setup</h4><p>If you’re using a <strong>monorepo</strong>, the setup is simple.<br>You can include your common library as a <strong>submodule in gradle</strong>, and Gradle will handle dependency resolution automatically.</p><blockquote>settings.gradle.kts</blockquote><pre>include(&quot;common-lib&quot;)<br>project(&quot;:common-lib&quot;).projectDir = File(&quot;../common-lib&quot;)</pre><blockquote>build.gradle.kts</blockquote><pre>dependencies {<br>    implementation(project(&quot;:common-lib&quot;))<br>}</pre><p>You can now import and use classes from your common-lib directly in your application without any extra setup.</p><p><strong><em>Note:</em></strong><em> If you want, you can use common library versions to avoid breaking changes in your service.</em></p><p><strong>2. Polyrepo Setup</strong></p><p>If you’re in a <strong>polyrepo</strong> setup, your common library lives in a different repository than your application.</p><p>You’ll need to <strong>publish the common library</strong> to an artifact repository (like <strong>Jfrog artifactory</strong>, <strong>Nexus</strong>, or <strong>GitHub Packages</strong>) so other services can consume it as a dependency.</p><p>Once published, you can declare it like this in your application:</p><pre>repositories {<br>    maven {<br>      // Your artifactory setup<br>      setUrl(&quot;&lt;artifactory repo url&gt;&quot;)<br>      credentials {<br>          ...<br>        }<br>    }<br>}<br><br>dependencies {<br>    implementation(&quot;com.company:common-lib:1.0.0&quot;)<br>}</pre><h3>Testing the Common Module Locally</h3><p>Before officially publishing your library, it’s best to <strong>test it with an actual service</strong>.<br>There are a few ways to do that:</p><h4>Option 1: Publish a SNAPSHOT Version</h4><p>You can publish a temporary version of your library to your internal repository. Then you can consume it in your application.</p><p>However, this approach can quickly clutter your repository with multiple SNAPSHOT versions.<br>It’s not ideal for active development.</p><h4>Option 2: Link the Common Module Locally</h4><p>For <strong>local development</strong>, you can link your common module directly by updating your settings.gradle and build.gradle files — just like in a <strong>monorepo submodule setup</strong>.</p><p>⚠️ Limitation:<br> You’ll need to ensure both the <strong>application</strong> and the <strong>common library</strong> use the <strong>same Spring Boot version</strong>.</p><h4>Option 3: Publish to Local Maven Repository (Recommended)</h4><p>A cleaner and more flexible method is to <strong>publish your common library to your local Maven repository</strong>, allowing services to consume it without version lock.</p><p>Set &lt;local version&gt; for common library and run publish.</p><pre>./gradlew publishToMavenLocal</pre><p>Then, in your consuming service:</p><blockquote>build.gradle.kts</blockquote><pre>repositories {<br>    mavenLocal()<br>    mavenCentral()<br>}<br><br>dependencies {<br>    implementation(&quot;com.company:common-lib:&lt;local version&gt;&quot;)<br>}</pre><p>Your application now resolves dependencies directly from your local ~/.m2/repository.</p><h4>Testing in a Containerized Environment</h4><p>If your application runs inside Docker, you can <strong>mount your local Maven repository</strong> into the container.<br>This way, your service can still resolve the local library without needing a remote Artifactory.</p><blockquote>docker-compose.yml</blockquote><pre>version: &#39;3.8&#39;<br>services:<br>  app-service:<br>    build: .<br>    volumes:<br>      - ~/.m2:/root/.m2<br>    environment:<br>       ...</pre><p>This mounts your local Maven repo (~/.m2) inside the container so Gradle can resolve dependencies locally.</p><h4>Final Thoughts</h4><p>By modularizing shared logic into a <strong>common Spring Boot library</strong>, you can:</p><ul><li>Eliminate redundant code across services</li><li>Maintain consistent behavior and configurations</li><li>Test and evolve common features independently</li></ul><p>Whether you’re managing a <strong>monorepo</strong> or <strong>polyrepo</strong>, these techniques make developing and testing Spring Boot microservices far more scalable and maintainable.</p><p><strong><em>Have you built a shared Spring Boot library in your team?</em></strong><br>Share your approach or challenges in the comments — I’d love to learn how others handle it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4309fcf20436" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot Single Table Inheritance in a Nutshell]]></title>
            <link>https://thepurushoths.medium.com/single-table-inheritance-in-nutshell-7eeb129a4ed4?source=rss-14d19520a608------2</link>
            <guid isPermaLink="false">https://medium.com/p/7eeb129a4ed4</guid>
            <category><![CDATA[discriminatorcolumn]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[discriminatorvalue]]></category>
            <category><![CDATA[discriminatorformula]]></category>
            <category><![CDATA[single-table-inheritance]]></category>
            <dc:creator><![CDATA[Purushothaman S]]></dc:creator>
            <pubDate>Sat, 14 Dec 2024 15:36:25 GMT</pubDate>
            <atom:updated>2024-12-15T03:36:24.755Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l5Yq_vek7E_ZgWTW51gJqg.png" /></figure><h3>Introduction<strong>:</strong></h3><p>Single table inheritance is a technique used to represent object-oriented inheritance in a relational database. It involves mapping a database table to objects in an object-oriented language, where a specific field in the table indicates the class within the hierarchy to which an object belongs.</p><p>E.g. A electronic shop needs to manage various types of products in their e-commerce platform, such as <em>Electronics</em>, <em>Furniture</em>, and <em>Clothing</em>. Each product type shares common properties (like id, name, and price) but also has unique properties specific to the product type.</p><h4>Example in Kotlin</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Py1AtF1cwVe_vAKk32JUSQ.png" /></figure><pre>import jakarta.persistence.*<br>...<br><br>@Entity<br>@Inheritance(strategy = InheritanceType.SINGLE_TABLE)<br>@DiscriminatorColumn(<br>name = &quot;category&quot;, <br>discriminatorType = DiscriminatorType.STRING)<br>abstract class Product(<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.IDENTITY)<br>    val id: Long? = null,<br><br>    @Column<br>    val category: string? = null<br>    // Add common fields and methods here if needed<br>)<br><br>@Entity<br>@DiscriminatorValue(&quot;ELECTRONIC&quot;)<br>class Electronic(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val electronicFeatures: ElectronicFeatures? = null // Example field for Electronic<br>) : Product(category = &quot;ELECTRONIC&quot;, ...)<br><br>@Entity<br>@DiscriminatorValue(&quot;FURNITURE&quot;)<br>class Furniture(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val furnitureFeatures: FurnitureFeatures? = null // Example field for Furniture<br>) : Product(category = &quot;FURNITURE&quot;, ...)<br><br>@Entity<br>@DiscriminatorValue(&quot;CLOTHING&quot;)<br>class Clothing(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val clothingFeatures: ClothingFeatures? = null // Example field for Clothing<br>) : Product(category = &quot;CLOTHING&quot;, ...)</pre><h4><strong>Annotations</strong></h4><p><strong>Inheritance<br></strong>The @Inheritance annotation in JPA is used to define inheritance strategies for entity classes. It is applied at the class level on the base entity to specify how the inheritance hierarchy is mapped to the database.<br>Inheritance Strategies: SINGLE_TABLE (default), TABLE_PER_CLASS, JOINED.</p><p><strong>DiscriminatorValue<br></strong>@DiscriminatorValue assigns a specific identifier to subclasses in a JPA inheritance hierarchy, enabling efficient polymorphic queries within a shared table.</p><p><strong>DiscriminatorColumn<br></strong>The @DiscriminatorColumn annotation is used to differentiate between entity types in an inheritance hierarchy, particularly when using InheritanceType.SINGLE_TABLE or InheritanceType.JOINED. It specifies the column name and type for storing the entity discriminator value. Also, it should be applied to the parent entity in an inheritance hierarchy.</p><p>It returns the value of a single database column directly for the discriminator. It is straightforward and static, suitable when the discriminator is explicitly stored in one field.</p><p>We should define name (name) and type (discriminatorType). The type column will store the values to identify subclasses and discriminatorType defines type of the identifier. e.g., STRING, INTEGER.</p><p><strong>DiscriminatorFormula<br></strong>@DiscriminatorFormula annotation is used to define a custom SQL formula for determining the discriminator value. It allows for conditional logic or calculations based on one or more fields in the entity.</p><p><strong>When to use DiscriminatorColumn and DiscriminatorFormula?<br></strong>Both @DiscriminatorColumn and @DiscriminatorFormula are used in JPA to map objects to specific entities in an inheritance hierarchy. However, the choice between them depends on the requirements of the mapping logic.</p><h4>Key Differences:</h4><p><strong>@DiscriminatorColumn</strong></p><ul><li><strong>Purpose</strong>: Maps objects to entities using a single field in the database.</li><li><strong>Use Case</strong>: When the discriminator value is stored directly in a specific column of the table.</li></ul><pre>@DiscriminatorColumn(<br>name = &quot;type&quot;, <br>discriminatorType = DiscriminatorType.STRING<br>)</pre><p>Here, the value in the type column is used to identify the specific entity.</p><p><strong>@DiscriminatorFormula</strong></p><ul><li><strong>Purpose</strong>: Maps objects to entities based on a derived or calculated value, often involving multiple fields or expressions.</li><li><strong>Use Case</strong>: When the discriminator value needs to be computed dynamically using SQL expressions.</li></ul><pre>@DiscriminatorFormula(<br>&quot;CASE WHEN type = &#39;ELECTRONIC&#39; THEN &#39;ELECTRONIC&#39; ELSE &#39;NON_ELECTRONIC&#39; END&quot;<br>)</pre><p>Or you can also return value of the entity field from database.</p><pre>@DiscriminatorFormula(<br>&quot;CASE WHEN category = &#39;ACCESSORIES&#39; THEN &#39;ACCESSORIES&#39; ELSE type END&quot;<br>)</pre><p>Here, type is the filed of the entity.</p><p>Imagine a scenario where we have mobile devices, laptops, and their accessories. Mapping values solely based on the category might not work effectively because multiple products fall under dedicated ELECTRONIC category (Mobile, Laptop) and accessories type can be anything. So it is a dynamic type. e.g., mobile case, screen protector, etc.</p><p>To address this scenario, we can use the product type column for electronic devices and the category column specifically for accessories to achieve accurate mapping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYngaj89sBDmRM3sx6jYGg.png" /></figure><pre>import jakarta.persistence.*<br>...<br><br>@Entity<br>@Inheritance(strategy = InheritanceType.SINGLE_TABLE)<br>@DiscriminatorFormula(<br>&quot;CASE WHEN category = &#39;ACCESSORIES&#39; THEN &#39;ACCESSORIES&#39; ELSE type END&quot;<br>)<br>abstract class Product(<br>    @Id<br>    @GeneratedValue(strategy = GenerationType.IDENTITY)<br>    val id: Long? = null,<br><br>    @Column<br>    val category: string? = null,<br><br>    @Column<br>    val type: string? = null<br>    // Add common fields and methods here if needed<br>)<br><br>@Entity<br>@DiscriminatorValue(&quot;MOBILE&quot;)<br>class Mobile(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val furnitureFeatures: FurnitureFeatures? = null // Example field for Mobile<br>) : Product(category = &quot;ELECTRONIC&quot;, type = &quot;MOBILE&quot;, ...)<br><br>@Entity<br>@DiscriminatorValue(&quot;LAPTOP&quot;)<br>class Laptop(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val laptopFeatures: LaptopFeatures? = null // Example field for Laptop<br>) : Product(category = &quot;ELECTRONIC&quot;, type = &quot;LAPTOP&quot;, ...)<br><br>@Entity<br>@DiscriminatorValue(&quot;ACCESSORIES&quot;)<br>class Accessories(<br>    @Type(JsonBinaryType::class)<br>    @Column(name = &quot;features&quot;)<br>    val accessoriesFeatures: AccessoriesFeatures? = null, // Example field for Accessories<br><br>    val type: String, // Dynamic accessory type(eg. mobile case, ...)<br>) : Product(category = &quot;ACCESSORIES&quot;, type = type, ...)</pre><p>If the discriminator formula first checks whether the category is ACCESSORIES, it will map the value to the ACCESSORIES entity. If the condition is not met, it evaluates the device<strong> </strong>type field and maps the value to the corresponding entity.</p><p>This approach is particularly useful in scenarios where mapping logic depends on multiple fields or conditional criteria, enabling more dynamic and context-specific entity identification compared to static mappings with @DiscriminatorColumn.</p><h4>Conclusion</h4><p>Performance-wise, @DiscriminatorColumn is generally more efficient for simple use cases because it relies on static values stored in a dedicated column in the database. This direct mapping ensures faster lookups and less computational overhead during query execution.</p><p>In contrast, @DiscriminatorFormula involves dynamic evaluation of expressions or conditions to derive the discriminator value. While it offers flexibility for complex scenarios, this added computation can result in slightly reduced performance compared to the straightforward approach of @DiscriminatorColumn. Therefore, it is recommended to use @DiscriminatorFormula only when dynamic or multi-field evaluations are necessary.</p><p>Thank you for taking the time to read this article! I appreciate your support and hope you found it insightful. Feel free to share your thoughts or feedback!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7eeb129a4ed4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide to Internationalization (i18n) in React.js Applications]]></title>
            <link>https://thepurushoths.medium.com/a-guide-to-internationalization-i18n-in-react-js-applications-de3a80f4ef77?source=rss-14d19520a608------2</link>
            <guid isPermaLink="false">https://medium.com/p/de3a80f4ef77</guid>
            <category><![CDATA[multilingual-websites]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[react-i18next]]></category>
            <category><![CDATA[i18next]]></category>
            <dc:creator><![CDATA[Purushothaman S]]></dc:creator>
            <pubDate>Sun, 02 Jul 2023 20:17:20 GMT</pubDate>
            <atom:updated>2023-07-02T20:17:20.102Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Internationalization (i18next)" src="https://cdn-images-1.medium.com/max/1024/1*b0JjJ_SzQJYTnEgIKTXk4w.jpeg" /></figure><h4><em>Introduction</em></h4><blockquote>In today’s interconnected world, building applications that cater to a global audience is crucial. Internationalization (i18n) is the process of designing and developing software that can adapt to different languages, cultures, and locales. In this article, we’ll explore how to implement i18n in React.js applications using the popular library, react-i18next.</blockquote><h4>Getting Started with react-i18next</h4><pre>npm install i18next react-i18next</pre><p>This command will install both i18next and react-i18next packages in your project&#39;s node_modules.</p><h4>Managing Translations</h4><p>Create translation files in your project for the component and configure i18n with translations.</p><p>eg: <em>../translations/common/en.json</em></p><pre>{<br>    &quot;TITLE&quot;: &quot;title&quot;,<br>    &quot;TITLE_NAME&quot;: &quot;title name is {{title_name}}&quot;<br>}</pre><p>Create i18n config file.</p><pre>import i18n from &#39;i18next&#39;;<br>import { initReactI18next } from &#39;react-i18next&#39;;<br><br>import commonTranslationsEnglish from &#39;../translations/common/en.json&#39;;<br>import commonTranslationsFrench from &#39;../translations/common/fr.json&#39;;<br>import profileTranslationsEnglish from &#39;../translations/profile/en.json&#39;;<br>import profileTranslationsfrench from &#39;../translations/profile/fr.json&#39;;<br><br>i18n<br>  .use(initReactI18next) <br>  .init({<br>    ns: [&#39;common&#39;, &#39;profile&#39;],<br>    resources: {<br>      en: {<br>        common: commonTranslationsEnglish,<br>        profile: profileTranslationsEnglish<br>      },<br>      fr: {<br>        common: commonTranslationsFrench,<br>        profile: profileTranslationsfrench<br>      }<br>    },<br>    lng: &#39;en&#39;,<br>    interpolation: {<br>      escapeValue: false<br>    }<br>  });<br><br>export {i18n};</pre><h4>Implementing i18n in React Components</h4><pre>import {useTranslation} from &#39;react-i18next&#39;<br><br>function Title() {<br>  const {t: translations} = useTranslation([&#39;common&#39;, &#39;profile&#39;])<br><br>  return (<br>  &lt;&gt;<br>    &lt;div className=&quot;title&quot;&gt;<br>      {<br>        translations(&#39;TITLE&#39;, {ns: &#39;common&#39;})<br><br>        // or translations(&#39;common:TITLE&#39;)<br>      }<br>    &lt;/div&gt;<br>    &lt;div className=&quot;title-name&quot;&gt;<br>      {<br>        translations(&#39;TITLE&#39;, {ns: &#39;common&#39;, title_name: &#39;i18next blog&#39;})<br>      }<br>    &lt;/div&gt;<br>  &lt;/&gt;<br>  );<br>}<br><br>export default Title;</pre><p>You can also use i18n.t() to get translations<br>eg: i18n.t(‘common:TITLE’)</p><h4>Switching Languages Dynamically</h4><p>You can switch languages with <em>i18n.changeLanguage(‘&lt;locale&gt;’)<br></em>eg: <em>i18n.changeLanguage(‘fr’)<br></em>Or use any other language detector to switch between languages.</p><h4>Advantages</h4><p>Expanded user reach, enhanced user experience, increased accessibility, better engagement and conversion, global market penetration, scalability and maintainability, consistent branding, compliance with regulations, and collaboration opportunities.</p><p>For more ref: <a href="https://www.i18next.com/translation-function/essentials">https://www.i18next.com/translation-function/essentials</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de3a80f4ef77" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot Tesseract OCR in Kotlin with multi-stage Docker]]></title>
            <link>https://thepurushoths.medium.com/spring-boot-tesseract-ocr-in-kotlin-with-multi-stage-docker-515cdd13af37?source=rss-14d19520a608------2</link>
            <guid isPermaLink="false">https://medium.com/p/515cdd13af37</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[tesseract]]></category>
            <category><![CDATA[multi-stage-dockerfile]]></category>
            <category><![CDATA[ocr-tesseract]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Purushothaman S]]></dc:creator>
            <pubDate>Sun, 28 May 2023 11:40:12 GMT</pubDate>
            <atom:updated>2025-12-18T04:02:50.816Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>i. Context<br>ii. Challenges and approaches<br>iii. Docker setup<br>iv. OCR operation<br>v. Github source code</strong></blockquote><p><em>To start, let me give you a brief introduction to Tesseract OCR. Tesseract OCR is an open-source optical character recognition (OCR) engine that is used to recognise text from images. It was originally developed by Hewlett-Packard in the 1980s but has since been maintained and updated by Google. Tesseract OCR is widely used in the industry because it is highly accurate and is available for free.</em></p><figure><img alt="Tesseract - Optical Character Recognition (OCR)" src="https://cdn-images-1.medium.com/max/1024/1*v-HvTYIETew3K6KRzAoHfQ.png" /></figure><h3>I. Context</h3><blockquote>In this article, we will learn about extracting text from PDFs and images and setting up a Docker environment to perform OCR with the Tesseract library.<br>Tesseract supports other use cases such as text localisation, character recognition, converting scanned documents to searchable PDFs, etc.</blockquote><h3>II. Challenges and approaches</h3><p>While implementing the OCR feature, we implemented it with a single-stage Docker. But it increased image size and build time. To solve this, we adopted multi-stage Docker and JFrog Artifactory.</p><h3>III. Docker setup</h3><p><em>A multistage Dockerfile</em> is used to <strong>optimise the size</strong> and efficiency of Docker images. We will install the Leptonica and Tesseract libraries in Docker. It may have some unnecessary libraries. Those libraries are not required to build the production image, and they will increase the size of the Docker image. So we will extract only the required libraries for the next stage. Multistage Dockerfile involve using multiple build stages within a single Dockerfile to separate the build environment from the runtime environment.</p><h4><strong><em>.NET </em>equivalent</strong></h4><p><em>For teams working in </em><strong><em>.NET</em></strong><em>,</em><a href="https://ironsoftware.com/csharp/ocr/"><em> </em><strong><em>IronOCR</em></strong></a><em> can simplify this significantly. It bundles Tesseract and all dependencies into a single NuGet package, eliminating the need for multi-stage Docker builds to manage Leptonica, libtiff, libwebp, and trained data files.</em></p><pre>FROM mcr.microsoft.com/dotnet/aspnet:8.0<br>WORKDIR /app<br>COPY . .<br>ENTRYPOINT [&quot;dotnet&quot;, &quot;YourApp.dll&quot;]</pre><p>No COPY statements for individual <em>.so</em> files, no LD_LIBRARY_PATH configuration, and no separate build stage for OCR dependencies.</p><blockquote>Note: The .NET equivalent is included for conceptual comparison.</blockquote><h4>a. Docker build stage for <strong>Tesseract &amp; Leptonica</strong></h4><p>In the build environment (stage one), we have to install <strong>Tesseract</strong> and <strong>Leptonica</strong>. The Leptonica library needs some dependency libraries, such as libtiff, libwebp, libpng, open-jpeg, etc.<br>Download the <strong>trained data set</strong> for Tesseract to perform OCR.</p><p>Create a <em>Dockerfile</em> and paste the following two code blocks in the same Dockerfile.</p><pre>FROM amazoncorretto:11 as build<br>RUN yum update -y &amp;&amp;\<br>    yum -y -q install wget &amp;&amp;\<br>    yum install -y gcc gcc-c++ autoconfig automake make pkgconfig libtool gzip tar&amp;&amp;\<br>    yum install -y zlib-devel libtiff-devel libwebp-devel libpng-devel  openjpeg2-devel lib-jpeg-turbo-devel giflib-devel &amp;&amp;\<br>    yum clean all &amp;&amp;\<br>    rm -rf /var/cache/yum<br><br>RUN wget -q https://github.com/DanBloomberg/leptonica/archive/refs/tags/1.82.0.tar.gz \<br>    &amp;&amp; tar -zxvf 1.82.0.tar.gz -C /opt \<br>    &amp;&amp; rm -f 1.82.0.tar.gz<br><br>WORKDIR /opt/leptonica-1.82.0<br>RUN ./autogen.sh<br>RUN ./configure<br>RUN make &amp;&amp; make install<br><br>RUN wget -q https://github.com/tesseract-ocr/tesseract/archive/5.2.0.tar.gz \<br>    &amp;&amp; tar -zxvf 5.2.0.tar.gz -C /opt \<br>    &amp;&amp; rm -f 5.2.0.tar.gz<br><br>WORKDIR /opt/tesseract-5.2.0<br>RUN ./autogen.sh<br>RUN ./configure<br>RUN make &amp;&amp; make install<br><br>RUN wget https://github.com/tesseract-ocr/tessdata/raw/main/eng.traineddata -P /opt/<br>RUN wget https://github.com/tesseract-ocr/tessdata/raw/main/osd.traineddata -P /opt/</pre><h4>b. Docker final stage</h4><p>Extract only the required libraries to the final stage of the Dockerfile.</p><pre>FROM amazoncorretto:11<br><br>WORKDIR /opt<br><br>ARG LD_LIBRARY_PATH=/usr/local/lib<br>ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH}<br>ENV PKG_CONFIG_PATH ${LIBRARY_PATH}/pkgconfig<br>ARG TESSDATA_PREFIX=/usr/local/share/tessdata<br>ENV TESSDATA_PREFIX ${TESSDATA_PREFIX}<br><br>COPY --from=build /usr/local/lib/libtesseract.so.5.0.2 ${LD_LIBRARY_PATH}/<br>COPY --from=build /usr/local/lib/liblept.so.5.0.4 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libjpeg.so.62.3.0 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libtiff.so.5.2.0 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libwebp.so.4.0.2 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libopenjp2.so.2.4.0 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libgomp.so.1.0.0 ${LD_LIBRARY_PATH}/<br>COPY --from=build /lib64/libjbig.so.2.0 ${LD_LIBRARY_PATH}/<br><br>COPY --from=build /opt/*.traineddata ${TESSDATA_PREFIX}/<br><br>RUN echo ${LD_LIBRARY_PATH} &gt;&gt; /etc/ld.so.conf<br>RUN ldconfig<br><br>WORKDIR /app<br>COPY ./src/main/resources/static/tesseract.png tesseract.png<br>COPY ./src/main/resources/static/tesseract.pdf tesseract.pdf<br><br>COPY ./build/libs/tesseract-ocr-0.0.1.jar tesseract-ocr.jar<br><br>EXPOSE 8080<br>CMD [&quot;java&quot;, &quot;-jar&quot;, &quot;tesseract-ocr.jar&quot;]</pre><p>Now we have optimised the size (<em>Space complixity</em>) of the Docker image.<br>But the build time (<em>Time complexit</em><strong>y</strong>) is increased to extract the binary of Tesseract and Leptonica.</p><p><strong>Note:</strong> To <em>optimise the time complexity</em>, you can use any artifactory repository (ex. <strong>JFrog artifactory</strong>) and create a separate pipeline to push the binary of Tesseract, Leptonica, the Trained Data Set, and its dependency libraries as a one-time task. Later, you can download those binaries and data sets into your Docker image. It will reduce Docker build time.</p><h3>IV. OCR operation</h3><p>Add the Tesseract library to the <em>build.gradle.kts</em> file’s dependencies.</p><pre>import org.jetbrains.kotlin.gradle.tasks.KotlinCompile<br><br>plugins {<br> id(&quot;org.springframework.boot&quot;) version &quot;2.7.12&quot;<br> id(&quot;io.spring.dependency-management&quot;) version &quot;1.0.15.RELEASE&quot;<br> kotlin(&quot;jvm&quot;) version &quot;1.6.21&quot;<br> kotlin(&quot;plugin.spring&quot;) version &quot;1.6.21&quot;<br>}<br><br>group = &quot;com.example&quot;<br>version = &quot;0.0.1&quot;<br>java.sourceCompatibility = JavaVersion.VERSION_11<br><br>repositories {<br> mavenCentral()<br>}<br><br>dependencies {<br> implementation(&quot;org.springframework.boot:spring-boot-starter-web&quot;)<br> implementation(&quot;com.fasterxml.jackson.module:jackson-module-kotlin&quot;)<br> implementation(&quot;org.jetbrains.kotlin:kotlin-reflect&quot;)<br> testImplementation(&quot;org.springframework.boot:spring-boot-starter-test&quot;)<br> implementation(&quot;net.sourceforge.tess4j:tess4j:5.4.0&quot;)<br>}<br><br>tasks.withType&lt;KotlinCompile&gt; {<br> kotlinOptions {<br>  jvmTarget = &quot;11&quot;<br> }<br>}</pre><p>Create document type enums.</p><pre>package com.example.ocr.pdf.enum<br><br>enum class DocumentType {<br>    PDF,PNG<br>}</pre><p>In the following code, we are performing OCR operations for both PDF and images.<br>Text extraction is one of the use cases for the Tesseract library.</p><pre>package com.example.ocr.pdf<br><br>import com.example.ocr.pdf.enum.DocumentType<br>import net.sourceforge.tess4j.Tesseract<br>import net.sourceforge.tess4j.TesseractException<br>import org.springframework.stereotype.Service<br>import java.io.File<br><br>@Service<br>class OCRService {<br><br>    fun getContent(documentType: DocumentType): String {<br>        val tesseract = Tesseract()<br><br>        try {<br>            val filePath = getFilePath(documentType)<br>            val image = File(filePath)<br><br>            tesseract.setDatapath(System.getenv(TESSDATA_PREFIX))<br>            tesseract.setLanguage(&quot;eng&quot;)<br>            tesseract.setVariable(&quot;tessedit_create_horc&quot;, &quot;1&quot;)<br>            tesseract.setPageSegMode(1)<br>            tesseract.setOcrEngineMode(1)<br><br>            println(&quot;Document type ===&gt; ${documentType.name}&quot;)<br>            return tesseract.doOCR(image)<br>        } catch (e: TesseractException) {<br>            throw Exception(e)<br>        }<br>    }<br><br>    fun getFilePath(documentType: DocumentType): String {<br>        return when (documentType) {<br>            DocumentType.PDF -&gt; PDF_FILE<br>            DocumentType.PNG -&gt; PNG_FILE<br>        }<br>    }<br><br>    companion object {<br>        const val TESSDATA_PREFIX = &quot;TESSDATA_PREFIX&quot;<br><br>        const val PDF_FILE = &quot;tesseract.pdf&quot;<br>        const val PNG_FILE = &quot;tesseract.png&quot;<br>    }<br>}</pre><h3>V. Github source code</h3><p><a href="https://github.com/thisispurushoth/tesseract-ocr">https://github.com/thepurushoths/tesseract-ocr</a></p><p>If there are any other optimized approaches, then let me know in the comments. I will learn with you.</p><p>Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=515cdd13af37" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>