1

I have a Java Maven project with several modules. When referencing to the parent module from the individual modules, the following information is added to project A\module-sample-app\pom.xml:

   <groupId>org.apache.maven</groupId>
   <artifactId>module-sample-app</artifactId>
   <packaging>jar</packaging>
   <name>module-sample-app</name>
   <parent>
      <groupId>org.apache.maven</groupId>
      <artifactId>sample-app-parent</artifactId>
      <version>1.0-${buildLifeId}</version>
    </parent>

The parent application A has the following entry in A\pom.xml:

   <artifactId>A</artifactId>
   <packaging>jar</packaging>
   <name>A</name>
   <version>1.0-${buildLifeId}</version>

To run the build, the following is executed on the parent directory of project A:

Example: mvn clean install -DbuildLifeId=1001

The JAR file generated has an embedded pom.xml with string ${buildLifeId} and not the value 1001.

When trying to use on the module sample-app in another application, B, as the runtime variable was not replaced with the actual value, it is failing to fetch the dependency. Just to highlight, B is not a module of A.

In B\pom.xml:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>module-sample-app</artifactId>
    <version>1.0-1001</version>
</dependency>

With this dependency, as the pom.xml embedded within the module-sample-app-1.0-1001.jar would have the parent version defined as <version>1.0-${buildLifeId}</version>, the dependency fails to load.

So my questions is - does Maven support a plugin that can replace the run time variables before packaging the pom.xml into the JAR? Or is there a workaround. Can I use Ant plugins to replace the buildLiefeId variable in the version node to actual value when JAR is built?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
gautam
  • 21
  • 8
  • Is it primarily for the version number in the jar file that you're doing this? – ernest_k Aug 08 '18 at 14:35
  • Yes, for the version number alone. When I try to add a dependency on this module in a completely different application (Say App-B), since the pom.xml has a variable which doesn't get replaced, the dependency isn't resolved in the new application (App-B). – gautam Aug 08 '18 at 14:43
  • See also [Maven project version inheritance - do I have to specify the parent version?](https://stackoverflow.com/q/10582054/1744774). – Gerold Broser Aug 08 '18 at 15:11
  • Thanks @Gerold. I tried to rewrite the question to make it more detailed around the problem am facing. – gautam Aug 08 '18 at 15:26
  • 1
    You can do that but you have to use `${revision}` or `${sha1}` or `${changelist}` and use it with care see the documentation https://maven.apache.org/maven-ci-friendly.html Furthermore if you really use something `1.0-1001` you have created a release which seemed to be more related to a SNAPSHOT version instead. You have to use Maven 3.5.0+ (best would be to use Maven 3.5.4)... – khmarbaise Aug 08 '18 at 15:31
  • @khmarbaise, yes that is what am looking for. Thank you. Let me try it out, not sure if our repositories have 3.5.0+ as we are still at a lower version. But this is very helpful. Thank you! – gautam Aug 08 '18 at 16:25
  • I strongly recommend to use the most recent version of Maven... – khmarbaise Aug 08 '18 at 16:37
  • It didn't resolve my problem. I get a similar error: Failed to read artifact descriptor for org.apache.maven:module-sample-app:jar:1.0-1001: Could not transfer artifact org.apache.maven:sample-app:jar:${revision}${changelist} from/to central . It is still unable to replace the ${revision} on the pom.xml which is embedded within the module-sample-app.jar – gautam Aug 08 '18 at 17:53
  • @khmarbaise thank you for informing about the ${revision}. It helped with so many other projects. – gautam Aug 16 '18 at 16:30

1 Answers1

1

I was already using ${revision} - I just had to use the Maven Flatten plugin in both the parent and child poms to fix this issue.

r590
  • 689
  • 1
  • 10
  • 15