{"id":1724,"date":"2021-09-09T08:43:10","date_gmt":"2021-09-09T08:43:10","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1724"},"modified":"2021-09-09T08:44:51","modified_gmt":"2021-09-09T08:44:51","slug":"maven-profiles","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/09\/09\/maven-profiles\/","title":{"rendered":"Maven Profiles"},"content":{"rendered":"\n<p class=\"has-black-color has-text-color has-normal-font-size\">The concept of profiles let&#8217;s a developer define what he wants to build and what to do in specific cases. In this blog I will talk about the problem that I solved using profiles.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-rounded wp-duotone-000000-ffffff-1\"><figure class=\"aligncenter is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited.jpg\" alt=\"\" class=\"wp-image-1727\" width=\"328\" height=\"437\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited.jpg 1310w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited-225x300.jpg 225w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited-767x1024.jpg 767w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited-768x1025.jpg 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/09\/PicsArt_03-21-03.25.55-edited-1150x1536.jpg 1150w\" sizes=\"(max-width: 328px) 100vw, 328px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Multiple Modules<\/h3>\n\n\n\n<p>In my source code there were around 8 sub-projects that were part of the same code base<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Module Parent\n\\-Module Schemas\n  Module Binary\/War\n  Module DockerImage\n  Module Simulators Binaries\n  Module MainAppChart\n  Module Simulator Docker Images\n  Module Simulator Charts\n  \\- Module Simulator1<\/code><\/pre>\n\n\n\n<p>The building was a nightmare as you would have imagined considering people coming and going out in an organisation (not specific to any particular work-exp) people added and due lack of awareness or guidelines we ended up in this nightmare.<\/p>\n\n\n\n<p>First thing to do was define parent-pom and child-pom relationship for all the modules. Parent pom had it is <code>artifactId<\/code> and <code>groupId<\/code><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Parent<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n&lt;groupId&gt;me.samarthya&lt;\/groupId&gt;\n&lt;artifactId&gt;module-parent&lt;\/artifactId&gt;\n&lt;version&gt;20.0.0-SNAPSHOT&lt;\/version&gt;\n&lt;packaging&gt;pom&lt;\/packaging&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Child<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n&lt;artifactId&gt;module-main-war&lt;\/artifactId&gt;\n&lt;name&gt;My web app&lt;\/name&gt;\n&lt;packaging&gt;war&lt;\/packaging&gt;\n\n&lt;parent&gt;\n   &lt;groupId&gt;me.samarthya&lt;\/groupId&gt;\n   &lt;artifactId&gt;module-parent&lt;\/artifactId&gt;\n   &lt;version&gt;20.0.0-SNAPSHOT&lt;\/version&gt;\n&lt;\/parent&gt;<\/code><\/pre>\n\n\n\n<p>Repeating these for modules helped me define the required relationship which was easy to view using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn -q -Dexec.executable='echo' -Dexec.args='${project.groupId} ${project.version} ${project.artifactId} ${project.packaging}' exec:exec<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>me.samarthya 20.0.0-SNAPSHOT module-parent pom\n<meta charset=\"utf-8\">me.samarthya 20.0.0-SNAPSHOT module-schemas pom\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-main-war war\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-main-chart helm\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-main-docker pom\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-simulator pom\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-all-simulators war\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-simulator-charts pom\n<meta charset=\"utf-8\">me.samarthya <meta charset=\"utf-8\">20.0.0-SNAPSHOT module-simulator1 helm<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Coming to profiles&#8230;<\/h2>\n\n\n\n<p>Essentially all I learnt is from the official <a rel=\"noreferrer noopener\" href=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-profiles.html\" data-type=\"URL\" data-id=\"https:\/\/maven.apache.org\/guides\/introduction\/introduction-to-profiles.html\" target=\"_blank\">documentation<\/a>.<\/p>\n\n\n\n<p>Profiles are specified using a subset of the elements available in the POM itself (plus one extra section), and are triggered in any of a variety of ways.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\" id=\"Modify\"><blockquote class=\"has-text-color has-white-color\"><p>&#8220;Profiles modify the POM at build time, and are meant to be used in complementary sets to give equivalent-but-different parameters for a set of target environments (providing, for example, the path of the appserver root in the development, testing, and production environments).&#8221;<\/p><cite>official documentation<\/cite><\/blockquote><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>The kind of profiles <ul><li>Global<\/li><li>Per user <\/li><li>Per project <\/li><\/ul><\/li><li>How you trigger a profile through command line<ul><li>From the command line (using <code>-P profileName<\/code>)<\/li><\/ul><ul><li>Through Maven settings (<code>&lt;settings&gt; &lt;activeProfiles&gt; &lt;activeProfile&gt;profile&lt;\/activeProfile&gt;&lt;\/activeProfiles&gt;&lt;\/settings&gt;<\/code>)<\/li><li>Based on environment variables<\/li><\/ul><ul><li>OS settings<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How I used them?<\/h2>\n\n\n\n<p>For me the convenience was to allow grouping specific modules under a profile to allow activation from command line or CI\/CD which can help generate <code>artifacts<\/code> just by manipulating one argument<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mvn clean install deploy -P profile1<\/code><\/pre>\n\n\n\n<p>This I achieved by defining the profiles in the <code>parent pom<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code has-normal-font-size\"><code> &lt;profiles&gt;\n        &lt;profile&gt;\n            &lt;id&gt;dev&lt;\/id&gt;\n            &lt;modules&gt;\n                &lt;module&gt;<meta charset=\"utf-8\">module-schemas&lt;\/module&gt;\n                &lt;module&gt;<meta charset=\"utf-8\">module-main-war&lt;\/module&gt;\n            &lt;\/modules&gt;\n        &lt;\/profile&gt;\n        &lt;profile&gt;\n            &lt;id&gt;helm&lt;\/id&gt;\n            &lt;modules&gt;\n                &lt;module&gt;<meta charset=\"utf-8\">module-main-chart&lt;\/module&gt;\n                &lt;module&gt;<meta charset=\"utf-8\">module-simulator1&lt;\/module&gt;\n            &lt;\/modules&gt;\n        &lt;\/profile&gt;\n&lt;\/profiles&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The concept of profiles let&#8217;s a developer define what he wants to build and what to do in specific cases. In this blog I will talk about the problem that I solved using profiles. Multiple Modules In my source code there were around 8 sub-projects that were part of the same code base The building [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1725,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[133],"class_list":["post-1724","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-maven","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1724"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1724\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1725"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}