Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Configure Profiles in a Maven Project

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 450
    Comment on it

    Hello Guys !

    In this blog, we'll see how to configure the profiles in a Maven project. We might have different sensitive information that we need to maintain to different environments.

    For example - A developer might have different server properties like database username, password, drivers etc for local server, QA server and Production Server. It is sometimes very difficult to change the all properties when you release the project for every server.

    Maven provided Profiling to resolve this problem. Follow the following steps to configure the Maven Profiles -

    1. In your POM, add the profiles and profiles under this.

    <profiles>
    	<profile>
    		<activation>
    			<activeByDefault>true</activeByDefault>
    		</activation>
    		<id>dev</id>
    		<properties>
    
    		<!-- DB Connection -->
    		<db.jdbc.driverClassName>com.mysql.jdbc.Driver</db.jdbc.driverClassName>
    		<db.jdbc.url>jdbc:mysql://localhost:3306/devDBName</db.jdbc.url>
    		<db.jdbc.username>devUserName</db.jdbc.username>
    		<db.jdbc.password>devPassword</db.jdbc.password>
    		<!-- Hibernate -->
    		<hibernate.show_sql>true</hibernate.show_sql>
    		<hibernate.dialect>org.hibernate.dialect.MySQLDialect</hibernate.dialect>
    		</properties>
    
    	 </profile>
    	 <profile>
     		<activation>
     			<activeByDefault>false</activeByDefault>
    		</activation>
    		<id>qa</id>
    		<properties>
    		<!-- DB Connection -->
    		<db.jdbc.driverClassName>com.mysql.jdbc.Driver</db.jdbc.driverClassName>
    		<db.jdbc.url>jdbc:mysql://localhost:3306/qaDBName</db.jdbc.url>
    		<db.jdbc.username>qaUserName</db.jdbc.username>
    		<db.jdbc.password>qaPassword</db.jdbc.password>
    		<!-- Hibernate -->
    		<hibernate.show_sql>false</hibernate.show_sql>
    		<hibernate.dialect>org.hibernate.dialect.MySQLDialect</hibernate.dialect>
    
    		
    		</properties>
    	</profile>
    	<profile>
    		<activation>
    			<activeByDefault>false</activeByDefault>
    		</activation>
    		<id>prod</id>
    		<properties>
    
    		<!-- DB Connection -->
    		<db.jdbc.driverClassName>com.mysql.jdbc.Driver</db.jdbc.driverClassName>
    		<db.jdbc.url>jdbc:mysql://localhost:3306/prodDBName</db.jdbc.url>
    		<db.jdbc.username>prodDBUser</db.jdbc.username>
    		<db.jdbc.password>prodPassword</db.jdbc.password>
    		<!-- Hibernate -->
    		<hibernate.show_sql>false</hibernate.show_sql>
    		<hibernate.dialect>org.hibernate.dialect.MySQLDialect</hibernate.dialect>
    
    		</properties>
    	</profile>
    </profiles>

    In the code above, I have mentioned three profile with three different data base server having different properties. We can have many more properties in all environment.

    2. Enable placeholders configuration directory in your project. Keep the following code inside your Maven build tag.

    <resources>
    	<resource>
    		<directory>src/main/resources</directory>
    		<filtering>true</filtering>
    	</resource>
    </resources>

    The resource files must have the the placeholders for the properties defined in the profiles. For example -

    db.jdbc.driverClassName=${db.jdbc.driverClassName}
    db.jdbc.url=${db.jdbc.url}
    db.jdbc.username=${db.jdbc.username}
    db.jdbc.password=${db.jdbc.password}
    hibernate.dialect=${hibernate.dialect}
    hibernate.show_sql=${hibernate.show_sql}

    3. You are done with the profile configuration. Now you are allowed to use the profiling in your run configuration. Please Follow this blog to know how to set the Run Configuration.

    Thanks. Happy coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: