24 April 2014

Filter Properties and pass them into Java Apps with Maven

If you create an app that will connect to other secured services, the day will come you have to add some developer keys or passwords. Or you want to simply define different environments (production, development, test) for your app running into e.g. for testing purposes. Changing that properties every time or pushing that properties to the repository would be a flaw.

Seriously, you should never push passwords etc. to your repository, no matter if it's private or not!

So what to do to prevent this? Passing that properties with Maven is one solution. Let's start!
Create a Maven Web Application Project if you haven't and open your pom.xml You will probably see a properties section, if not create one and add these properties:



Now go to the build section and add



also add the following plugin to the build -> plugins section



Now the whole maven-part is done but we need some files to test our configuration.
Create a web.xml file under src/main/webapp/WEB-INF/ and fill in the following:

        Faces Servlet
        Faces Servlet

Also add a index.xhtml file within src/main/webapp/ with the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            Project Stage: #{facesContext.application.projectStage}

Now you can Clean & Build the application and deploy it on your Application Server. The starting page index.xhtml will show you your actual project stage. The app will get the property <jsf.projectstage>Development</jsf.projectstage> from Maven and pass it into the web.xml. The web.xml is a Web Resource so the maven-war-plugin is responsible for that. Thus you can quickly change the JSF-Environment of your JSF-Application in the properties section of the Maven pom.xml. In addition you can pass that property when you trigger the build. For this simply execute the following line on the Terminal in your project dir:

mvn clean install -Djsf.projectstage=Production

Now we know the maven-war-plugin filters the web.xml but whats with that other stuff (maven-resources-plugin etc.)? With that you can filter not just Web Resource, you can filter usual resources. So let's do it. Create a filtered_resource.properties file under src/main/resources/

filtered_value = ${property.2}

Now we need something to read the property. For that we create a Stateless EJB:

public class OurService {

 public void readProperties() throws IOException {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  InputStream stream = classLoader.getResourceAsStream("filtered_resource.properties");

  Properties properties = new Properties();

  String value = properties.getProperty("filtered_value", "not_filtered");

  System.out.println("Value: " + value);

When you call the readProperties() method it will print the following on the console if everything is okay:

Reading properties...
Value: default2
...if not it will print:
Reading properties...
Value: not_filtered

That's it! Now you can pass properties into your Java Apps with Maven.

Happy Coding!

1 comment :

  1. Azizi Developments offers Affordable Luxurious residential apartments inAl Furjan which is a mega residential property development community developed near Jebel Ali 1. Nakheel is the master developer of this project and Azizi Developments is one of the Largest Private developer with 24 Plots.

    There are 8 Projects under-construction of which its divided under 2 phases.
    Phase 1 is expected to complete by March 2016 with a 40/60 payment plan which consist of
    Azizi Yasamine
    Azizi Feirouz
    Azizi Iris
    Azizi Orchid
    Azizi Liatris

    and Phase 2 is expected to complete by March 2017 with a 50/50 Payment plan which consist of
    Azizi Tulip
    Azizi Freesia
    Azizi Daisy

    We have an options of 1 Bedroom, 2 Bedroom and3 Bedroom plus Maids in all our projects with European finishing.

    To book your dream home please call us on 00971558089889.

    Fahad Fareed