Skip to content

Java Project Usage

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

Java file and packages

The plugin has the ability to perform value replacement within the Java structure. The configuration is slightly different from other file types (XML, JSON, and YAML) because instead of specifying the file path, you need to provide the classpath (for both code and tests) and indicate that the structure is Java. It follows the following structure:

  • type: Attribute to specify the type of structure to be worked with, in this case, it is Java.
  • base_dir: Location of the classpath to be worked with. In this version, separate configurations for the main classpath and test classpath are still required.
  • placeholder: Follows the previous structure of containing the query and name.
  • query: The part of the package that will be replaced by a new value.
  • name: The value that will be used for replacement.

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/com/myPackage/
|                            └── MyClass.java
|                            └── interfaces/
|                                         └── InterfaceClass.java
└── xmls/
|      └── complex/
|                └── generic_2.xml
|      └── generic_1.xml
└── ymls/
|      └── generic1.yml
└── maven-cookiecutter.yml
└── pom.xml 

For this example, the plugin configuration file will be formatted as follows:

mappings:
  - file: pom.xml
    placeholders:
      - query: /project/groupId
        name: Cookiecutter.param.groupId
      - query: /project/artifactId
        name: Cookiecutter.test.replace.map.artifactId
      - query: /project/dependencies/dependency/scope[text()='test']
        name: Cookiecutter.replace.map.scopes

  - file: xmls/generic_1.xml
    placeholders:
      - query: /note/heading
        name: New Reminder

  - file: xmls/complex/generic_2.xml
    placeholders:
      - query: /bookstore/book/author[text()='Kurt Cagle']
        name: Cookiecutter.kurtCagle
      - query: /bookstore/book/year[text()='2005']
        name: Cookiecutter.NewYear

  - file: yamls/generic1.yml
    placeholders:
      - query: mappings[0].file
        name: newFile
      - query: mappings[0].placeholders[0].query
        name: Cookiecutter.query.project.groupId
      - query: mappings[0].placeholders[0].name
        name: Cookiecutter.replace.map.groupId
      - query: mappings[0].placeholders[1].query
        name: Cookiecutter.query.project.artifactId
      - query: mappings[0].placeholders[1].name
        name: Cookiecutter.replace.map.artifactId

  - type: java
    base_dir: src/main/java
    placeholders:
      - query: com.myPackage
        name: cookiecutter.package
The expected behavior for each of the files will be described below for type java:
Before MyClass.java
package com.myPackage;

/**
 * ...
 */
public class MyClass {
...
After MyClass.java
package {{cookiecutter.package}};

/**
 * ...
 */
public class MyClass {
...
Before InterfaceClass.java
package com.myPackage.interfaces;

/**
 * InterfaceClass
 */
public class InterfaceClass {
...
After InterfaceClass.java
package {{cookiecutter.package}}.interfaces;

/**
 * InterfaceClass
 */
public class InterfaceClass {
...

At the end the folder structure should be like this:

root
└── src/ 
|     └── main/java/{{cookiecutter.package}}/
|                            └── MyClass.java
|                            └── interfaces/
|                                         └── InterfaceClass.java
└── xmls/
|      └── complex/
|                └── generic_2.xml
|      └── generic_1.xml
└── ymls/
|      └── generic1.yml
└── maven-cookiecutter.yml
└── pom.xml 
Clone this wiki locally