0

I have a project that consists of multiple Maven sub projects. And the structure looks like this

my-project
|
|__sub-project-1
|__sub-project-2
|__sub-project-3
|__sub-project-4
|__sub-project-5
|__sub-project-6
|__sub-project-7
|__scripts
|__pom.xml

Inside the folder scripts I have shell scripts that start my sub projects, i.e.

java \
  -jar -Djava.net.preferIPv4Stack=true \
  -Ddata.folder="test-data" \
   <...>
  "sub-project-1.[1.0.1].jar" \
   <...>

I want to make so, that version (inside square brackets) in shell scripts would update after changing the version inside a sub-project[n] pom.xml.

What is a standard way to achieve?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
miroana
  • 494
  • 3
  • 22
  • Are those sub-project stand-alone projects? I've got the impression yes ? Can you give an example project on github or alike? – khmarbaise Dec 25 '21 at 21:04
  • yes, they are standalone project – miroana Dec 26 '21 at 10:06
  • 1
    Then I would ask why you have combined them into a directory structure ? and using a common script ? Can you also give more details on them ...what kind of project are those? Spring Boot ? Or Just executable jars ? – khmarbaise Dec 26 '21 at 10:26
  • I use a pom aggregator to easier manage these sub-projects, and every sub-project has their own script (located in scripts folder). And yes, they are Spring Boot projects. And I don't use Docker nor any other containerization solution. These are only Spring Boot artifacts (with a self-contained tomcat container) that are invoked via shell script, as of now the biggest downside is that after every release I have to manually update shell scripts (adjusting versions). I thought that maybe there is a standard way in updating versions in shell scripts, but I just fail to figure that way. – miroana Dec 26 '21 at 10:43
  • You should put your shell script into a folder like `src/main/filtered-resources` (or maybe different name) and let Maven filter the shell script and replace the version number if you really need that... https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html – khmarbaise Dec 26 '21 at 11:13
  • You mean that I should create script folder per sub project? – miroana Dec 26 '21 at 11:29
  • @mir you can write only one script with a variable, using the bash command I mention in my answer below – VonC Dec 26 '21 at 11:42
  • You have wrote that each sub project has already a script folder? `and every sub-project has their own script (located in scripts folder)`... ? – khmarbaise Dec 26 '21 at 12:33

1 Answers1

1

From "Maven project version inheritance - do I have to specify the parent version?", the modules of a multi-project are supposed to inherit the parent version.
That allows all children projects to be updated through one mvn command.

That means there should be only one version to manage.

But if not, you would need your script to extract the version through:

  • mvn itself

    VERSION=$(mvn exec:exec -Dexec.executable='echo' \ 
                            -Dexec.args='${project.version}' --non-recursive -q)
    
  • or through bash (even on Windows, using Git for Windows)

    cat pom.xml | grep "^    <version>.*</version>$" | awk -F'[><]' '{print $3}'
    
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The second one is not reliable because grep etc. using for XML does not work always... first one means you have to run it afterwards an release has been created as a supplemental step... apart from that there are easier solutions using the maven-help-plugin.... – khmarbaise Dec 26 '21 at 12:34
  • @khm I agree. I still think one script ought to be enough, but it needs to be more robust indeed. – VonC Dec 26 '21 at 12:52