Skip to content

Instantly share code, notes, and snippets.

@eratzlaff
Last active April 21, 2016 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eratzlaff/7cdd37edc9a5122f2f0c48a030aa0388 to your computer and use it in GitHub Desktop.
Save eratzlaff/7cdd37edc9a5122f2f0c48a030aa0388 to your computer and use it in GitHub Desktop.
Version file based on pom.xml attributes
mvn -P Production install
mvn install
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<properties>
<env.value>Dev</prop.value>
</properties>
.....
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
</resources>
.....
</build>
....
<profiles>
<profile>
<id>Production</id>
<properties>
<env.value>Prod</env.value>
</properties>
....
</profile>
</profiles>
....
</project>
@Named
@ApplicationScoped
public class Version {
private static final String FILE_NAME = "/META-INF/version.properties";
private static final String VERSION = "version";
private static final String TIMESTAMP = "timestamp";
private static final String NAME = "name";
private static final String VENDOR = "vendor";
private static final String ENV = "env";
public String getVendor() {
try {
return loadConfig().getProperty(VENDOR);
} catch (IOException ex) {
return "";
}
}
public String getTimestamp() {
try {
return loadConfig().getProperty(TIMESTAMP);
} catch (IOException ex) {
return "";
}
}
public String getName() {
try {
return loadConfig().getProperty(NAME);
} catch (IOException ex) {
return "";
}
}
public String getVersion() {
try {
return loadConfig().getProperty(VERSION);
} catch (IOException ex) {
return "";
}
}
public String getEnvironment() {
try {
return loadConfig().getProperty(ENV);
} catch (IOException ex) {
return "";
}
}
private Properties loadConfig() throws FileNotFoundException, IOException {
Properties prop = new Properties();
prop.load(getClass().getResourceAsStream(FILE_NAME));
return prop;
}
}
version=${pom.version}
timestamp=${timestamp}
name=${project.artifactId}
vendor=${project.organization.name}
env=${env.value}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment