webapp resources into target - java

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/");
}

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>

Is it possible to exclude (or include) classes or resources on a per profile basis in Maven?

I have two maven profiles P1 and P2 and what I want to do is that depending on the profile I use to build my project, certain resources should be excluded.
For example
<profiles>
<profile>
<id>P1</id>
<properties>
<app.home>Path to project home</app.home>
<exclude>src/main/java/foo/*.*</exclude> <!-- need to exclude all files in src/main/java/foo in this profile -->
</properties>
</profile>
<profile>
<id>P2</id>
<properties>
<app.home>Path to project home</app.home>
<exclude>src/main/java/bar/*.*</exclude> <!-- need to exclude all files in src/main/java/bar in this profile-->
</properties>
</profile>
</profiles>
So, here what I want to do is to exclude all files in src/main/java/foo/ when I build using the P1 profile and exclude all files in src/main/java/bar when I build using the P2 profile.
Is this possible and if not is there any alternative?
You can add a build with the Maven Compiler Plugin to your profile and add a exclude in there
E.g.
<profile>
<id>P1</id>
<properties>
<app.home>Path to project home</app.home>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**src/main/java/foo/*.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
See for additional information Maven: excluding java files in compilation
If you are using spring boot maven plugin, use should do it like this:
<profiles>
<profile>
<id>sample-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/example/foo/ToSkip.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This would do below. For more details, see: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<excludes>
<exclude>**/*.bmp</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>

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 resources does not copy anything to .properties file

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}"

Categories

Resources