feat(ext_prop): add build properties

This commit is contained in:
dancingCycle 2022-04-20 11:09:33 +02:00
parent ee3b8c046e
commit 5e0136eb22
5 changed files with 84 additions and 22 deletions

View File

@ -9,7 +9,7 @@
<url>https://swingbe.de</url> <url>https://swingbe.de</url>
<groupId>de.swingbe</groupId> <groupId>de.swingbe</groupId>
<artifactId>ext_prop</artifactId> <artifactId>ext_prop</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<licenses> <licenses>
@ -26,7 +26,9 @@
<properties> <properties>
<!-- Other properties --> <!-- Other properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.properties>src/main/resources/version.properties</version.properties> <app.date>${maven.build.timestamp}</app.date>
<maven.build.timestamp.format>yyyy-MM-dd:HH:mm:ss</maven.build.timestamp.format>
<app.version>${pom.version}</app.version>
</properties> </properties>
<build> <build>
@ -103,6 +105,7 @@
<configuration> <configuration>
<files> <files>
<file>src/main/resources/version.properties</file> <file>src/main/resources/version.properties</file>
<file>src/main/resources/build.properties</file>
</files> </files>
<outputFile/> <outputFile/>
<properties/> <properties/>

View File

@ -0,0 +1,42 @@
package de.swingbe.ext_prop;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class BuildProperties {
private final Properties properties;
private final java.net.URL url;
public BuildProperties() {
url = ClassLoader.getSystemResource("build.properties");
properties = new Properties();
}
public String getVersion() {
try {
properties.load(url.openStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty("build.version");
}
public String getDate() {
try {
properties.load(url.openStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty("build.date");
}
}

View File

@ -1,28 +1,14 @@
package de.swingbe.ext_prop; package de.swingbe.ext_prop;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello world!"); System.out.println("Hello world!");
Properties properties = new Properties(); VersionProperties versionProperties = new VersionProperties();
java.net.URL url = ClassLoader.getSystemResource("version.properties"); System.out.println("version: " + versionProperties.getVersion());
BuildProperties buildProperties = new BuildProperties();
try { System.out.println("build.version: " + buildProperties.getVersion());
properties.load(url.openStream()); System.out.println("build.date: " + buildProperties.getDate());
} catch (FileNotFoundException fie) { System.out.println("Done!");
fie.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("version: " + properties.getProperty("version"));
Set<String> keys = properties.stringPropertyNames();
for (String key : keys) {
System.out.println(key + " - " + properties.getProperty(key));
}
} }

View File

@ -0,0 +1,29 @@
package de.swingbe.ext_prop;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class VersionProperties {
private final Properties properties;
private final java.net.URL url;
public VersionProperties() {
url = ClassLoader.getSystemResource("version.properties");
properties = new Properties();
}
public String getVersion() {
try {
properties.load(url.openStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties.getProperty("version");
}
}

View File

@ -0,0 +1,2 @@
build.version=${app.version}
build.date=${app.date}