maven resources does not copy anything to .properties file - java

I am trying to use maven-resources-plugin to copy properties from maven profiles onto the application properties file but with no luck, and i cant find what i am missing for it to work
I have in my pom.xml:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.Amazon.S3.accessKeyId>123</profile.Amazon.S3.accessKeyId>
<profile.Amazon.S3.secretAccessKey>123</profile.Amazon.S3.secretAccessKey>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<profile.Amazon.S3.accessKeyId>456</profile.Amazon.S3.accessKeyId>
<profile.Amazon.S3.secretAccessKey>4567</profile.Amazon.S3.secretAccessKey>
</properties>
</profile>
</profiles>
AND:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
my properties file:
Amazon.S3.accessKeyId=${profile.Amazon.S3.accessKeyId}
Amazon.S3.secretAccessKey=${profile.Amazon.S3.secretAccessKey}
when running the application (does not matter if i specify a profile or not) i always get an error from spring on this line:
private #Value("${Amazon.S3.accessKeyId}") String S3_ACCESS_KEY;
Could not resolve placeholder 'profile.Amazon.S3.accessKeyId' in string value "${profile.Amazon.S3.accessKeyId}"

Related

Springboot Maven Resource filtering for application.properties

I have a SpringBoot project ( maven / java 8 ).
I want to filter some custom variables in src/main/resources/application.properties by Maven
profile ( dev.properties | prod.properties )
The problem is after compilation in target/MyApp/WEB-INF/classes/application.properties is still the same and not filtered
Maven command:
mvn clean package -Pdev
application.properties :
b.token = ${origin.token}
dev.properties :
origin.token = OixxxxxeyJhbGciOiJIiY3ODkwopsdbuerfazdf
prod.properties :
origin.token = epbeoebbrtrryiterzerfeciIxxxxxxxibmFtZS
pom.xml :
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>src/main/conf/filters/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This solution worked for me, you can choose your profile from Maven -P argument:
Maven command:
mvn clean package -Pdev
application.properties :
#Active Profile
spring.profiles.active = #mainprofile#
dev.properties :
origin.token = OixxxxxeyJhbGciOiJIiY3ODkwopsdbuerfazdf
prod.properties :
origin.token = epbeoebbrtrryiterzerfeciIxxxxxxxibmFtZS
pom.xml
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<mainprofile>dev</mainprofile>
</properties>
</profile>
<!-- Do the same for prod profile here -->
</profiles>

webapp resources into target

web resources are not being copied over to the target/classes folder and hence not in target/${finalName}/WEB-INF/classes folder.
at the moment the output from mvn clean install:
target/classes/(src/main/java)
however I am also trying to get:
target/classes/(src/main/webapp)
pom:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>StrategicTestArchitecture</groupId>
<artifactId>STATestAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<!-- <configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warSourceDirectory>WebContent</warSourceDirectory>
<webResouces>
<resource>
<directory>src/main/webapp</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
<resource>
<directory>${baseDir}/src/main/webapp</directory>
<targetPath>target/${finalName}/WEB-INF</targetPath>
</resource>
</webResouces>
</configuration> -->
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>Spring.properties</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>staging</id>
<activation>
<property>
<name>environment</name>
<value>staging</value>
</property>
</activation>
<properties>
<hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</hibernate.dialect>
<hibernate.show_sql>true</hibernate.show_sql>
<hibernate.url>myoracleurl</hibernate.url>
<DriverClassName>oracle.jdbc.driver.OracleDriver</DriverClassName>
</properties>
</profile>
<profile>
<id>alpha</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<hibernate.dialect>org.hibernate.dialect.Oracle10gDialect</hibernate.dialect>
<hibernate.show_sql>true</hibernate.show_sql>
<hibernate.url>myoracleurl</hibernate.url>
<DriverClassName>oracle.jdbc.driver.OracleDriver</DriverClassName>
</properties>
</profile>
</profiles>
<properties>
<!-- Generic properties -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<!-- SPRING & HIBERNATE / JPA -->
<spring.version>4.3.5.RELEASE</spring.version>
<hibernate.version>5.2.5.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>StrategicTestArchitecture</groupId>
<artifactId>Commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Can anyone shed some light as to why these files are not being copied into the output directory?
As mentioned by Steve C, the web resources do not need to be on the classpath and therefore the web configuration can be left blank. This will copy everything from the src/main/webapp directory into the root of the war file.
In my case we have a folder called resources located at src/main/webapp/resources which contains our html/css/js
For reference in the spring config the registryhandler can modified as such:
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/resources/**").addResourceLocations("resources/");
}

Error loading property file (plugins:maven-war-plugin:2.4:war:default-war:package)

I would like to configure a properties files depending on a profile, either dev or prod, however the location of the properties file is not correctly constructed. I have found answers to similar issues on SO but none of the those answers have helped resolve this issue.
The error I receive is as follows:
Error loading property file 'E:\Development\CodeSource\GitHub\myproject\profiles\dev\mongo.properties' (org.apache.maven.plugins:maven-war-plugin:2.4:war:default-war:package)
The actual location of the properties file is:
E:\Development\CodeSource\GitHub\myproject\src\main\resources\profiles\dev\mongo.properties
So I set the resources location as follows:
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
Nevertheless it still does not find the properties file.
Here is the entire pom file:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>abc.myproject</groupId>
<artifactId>myartifact</artifactId>
<packaging>war</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>projectname</name>
<description>Site Description</description>
<url>http://www.myproject.abc</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<tomcat-deploy-path>output</tomcat-deploy-path>
</properties>
<dependencies>
// various dependencies
</dependencies>
<build>
<finalName>v${project.artifactId}#${project.version}</finalName>
<filters>
<filter>profiles/${build.profile.id}/mongo.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<outputDirectory>${tomcat-deploy-path}</outputDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
</profiles>
</project>
I am using Eclipse.
The path to the filter files is relative to the project root, not to src/main/resources. As such, you need to configure it like so:
<filters>
<filter>src/main/resources/profiles/${build.profile.id}/mongo.properties</filter>
</filters>

Using Maven WAR Plugin with Profiles

I'm trying to get images to my Maven web-application according to the profile selected at build time. But I keep on getting the whole folder of all the profiles to the point where I need the images to go.
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
</profile>
<profile>
<id>dev</id>
</profile>
</profiles>
How to fix this? Default image is banner.jpg and both profiles have respective images in correct order. Inside src/main/profile/webapp/ there are 2 folders user and dev with each having images folder and the banner.jpg inside images folder.
You need to define a custom property inside each profile and then reference that property in the configuration of the plugin:
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/${banner.folder}</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
<properties>
<banner.folder>user</banner.folder> <!-- defines banner.folder property when the user profile is activated -->
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<banner.folder>dev</banner.folder> <!-- defines banner.folder property when the dev profile is activated -->
</properties>
</profile>
</profiles>

Maven , change in order of profile results different build directory struture

I am facing very ridiculous problem here i have a pom.xml which is being used to build the war file .I have introduced profile to work it for different environment(dev/prod) .but the problem is when i create build it create correct build directory for the profile which is below in order in pom.xml
Please assist what is the issue here.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>prod</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>**login.jsp</warSourceExcludes>
</configuration>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
Correct build directory should be
if -Dlifecycle=dev
target/dev/testapp/
if -Dlifecycle=prod
target/prod/testapp/ but here i get 'target/testapp-xx.x'
why is this different behavior?
Command to trigger the build:mvn clean install -Dlifecycle=prod
Adding an id element to the prod profile fix this.
One thing you can do is specify which profile should be built.
Define an ID for each profile:
<profiles>
<profile>
<id>dev</id>
...
<profile>
When building, use the argument -P to inform maven which profiles to activate
mvn clean install -P dev

Categories

Resources