bring maven profiles information on jsp - spring

I have maven properties on pom.xml as follow
pom.xml
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
if I want what env value from jsp
what should I do?
I want to use this value like
<c:when test=env=='local'>
</c:when>
<c:when test=env=='dev'>
</c:when>
<c:when test=env=='env'>
</c:when>

You need to use the maven resource filtering which works like this:
Create a build.properties in src/main/resources like:
env=${env}
Then add the resource filter to your pom
<properties>
<java.version>1.8</java.version>
<env>prod</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
After a build your build,properties will look like :
greg#greg-XPS-13-9360:~/work/jsp-example$ cat target/classes/build.properties
env=prod
You can inject them into the JSP in the normal way, for my example I used Spring boot.
Note if you use Spring Boot the build.properties should be of the format
greg#greg-XPS-13-9360:~/work/jsp-example$ cat src/main/resources/build.properties
env=#env#

Related

How to use profile variables to define final properties values?

In Spring Boot, I know I can define profiles on application.yml. However, it's not clear to me how to achieve the same result as in this pom.xml snippet:
...
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pucomex.config.redis.host>devhost</pucomex.config.redis.host>
<pucomex.config.redis.port>5678</pucomex.config.redis.port>
</properties>
</profile>
<profile>
<id>tst</id>
<properties>
<pucomex.config.redis.host>tsthost</pucomex.config.redis.host>
<pucomex.config.redis.port>1234</pucomex.config.redis.port>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>2.5.0.Final</version>
<configuration>
<properties>
<!-- properties based on profile variables -->
<pucomex.config.redis.host>${pucomex.config.redis.host}</pucomex.config.redis.host>
<pucomex.config.redis.port>${pucomex.config.redis.port}</pucomex.config.redis.port>
<!-- global properties -->
<elastic.port>9200</elastic.port>
<elastic.rhlc.connect.timeout.ms>5000</elastic.rhlc.connect.timeout.ms>
<elastic.rhlc.socket.timeout.ms>60000</elastic.rhlc.socket.timeout.ms>
</properties>
</configuration>
</plugin>
</plugins>
</build>
...
In other words: I want to have some properties values varying according to each profile and then use them to define the value of another property.

How to acess a property under specific profile in Maven

I have 2 pom files - ParentPOM and ChildPOM. I want to Access property1 in ParentPOM in ChildPOM. I know this can be done by making ParentPOM as the parent for ChildPOM and use ${property1}. But property1 is defined in 2 profiles - trunk and release and I always want to get the value of property1 defined in release. How do I do that? ie:- In the below example, I want the value should be 0.0.2 and not 0.0.1 when I Access property1 in ChildPOM.
Note:I cannot modify ParentPOM
<project>
<modelVersion>x.x.x</modelVersion>
<artifactId>ParentPOM</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>ParentPOM</name>
<profiles>
<profile>
<id>trunk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property1>0.0.1</property1>
</properties>
</profile>
<profile>
<id>release</id>
<properties>
<property1>0.0.2</property1>
</properties>
</profile>
</profiles>
</project>
<project>
<modelVersion>x.x.x</modelVersion>
<parent>
<groupId>com.temp</groupId>
<artifactId>ParentPOM</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>ChildPOM</artifactId>
<version>2.0.0</version>
<packaging>pom</packaging>
<name>ChildPOM</name>
<profiles>
<profile>
<id>trunk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<property1>x.x.x</property1>
</properties>
</profile>
<profile>
<id>release</id>
<properties>
<property1>y.y.y</property1>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<version>${property1}</version>
</dependency>
</dependencies>
</project>
Introduce a property "releaseProperty1" in your parent pom. Use this property both for the definition of "property1" in the profile "release" and for your use cases in the child pom.
There is one cleanish solution, but it all depends on the why... why do you want this?
The solution is: removing the release-profile from child and execute Maven like this: mvn <goal> -Prelease,!trunk, i.e enable release profile and disable trunk profile.
To confirm this solution, run mvn help:evaluate -Dexpression=property1 -Prelease,!trunk

bring pom's profile value on jsp

I have dev,local, prod profiles on pom.xml
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
is it possible to bring this profile value on jsp?
I want to split some area of jsp with different compile option
<c:choose>
<c:when test="${isFlag =='loc'}">
<c:when test="${isFlag =='dev'}">
</>

exclude data.sql from Spring boot jar

I am trying to exclude my data.sql and schema.sql from the Spring boot application jar.
So far I have tried several options but they do not seem to work. This is my POM configuration.
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<excludes>
<exclude>data.sql</exclude>
<exclude>schema.sql</exclude>
</excludes>
</resource>
</resources>
</build>
</profile>
</profiles>
Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql.If you want to disable this you can try,
spring.datasource.initialize=false
in application.properties or if you want to change default scheme or data scripts locations you can do it with,
spring.datasource.schema=
spring.datasource.data=
You can write this annotation on top of your test method
#TestPropertySource(locations = "classpath:application-test.properties")
Your application-test.properties should have something like this
spring.datasource.data=classpath:/database/seed.sql

Maven Profile Variable web project not working

I´ve got a modular maven project that contains 3 modules. In one of those (WEB), the profile variable is not being recognized as an environment variable, but it takes que real name of it, as shown below:
<Principal POM>
<Module App-core>
<Module ejb>
<Module web>
At the principal POM, there are 3 profiles thas have the same profile propertie with distinct values:
<profile1>
<environment>value1</environment>
<profile2>
<environment>value2</environment>
<profile3>
<environment>value3</environment>
That variable is used as classifier variable for dependencies, as below:
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
At maven compile phase, only at Web Project, it doen´t change the variable name with the variable value.
Any help?
The tags profile1 profile2 profile3 don't enable the replacing property. To do this you have change your parent pom in this way
<project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>profile1</id>
<properties>
<environment>value1</environment>
</properties>
</profile>
<profile>
<id>profile2</id>
<properties>
<environment>value2</environment>
</properties>
</profile>
<profile>
<id>profile3</id>
<properties>
<environment>value2</environment>
</properties>
</profile>
</profiles>
</project>
Now you have to run mvn install -P profile1 yourProjectName
I've resolved the issue.
I've replicated the dependencies into profiles. I had multiples dependencies
This is the code to the profile1
<profile>
<id>profile1</id>
<properties>
<environment>value1</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>
This is the code to the profiles2
<profile>
<id>profile2</id>
<properties>
<environment>value2</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>
And this the code is to the profile3
<profile>
<id>profile3</id>
<properties>
<environment>value3</environment>
</properties>
<dependency>
<groupId>com.test</groupId>
<artifactId>app-core</artifactId>
<version>1.0.0</version>
<classifier>${environment}</classifier>
</dependency>
</profile>

Resources