Merge pull request #9628 from aurasphere/master

[BAEL-4074] Accessing Maven properties in Java
This commit is contained in:
Greg 2020-07-22 08:56:50 -04:00 committed by GitHub
commit f28757b256
6 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,2 @@
/output-resources
/.idea/

View File

@ -0,0 +1,8 @@
## Apache Maven
This module contains articles about core Apache Maven. Articles about other Maven plugins (such as the Maven WAR Plugin)
have their own dedicated modules.
### Relevant Articles
- [Accessing Maven Properties in Java]

View File

@ -0,0 +1,51 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<artifactId>maven-properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven-properties</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.outputDirectory}/properties-from-pom.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<name>${project.name}</name>
<my.awesome.property>property-from-pom</my.awesome.property>
</properties>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.maven.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Reads properties from one file.
*
* @author Donato Rimenti
*/
public class PropertiesReader {
/**
* Properties managed by this reader.
*/
private Properties properties;
/**
* Reads the property file with the given name.
*
* @param propertyFileName the name of the property file to read
* @throws IOException if the file is not found or there's a problem reading it
*/
public PropertiesReader(String propertyFileName) throws IOException {
InputStream is = getClass().getClassLoader()
.getResourceAsStream(propertyFileName);
this.properties = new Properties();
this.properties.load(is);
}
/**
* Gets the property with the given name from the property file.
* @param propertyName the name of the property to read
* @return the property with the given name
*/
public String getProperty(String propertyName) {
return this.properties.getProperty(propertyName);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.maven.properties;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
/**
* Test for {@link PropertiesReader}.
*
* @author Donato Rimenti
*/
public class PropertiesReaderUnitTest {
/**
* Reads a property and checks that's the one expected.
*
* @throws IOException if anything goes wrong
*/
@Test
public void givenPomProperties_whenPropertyRead_thenPropertyReturned() throws IOException {
PropertiesReader reader = new PropertiesReader("properties-from-pom.properties");
String property = reader.getProperty("my.awesome.property");
Assert.assertEquals("property-from-pom", property);
}
}

View File

@ -23,6 +23,7 @@
<module>maven-unused-dependencies</module>
<module>maven-war-plugin</module>
<module>maven-profiles</module>
<module>maven-properties</module>
<module>versions-maven-plugin</module>
<module>version-collision</module>
</modules>