Skip to content

Java Project Usage

Micci - Luiz Miccieli edited this page Dec 12, 2024 · 4 revisions

Java file and packages

This guide provides a step-by-step example for configuring the Templify plugin to perform automated package name replacements in Java files, particularly useful when managing multiple files under a large package structure. This example will cover how to replace package names consistently throughout a sample project.

Let's use a simple project structure to illustrate the usage of the plugin. The project structure will be as follows:

root
└── src/ 
|     └── main/java/br/com/client/sfc/datalake/sfcdatatransferdatalake/
|                                                                   └── adapters/
|                                                                           └── MyAdapter.java
|                                                                   └── controllers/
|                                                                           └── MyAdapter.java
|                                                                   └── domains/
|                                                                           └── MyDomain.java
|                                                                   └── exceptions/
|                                                                           └── MyException.java
|                                                                   └── Application.java
└── maven-templify.yml
└── pom.xml 

In this project, we have Java files with the same base package br.com.client.sfc.datalake.sfcdatatransferdatalake in various directories under src/main/java.

Yaml Configuration

The Templify plugin configuration for this project is set up as follows in the maven-templify.yml file:

steps:
  - kind: XmlHandler
    apiVersion: v1
    spec:
      - files:
          - pom.xml
        placeholders:
          - match: /project/groupId
            replace: templify.groupId
          - match: /project/artifactId
            replace: templify.artifactId
  - kind: JavaHandler
    apiVersion: v1
    spec:
      - placeholders:
          - match: br.com.client.sfc.datalake.sfcdatatransferdatalake
            replace: templify.package
        baseDir: src/main/java

spec Configuration Details

Files under: src/main/java/* For Java files located in src/main/java, the spec configuration enables the following operations:

  1. placeholders Specifies the patterns in the Java files that will be replaced.
  • match: Finds occurrences of the base package name (br.com.client.sfc.datalake.sfcdatatransferdatalake).
  • replace: Substitutes the found pattern with templify.package.
  1. baseDir Specifies the root directory where the JavaHandler should apply the placeholder replacements. Here, it is set to src/main/java, covering all Java files in subdirectories.

Expected Behavior in Java Files

Before MyAdapter.java
package br.com.client.sfc.datalake.sfcdatatransferdatalake.adapters;

/**
 * MyAdapter
 */
public class MyAdapter {


}
After MyAdapter.java
package {{templify.package}}.adapters;

/**
 * ...
 */
public class MyAdapter {
Before MyDomain.java
package br.com.client.sfc.datalake.sfcdatatransferdatalake.domain;

import java.util.List;
import br.com.client.sfc.datalake.sfcdatatransferdatalake.controllers.*;
import br.com.client.sfc.datalake.sfcdatatransferdatalake.adapters.MyAdapter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * MyDomain
 */
public class MyDomain {
...
After InterfaceClass.java
package {{templify.package}}.domain;

import java.util.List;
import {{templify.package}}.controllers.*;
import {{templify.package}}.adapters.MyAdapter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * MyDomain
 */
public class MyDomain {
...

Final Project Structure

After applying Templify, the structure should look like this, with the {{templify.package}} placeholder replaced by the defined package name in your templify configuration:

root
└── src/ 
|     └── main/java/{{templify.package}}/
|                                         └── adapters/
|                                                  └── MyAdapter.java
|                                         └── controllers/
|                                                  └── MyAdapter.java
|                                         └── domains/
|                                                  └── MyDomain.java
|                                         └── exceptions/
|                                                  └── MyException.java
|                                         └── Application.java
└── pom.xml 

This setup allows the Templify plugin to handle large Java projects by updating package names automatically, making it easier to maintain a structured, adaptable codebase.