This is continuation to my previous post. In this post, I
am going to demo a simple application, where there are 3 profiles.
a. test
b.
development
c.
production
test build profile uses the data in
config.test.properties.
development build profile uses the data in
config.dev.properties.
production build profile used the data in
config.prod.properties.
I will demo you, how can we achieve this by using
profiles.
Below step-by- step
procedure explains how can you setup the maven project and use the profiles.
Step 1: Create new
maven project.
Open command prompt/terminal and execute below command.
mvn archetype:generate
-DarchetypeArtifactId=maven-archetype-quickstart
When you execute above command, maven asks groupId,
artifactId, version, and package information and create a simple project.
Give the groupId as org.selflearningjava, artifactId as
selflearningjava, version as 1 and package as 'com.sample.demo'
[INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode Define value for property 'groupId': org.selflearningjava Define value for property 'artifactId': helloworld Define value for property 'version' 1.0-SNAPSHOT: : 1 Define value for property 'package' org.selflearningjava: : com.sample.demo Confirm properties configuration: groupId: org.selflearningjava artifactId: helloworld version: 1 package: com.sample.demo Y: : Y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: C:\Users\krishna\Documents\Study\Maven\projects [INFO] Parameter: package, Value: com.sample.demo [INFO] Parameter: groupId, Value: org.selflearningjava [INFO] Parameter: artifactId, Value: helloworld [INFO] Parameter: packageName, Value: com.sample.demo [INFO] Parameter: version, Value: 1 [INFO] project created from Old (1.x) Archetype in dir: C:\Users\krishna\Documents\Study\Maven\projects\helloworld [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:54 min [INFO] Finished at: 2018-04-23T14:48:50+05:30 [INFO] ------------------------------------------------------------------------
Project structure looks like below.
Step 2: create
'resources' folder under src/main.
Under src/main/resources create four files:
config.properties - This is the default configuration and
will be packaged in the artifact by default.
config.test.properties - This is the variant for the test
environment, and will be used in the test environment.
config.dev.properties - This is the variant for the development
environment, and will be used in the development environment.
config.prod.properties - This is the variant for the
production environment, and will be used in the production environment.
config.test.properties
environment=test
config.dev.properties
environment=development
config.prod.properties
environment=production
Step 3: Update
pom.xml and add three profiles(dev, production, test) to it.
pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.selflearningjava</groupId> <artifactId>helloworld</artifactId> <packaging>jar</packaging> <version>1</version> <name>helloworld</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>test</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/config.test.properties" tofile="${project.build.outputDirectory}/config.properties"/> </tasks> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>test</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>dev</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/config.dev.properties" tofile="${project.build.outputDirectory}/config.properties"/> </tasks> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>dev</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/config.prod.properties" tofile="${project.build.outputDirectory}/config.properties"/> </tasks> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>prod</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
As you see the task of dev profile, it looks like below.
<tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/config.dev.properties" tofile="${project.build.outputDirectory}/config.properties"/> </tasks>
Above task delete the configuration file
'config.properties' and replace the content with config.dev.properties.
Step 4: Execute
the profile.
Go to the directory, where the pom.xml file is located
and run below command to execute test profile.
mvn -Ptest install
[INFO] Executing tasks [delete] Deleting: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\classes\config.properties [copy] Copying 1 file to C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\classes [INFO] Executed tasks [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld --- [INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default) @ helloworld --- [INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1-test.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld --- [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1.jar [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\pom.xml to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1.pom [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1-test.jar to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1-test.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.253 s [INFO] Finished at: 2018-04-23T16:38:03+05:30 [INFO] ------------------------------------------------------------------------
As you see the jar file name ‘helloworld-1-test.jar’, it
contains the profile name in it.
Extract the jar file and open 'config.properties', you
can able to see below configuration in it.
environment=test
Run the command ‘mvn -Pdev install’ to execute the dev
profile.
[INFO] --- maven-antrun-plugin:1.3:run (default) @ helloworld --- [INFO] Executing tasks [delete] Deleting: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\classes\config.properties [copy] Copying 1 file to C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\classes [INFO] Executed tasks [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld --- [INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default) @ helloworld --- [INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1-dev.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld --- [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1.jar [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\pom.xml to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1.pom [INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1-dev.jar to C:\Users\krishna\.m2\repository\org\selflearningjava\helloworld\1\helloworld-1-dev.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.191 s [INFO] Finished at: 2018-04-23T16:42:13+05:30 [INFO] ------------------------------------------------------------------------
Extract the jar ‘helloworld-1-dev.jar’ and open
config.properties, you can able to see below message.
environment=development
No comments:
Post a Comment