How to use maven profile based on the IntelliJ profile? - java

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:

Related

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

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?

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!!

Maven build for Spring boot Application for windows and linux platforms

I need to generate my spring boot application binaries for windows and linux platforms using Maven build.
I am planning to have two different application.properties for windows and linux i.e
application-windows.properties
application-linux.properties
and in the pom.xml I will have this
<profile>
<id>dev</id>
<properties>
<activatedProperties>windows</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<activatedProperties>linux</activatedProperties>
</properties>
</profile>
Maven build will be triggered using Jenkins.But in this way I can have only one profile active at a time,So It will require two jenkins project for windows and linux.
Is there any better way of doing this?
Profile should be chosen by giving build argument while starting the spring boot application.
-Dspring.profles.active=$profile
If you are using jenkins to start the application , just add an option to choose profile during job execution which can add parameter to application.
https://docs.spring.io/spring-boot/docs/1.1.6.RELEASE/reference/html/boot-features-profiles.html

IntelliJ: activate Maven profile when running Junit tests

I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).

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. :-)

Categories

Resources