1

I have to maven projects client and server. The version of the server is always the version of the client. This "version" is not the version of the maven artifact of the client. Is more a property from client that I need it in the server. It is created from a property of the pom and the build number or "trunk-snapshot" (when the client is build on the developer machine).

The client version is created @build time (Jenkins build or locally mvn clean install). I need to read the client version on the server module using Java.

What would be the best approach?

I tried to create a text file as artifact of the client, but I couldn't find an easy way to read it in the server.

telebog
  • 1,706
  • 5
  • 25
  • 34
  • Are you simply referring to two maven projects when you mentioned client project and server project? Is there a parent project that builds both those projects or are they completely independent and built separately in two separate machines? – lkamal Jan 17 '19 at 18:12
  • Yes two maven project that have the same parent. But they can be build separately. We have an artifactory usually they are build on the same machine. – telebog Jan 17 '19 at 18:14
  • 1
    You should be able to define a property in parent pom and refer in modules, I have added a sample as an answer below. – lkamal Jan 17 '19 at 18:23

3 Answers3

4

Simplest solution would be to define a property in your parent pom file and refer that in both client and server modules.

pom.xml
server/pom.xml
client/pom.xml

Now parent pom.xml will have below.

<properties>
    <my.custom.version>1234</my.custom.version>
</properties>

In server and client pom.xml files, you can refer it as below.

<version-needed-tag>${my.custom.version}</version-needed-tag>
lkamal
  • 3,788
  • 1
  • 20
  • 34
  • how can I update the property when I build the client? – telebog Jan 18 '19 at 10:05
  • One option is to pass the value of the property via command line. mvn clean install -Dmy.custom.version=1234 – lkamal Jan 18 '19 at 10:12
  • K, this will update the property of the client's artifact(s) and then how I can get this property for the server module using java? – telebog Jan 18 '19 at 10:16
  • When you build the two modules client and server using the parent pom, you will need to pass the property via command line. Then both sub modules will receive the same value for the property. – lkamal Jan 18 '19 at 10:22
  • yes, that works, but my main issue is that most of the time they are build separately, so I need a way @ server module to read the version from the client artifact(s) – telebog Jan 18 '19 at 10:40
  • So, when you build the client artifact a property value will be there, and an artifact will be generated. Then, later when you build the server artifact, you want to refer to the client artifact and find out the property value used in client artifact. Did I understand you question correctly? – lkamal Jan 18 '19 at 11:21
  • yes, now I am trying this solution https://stackoverflow.com/questions/33821074/read-properties-file-in-multi-module-project – telebog Jan 18 '19 at 13:07
2

Review the POM documentation here: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html . Specifically look at project aggregation: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Aggregation

What you likely want is a parent POM which then has a module for the client and a module for the server.

Your project structure will look like this:

./pom.xml <-- this is your parent pom, it has version info, module entries for client/server, and dependencies that are used by both the client and server
./server
./server/pom.xml <- this is the pom for your server
./client
./client/pom.xml <- this is the pom for your client

You can then define the version in your parent pom and your client and server inherit it. To define your build number you can use a solution like lkamal suggests by altering your Maven version property.

Another option to consider is how you generate your build numbers. The Build Number Plugin for Maven can generate either an arbitrary sequential build number or generate one from the SCM version.

Freiheit
  • 8,408
  • 6
  • 59
  • 101
  • I don't think that this helps me. The version is created only at the build time and it is not the version of the artifact. – telebog Jan 17 '19 at 18:17
  • 1
    Are you generating this version using code or using something like the source control version number? If the latter case - use the maven build number plugin to generate that version as part of your project version. http://www.mojohaus.org/buildnumber-maven-plugin/usage.html – Freiheit Jan 17 '19 at 18:29
  • After the client is build how can I get the version in server using Java? – telebog Jan 18 '19 at 10:11
  • Ok so your code is doing something like a sanity check to make sure that the version of the client calling a server matches the version of the server. Some strategies for that are spelled out here: https://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code . I'll think on this for a bit and post back. I think you have 3 or 4 subtasks to solve your overall use case. – Freiheit Jan 18 '19 at 14:58
  • yes something like that. The application works only if client and server have the same version. – telebog Jan 18 '19 at 17:24
  • 1
    I found a solution using properties-maven-plugin in client to create a properties files that will end up in client.jar. And that one I read it in the server using ClassLoader.getSystemResourceAsStream("my.properties") – telebog Jan 18 '19 at 17:26
0

I solved it using properties-maven-plugin in client to create a properties file, that will end up in client.jar. I read it in the server using ClassLoader.getSystemResourceAsStream("my.properties").

telebog
  • 1,706
  • 5
  • 25
  • 34