diff --git a/maven-modules/maven-properties/.gitignore b/maven-modules/maven-properties/.gitignore new file mode 100644 index 0000000000..bae0b0d7ce --- /dev/null +++ b/maven-modules/maven-properties/.gitignore @@ -0,0 +1,2 @@ +/output-resources +/.idea/ diff --git a/maven-modules/maven-properties/README.md b/maven-modules/maven-properties/README.md new file mode 100644 index 0000000000..65d976189c --- /dev/null +++ b/maven-modules/maven-properties/README.md @@ -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] \ No newline at end of file diff --git a/maven-modules/maven-properties/pom.xml b/maven-modules/maven-properties/pom.xml new file mode 100644 index 0000000000..2cd92da42f --- /dev/null +++ b/maven-modules/maven-properties/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + maven-properties + 0.0.1-SNAPSHOT + maven-properties + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../.. + + + + + junit + junit + 4.13 + + + + + + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + + + generate-resources + + write-project-properties + + + ${project.build.outputDirectory}/properties-from-pom.properties + + + + + + + + + ${project.name} + property-from-pom + + + \ No newline at end of file diff --git a/maven-modules/maven-properties/src/main/java/com/baeldung/maven/properties/PropertiesReader.java b/maven-modules/maven-properties/src/main/java/com/baeldung/maven/properties/PropertiesReader.java new file mode 100644 index 0000000000..e7000ec2ce --- /dev/null +++ b/maven-modules/maven-properties/src/main/java/com/baeldung/maven/properties/PropertiesReader.java @@ -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); + } + +} \ No newline at end of file diff --git a/maven-modules/maven-properties/src/test/java/com/baeldung/maven/properties/PropertiesReaderUnitTest.java b/maven-modules/maven-properties/src/test/java/com/baeldung/maven/properties/PropertiesReaderUnitTest.java new file mode 100644 index 0000000000..a1d6e66047 --- /dev/null +++ b/maven-modules/maven-properties/src/test/java/com/baeldung/maven/properties/PropertiesReaderUnitTest.java @@ -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); + } + +} \ No newline at end of file diff --git a/maven-modules/pom.xml b/maven-modules/pom.xml index c4d8c253df..2de8560244 100644 --- a/maven-modules/pom.xml +++ b/maven-modules/pom.xml @@ -24,6 +24,7 @@ maven-profiles versions-maven-plugin version-collision + maven-properties