Skip to content

Instantly share code, notes, and snippets.

@heshamMassoud
Created November 21, 2017 14:36
Show Gist options
  • Save heshamMassoud/7b77957f444bdfb211ec446481b926d7 to your computer and use it in GitHub Desktop.
Save heshamMassoud/7b77957f444bdfb211ec446481b926d7 to your computer and use it in GitHub Desktop.
Get library from properties file
task persistApplicationProperties(type: Task){
description "Persists the version of the library in version.properties so it can be accessed at run time in " +
"/commercetools-sync-java/src/main/java/com/commercetools/sync/commons/utils/SyncSolutionInfo.java"
String libVersion = rootProject.version
String libName = "commercetools-sync-java"
String propertiesFilePath = "src/main/resources/application.properties"
if (!rootProject || !(rootProject.version?.trim())) {
throw new InvalidUserDataException("Please, specify rootProject.version field.")
} else {
Properties props = new Properties()
File propsFile = new File(propertiesFilePath)
props.load(propsFile.newDataInputStream())
/*props.setProperty('name', libName)
props.setProperty('version', libVersion)*/
props.store(propsFile.newWriter(), null)
}
}
compileJava.dependsOn persistApplicationProperties
public class SyncSolutionInfo extends SolutionInfo {
static final String UNSPECIFIED = "unspecified";
-
+ static final String LIBRARY_PROPERTIES = "application.properties";
+ static final String LIBRARY_NAME_PROPERTY = "name";
+ static final String LIBRARY_VERSION_PROPERTY = "version";
public SyncSolutionInfo() throws IOException, IllegalArgumentException {
final InputStream propertiesStream = SyncSolutionInfo.class.getClassLoader()
.getResourceAsStream(LIBRARY_PROPERTIES);
if (propertiesStream != null) {
final Properties libProperties = new Properties();
libProperties.load(propertiesStream);
setName(getPropertyValueOrThrow(libProperties, LIBRARY_NAME_PROPERTY));
setVersion(getPropertyValueOrThrow(libProperties, LIBRARY_VERSION_PROPERTY));
} else {
throw
new IllegalArgumentException(format("Library properties file (%s) doesn't exist. Cannot fetch library "
+ "properties at runtime.", LIBRARY_PROPERTIES));
}
}
private static String getPropertyValueOrThrow(@Nonnull final Properties properties,
@Nonnull final String propertyKey) throws IllegalArgumentException {
return Optional.ofNullable(properties.getProperty(propertyKey))
.orElseThrow(() ->
new IllegalArgumentException("Missing property value '" + propertyKey + "'.\n"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment