Maven: How can I use a separate file specifying developer-specific config? - java

Database URL, username, password vary in our developer team. Now I want to use this way and set up the profile in pom.xml:
<profiles>
<profile>
<id>local</id>
<properties>
<db.url>jdbc:mysql://127.0.0.1:3306/xx?sendStringParametersAsUnicode=false</db.url>
<db.mysql.username>xx</db.mysql.username>
<db.mysql.password>xx</db.mysql.password>
<serverBaseUrl>http://127.0.0.1:8080</serverBaseUrl>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<db.url>jdbc:mysql://xx/xx?sendStringParametersAsUnicode=false</db.url>
<db.mysql.username>xx</db.mysql.username>
<db.mysql.password>xx</db.mysql.password>
<serverBaseUrl>http://xx:8133</serverBaseUrl>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
I want properties like db.mysql.username be set in a separate file named developer-specific-config.properties, and create a file name developer-specific-config.properties.example to tell other developers how to use. Also, I'd like to add developer-specific-config.properties to gitignore so that it won't be traced.
How to finish it?

Related

How to use maven profile based on the IntelliJ profile?

I would like to activate maven profile based on the IntelliJ profile I select. I need to use different dependency when running the app locally and when deploying it to server.
I have these two profiles which select correct version of TimesTen, I would like to use "local" profile when testing the app locally in IntelliJ and the "server" profile when building the app.
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<org.timesten.version>6</org.timesten.version>
</properties>
</profile>
<profile>
<id>server</id>
<properties>
<org.timesten.version>11</org.timesten.version>
</properties>
</profile>
</profiles>
These are my IntelliJ profiles:

Cannot manage maven profiles in IntelliJ

I have a Maven project with a pom.xml with some profiles like this:
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>staging</id>
<properties>
<webdriver.base.url>https://staging.url.de/</webdriver.base.url>
<server.id>staging</server.id>
</properties>
</profile>
<profile>
<id>rc</id>
<properties>
<webdriver.base.url>https://rc.url.de/</webdriver.base.url>
<server.id>rc</server.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<webdriver.base.url>https://www.url.de/</webdriver.base.url>
<server.id>prod</server.id>
</properties>
</profile>
</profiles>
as you can see, in the staging profile I have:
<activation>
<activeByDefault>true</activeByDefault>
</activation>
and if I move this code block to another profile(let's say rc) nothing changes - will be used staging. I have tried even delete staging profile and it executes anyway. Just don't get how to fix it. How to make it possible to execute different profiles, like it was before?
PS this project was working perfect for a long time. But today profiles got crazy.
Since working on IntelliJ, Please do this.
File --> Invalidate Caches and restart
Happy Coding!!

Is it possible in Maven to have more than one profile active at the same time, by default?

I have the following profile setup in Maven:
<profiles>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
<serverAddress>https://example.com/v1</serverAddress>
<pubNubSubscribeKey>blah-blah</pubNubSubscribeKey>
<sentryDsn>https://username:password#sentry.io/id</sentryDsn>
</properties>
</profile>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>dev</environment>
<serverAddress>http://localhost:8080/v1</serverAddress>
<pubNubSubscribeKey>blah blah</pubNubSubscribeKey>
<sentryDsn/>
</properties>
</profile>
<profile>
<id>win32</id>
<activation>
<os>
<family>Windows</family>
<arch>x86</arch>
</os>
</activation>
</profile>
<profile>
<id>win64</id>
<activation>
<os>
<family>Windows</family>
<arch>amd64</arch>
</os>
</activation>
</profile>
</profiles>
There are four profiles but one of the two architectures and one of the two prod/dev profiles should be active at the same time. The dev profile is configured to be active by default, when win64 gets active due to the activation criteria, the dev profile becomes inactive.
Is there a way to keep the dev profile active unless the prod profile becomes active?
In essence, I want a logic like this:
------------------------
| | win32 | win64 |
------------------------
| dev | | X |
------------------------
| prod | | |
------------------------
I have an application.properties template in src/main/resources that looks something like this:
version=${version}
environment=${environment}
server.address=${serverAddress}
pubnub.keys.subscribe=${pubNubSubscribeKey}
sentry.dsn=${sentryDsn}
and then using a filter:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
I populate it with the correct value for either prod or dev. If that doesn't happen, the application fails to start. I cannot relay on my computer's setting.xml or an environment variable for dev to be active, because that means it won't be active on other computers and it would be very confusing.
The activeByDefault is a fallback that is activated if no other profile is activated.
To activate more than one profile, you could either add them in the activeProfiles section of your settings.xml - which kind of makes sense for local development to set the profile to dev or to prod on other machines.
You could trigger it with CLI parameters like the -P option or via System-Property-Activation and -Dpropname=value. The system property option has also a negative-version that triggers the profile if a property is either not-set or doesn't have a specific values. This could well be used to implement an either-or-switch, with the default being triggered if a property is not set:
<profile>
<id>dev</id>
<activation>
<property>
<name>!prod</name> <!-- deactivated if system property prod is set, otherwise activated -->
</property>
</activation>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>prod</name>
</property>
</activation>
</profile>
Deactivate dev/activate prod with
mvn ... -Dprod=true
Apart from these options, it seems there is no direct way of activating a profile unconditionally in combination with another profile that is triggered conditionally.
But there is a little workaround that activates a profile conditionally, with the condition always being true, and that is use the file activation and a file that always exists, like the pom.xml or - if your poms are located in a different position, . works as well
<profile>
<id>dev</id>
<activation>
<file>
<exists>pom.xml</exists>
</file>
</activation>
</profile>
Profiles can also be activated based on the value of System properties.
You could have something like
<profiles>
<profile>
<id>win32</id>
<activation>
<property>
<name>arch</name>
<value>win32</value>
</property>
</activation>
...
</profile>
<profile>
<id>dev</id>
<activation>
<property>
<name>environment</name>
<value>dev</value>
</property>
</activation>
...
</profile>
<profile>
<id>win64</id>
<activation>
<property>
<name>arch</name>
<value>win64</value>
</property>
</activation>
...
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>environment</name>
<value>prod</value>
</property>
</activation>
...
</profile>
</profiles>
now if you set two system properties like arch=win64 and environment=dev then you should have 2 active profiles

How to use maven profile and overlays to build and package with specified project from multiple?

I have one web project called MyWebProject which having sub modules also and packaging as POM, I have other two simple java project called SimpleJavaProject1 and SimpleJavaProject2 which having packaging as JAR.
I am having dependency for both in web peoject. So I have to use Maven Profile and Overlays such way, that when I will build and package my web project with profile JavaProject1 then web project includes SimpleJavaProject1 in its war and when I said JavaProject2 then it should include SimpleJavaProject2. And it should use Overlays only for specified java project.
Can I use Overlays in Profile?
Please suggest some idea if any...
I'm not familiar with overlays, but hopefully this approach will work for them too.
Typically one solves this sort of problem by defining a property in your parent POM, based on the profile:
<profiles>
<profile>
<id>JavaProject1</id>
<properties>
<java.project>SimpleJavaProject1</java.project>
<java.project.version>1.1</java.project.version>
</properties>
</profile>
<profile>
<id>JavaProject2</id>
<properties>
<java.project>SimpleJavaProject2</java.project>
<java.project.version>1.2</java.project.version>
</properties>
</profile>
</profiles>
Then use this property when you define your dependency (and hopefully your overlays too):
<dependency>
<groupId>com.foo</groupId>
<artifactId>${java.project}</artifactId>
<version>${java.project.version}</version>
</dependency>
Got it...referring #Duncan answer I have tried following and its worked. :-)
Following are my Profiles,
<profile>
<id>JavaProject1</id>
<properties>
<roject.groupId>mygroupId</project.groupId>
<roject.artifactId>myartifactId</project.artifactId>
<roject.version>${myversion}</project.version>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>JavaProject2</id>
<properties>
<roject.groupId>mygroupId</project.groupId>
<roject.artifactId>myartifactId</project.artifactId>
<roject.version>${myversion}</project.version>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
And I have added Overlays in war plugin as follows,
<overlays>
<overlay>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<type>jar</type>
<targetPath>WEB-INF/classes</targetPath>
</overlay>
</overlays>
It worked successfully. :-)

is it possible to "extend" a profile in a child module? [duplicate]

This question already has answers here:
Can a maven profile inherit from another maven profile?
(2 answers)
Closed 7 years ago.
say i have a parent pom A, with profiles win32 and win64 activated by os:
<profile>
<id>windows32</id>
<activation>
<os>
<family>windows</family>
<arch>x86</arch>
</os>
</activation>
<properties>
<envClassifier>win-x32</envClassifier>
</properties>
</profile>
<profile>
<id>windows64</id>
<activation>
<os>
<family>windows</family>
<arch>amd64</arch>
</os>
</activation>
<properties>
<envClassifier>win-x64</envClassifier>
</properties>
</profile>
those profiles define env. variables like ${envClassifier} etc.
say that parent module has a child module B which would like to define some extra stuff IN ADDITION on win64:
<profile>
<id>windows64</id>
<properties>
<jreName>jre6u27.zip</jreName>
</properties>
</profile>
can i somehow extend the win64 profile from the parent, or am i doomed to copy-and-paste it along with its activation section and everything?
I just checked this case using mvn help:effective-pom.
Provided you specify <activation> section for the child profile the same way as for the parent profile, properties of these 2 profiles will be merged.
apparently its simply impossible.
i found a good explanation here - http://www.dashbay.com/2011/03/maven-profile-inheritance/

Categories

Resources