0

I have a maven based multi-module Java project. The root directory consists of various sub-modules as well as other directories which holds scripts, configurations and documentations files.

There are certain project metadata which will be provided by CI job via maven command and I want to replace the placeholders with these values.

For example, in CI job, I will pass parameters like below.

mvn clean install -Dpr.version=8.1.0-SNAPSHOT -Dstate.version=R12X -DLicense.key=CDW2025YY
    my-project/                    <-- Root directory (parent POM, not a real module)
    │
    ├── pom.xml                    <-- Parent POM (packaging: pom)
    ├── scripts/
    │   └── startup.ini            <-- Needs token replacement
    │
    ├── config/
    │   └── helm-templates
    │       └── some-config.template  <-- Needs token replacement
    │
    ├── module-A/                     <-- Maven module
    │   ├── pom.xml
    │   └── src/
    │       └── main/
    │           └── resources/
    │               └── application.properties  <-- Needs token replacement
    │
    ├── module-B/                     <-- Another Maven module
        ├── pom.xml
        └── ...

The other maven sub-modules are sorted and I am able to resolve placeholders as they are declared child of root pom.xml but since scripts and config directory in root directory are not declared child module in root pom.xml as they do not contain any source code, I am unable to resolve these placeholders in these directories.

I have tried maven-antrun-plugin to replace placeholders with their value. This is bind to validate phase. But on checking maven logs, I can see that the plugin is executing for all sub-modules but not for the parent pom.xml. The same I tried with maven-replacer-plugin as well but same results.

NOTE: If I run the antrun plugin explicitely via command "mvn antrun:run" then it works, but I want to bind this with "mvn clean install" command.

Below is my maven-antrun-plugin configuration.

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>3.1.0</version>
          <inherited>false</inherited>
          <executions>
              <execution>
                  <id>replace-pr-version</id>
                  <phase>validate</phase>
                  <goals>
                      <goal>run</goal>
                  </goals>
                  <configuration>
                      <target>
                          <echo>Replacing version string in files at root level</echo>
                          <replace token="##PR_VERSION##" value="${pr.version}">
                              <fileset dir="${project.basedir}">
                                  <include name="config/helm-templates/some-config.template"/>
                                  <include name="scripts/startup.ini"/>
                              </fileset>
                          </replace>
                      </target>
                  </configuration>
              </execution>
          </executions>
      </plugin>
3
  • 2
    Take a look about filtering... maven.apache.org/plugins/maven-resources-plugin/examples/… I would make a separate module which contains the scripts, config ... the application.properties can be configured to be filtered within the module... Commented Jun 4 at 18:33
  • @khmarbaise This is an existing enterprise project, can't change the project structure. Also, root directory is not a module as per maven, it only holds pom.xml with packaging=pom, so maven do not run plugin for root directory. Filtering is working for inner modules but not for root directory and other inner directories. Commented Jun 5 at 6:58
  • If it's existing and you can't change it ... that means you can fix it... That the root directory contains a packaging:pom is known to me... So as I suggested make a separate module of it .... and you can do it... Commented Jun 12 at 22:14

1 Answer 1

0

What khmarbaise meant in the first comment to the question is moving the scripts and config dirs into a new Maven project, e.g. resources:

+- my-project
   |
   +- resources
   |  +- pom.xml
   |  +- src/main/resources
   |     +- config
   |        +- helm-templates
   |           +- some-config.template
   |     +- scripts 
   |        +- startup.ini
   |
   +- .... all the others ...
   |
   ⋮ 

and using Resource Filtering (despite its awful, misleading name which should be resource string interpolation) in the files of this project.

If you really can't change the project structure (even not if the output and its functionality stays the same):

  • make config a Maven project (with src/main/resources), put everything there, ignore (the then empty) scripts
  • make each of config and scripts a Maven project (with src/main/resources)
    If you really must you can declare other resources directories than the defaults in their POMs.
  • or leave this project for greener pastures (IMHO, this is ridiculous: Trying to get a half-baked workaround with outdated Ant to work when there is an easy canonical Maven solution.)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.