-->

How can I get the version of the application (defi

2020-05-06 02:32发布

问题:

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?

回答1:

Try


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

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



回答2:

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;