telebog
telebog

Reputation: 1896

Maven project gets the version of other maven project?

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.

Upvotes: 1

Views: 715

Answers (3)

telebog
telebog

Reputation: 1896

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").

Upvotes: 0

Freiheit
Freiheit

Reputation: 8757

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.

Upvotes: 2

lkamal
lkamal

Reputation: 3938

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>

Upvotes: 4

Related Questions