4

I moved from a plain Java EE application to quarkus.io. In Java EE I had the a properties file with version=${project.version} and reeading this file in an JAX RS endpoint. This worked very well.

@GET
public Response getVersion() throws IOException {
    InputStream in = getClass().getClassLoader().getResourceAsStream("buildInfo.properties");
    if (in == null) {
        return Response.noContent().build();
    }
    Properties props = new Properties();
    props.load(in);
    JsonObjectBuilder propertiesBuilder = Json.createObjectBuilder();
    props.forEach((key, value) -> propertiesBuilder.add(key.toString(), value.toString()));
    return Response.ok(propertiesBuilder.build()).build();
}

Now that I am using quarkus and MicroProfile, I wonder if there is a better approach.

I tried it with the ConfigProperty setup from MicroProfile.

@ConfigProperty(name = "version")
public String version;

But I get the following error:

Property project.version not found.

Here is my build section of my pom.

<build>
    <finalName>quarkus</finalName>
    <plugins>
        <plugin>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-maven-plugin</artifactId>
            <version>1.0.0.CR2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire-plugin.version}</version>
            <configuration>
                <systemProperties>
                    <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>
</build>

Is there any solution / better approach?

Binyamin Regev
  • 914
  • 5
  • 19
  • 31
alexander
  • 1,191
  • 2
  • 20
  • 40

3 Answers3

15

Try


@ConfigProperty(name = "quarkus.application.version")
String version;

Also you can read the Implementation-Version from the manifest.

anamarija
  • 166
  • 1
  • 4
  • Hi @anamarija, I tried that. `@ConfigProperty(name ="quarkus.application.version") String quarkusVersion;`. Gaves me the error: `No config value of type [java.lang.String] exists for: quarkus.application.version`. Sorry, I should have included that in my question! – alexander Nov 22 '19 at 10:49
  • Could you post your pom.xml. Are you using the ```quarkus-maven-plugin``` Basically the quarkus build maven plugin takes the value of ```project.version``` and puts it into the ```quarkus.application.version``` if it isn't already set. – anamarija Nov 22 '19 at 11:06
  • I included the build section. – alexander Nov 28 '19 at 06:10
  • Did you manage to get this working? I'm having the same problem in version 1.0.1-Final. I've also tried to set the version via Maven like this in application.properties: quarkus.application.version=${project.version} But this didn't work either. – Serkan Dec 17 '19 at 16:34
  • No, I did not. Maybe we should ping the quarkus guys. – alexander Dec 18 '19 at 10:46
  • There was a bug: https://github.com/quarkusio/quarkus/issues/6255 – alexander Jan 22 '20 at 13:36
0

I'm not sure if my approach is the best case scenario but you can try this:

pom.xml :

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/application.properties</include>
            </includes>
        </resource>
   </resources>

In application.properties use version property:

quarkus.version=${quarkus.platform.version}

Then use it as a config property:

@ConfigProperty(name = "quarkus.version")
String version;
0

Here is another way:

String projectVersion = ConfigProvider.getConfig().getValue("quarkus.application.version", String.class);

Or .getOptionalValue() etc

Murrah
  • 1,508
  • 1
  • 13
  • 26
  • What's the source of this? I always just inject it. – alexander Feb 23 '23 at 08:43
  • https://quarkus.io/guides/config#programmatically-access-the-configuration - I find it useful to use this technique eg inside a function. – Murrah Feb 23 '23 at 18:47
  • I see what you mean, but that's not the current way for me. It's the way if you don't have Dependency Injection available. The way to go with DI is the one from the accepted answer, see: https://quarkus.io/guides/config#inject-the-configuration - maybe you can update the answer to just point out that the answer is just another way? I think that would be helpful to others! – alexander Feb 24 '23 at 09:47