Filtering Maven files into WEB-INF - java

I am trying to add some filtering to the application context file, which resides in the WEB-INF directory.
I have the file which is to be filtered (xmlgateway-context.xml) in the folder /src/main/resources.
I have the properties files (config-e05.properties) in the folder src/main/filters
And I have the POM set up as follows:
<!-- environment profiles -->
<profiles>
<profile>
<id>e04</id>
<properties>
<targetenv>e04</targetenv>
</properties>
</profile>
<profile>
<id>e05</id>
<properties>
<targetenv>e05</targetenv>
</properties>
</profile>
</profiles>
<!-- build settings (filtering) -->
<build>
<filters>
<filter>src/main/filters/config-${targetenv}.properties</filter>
</filters>
<resources>
<resource>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
This will mvn install correctly, but when I open the output war file, I was expecting the file xmlgateway-context.xml to be in the /WEB-INF directory, but it ends up in the folder /WEB-INF/classes/WEB-INF.
How can I get this file into the right place.
Alternatively, can I put the application context into a different location and have it referenced there.

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/xmlgateway-context.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
Add the above to your pom.xml.
EDIT: Just to explain what the above conf is doing. With this added, mvn is going to filter files under src/main/webapp/WEB-INF and in particular filter the included files xmlgateway-context.xml and after filtering it is going to push the files in WEB-INF folder (thats what the target tag is saying).
Update if something is not clear.

you should configure filtering via the maven war plugin: checkout these examples.

With filteringDeploymentDescriptors set to true
<build>
<finalName>web-project-name</finalName>
<filters>
<filter>src/main/resources/app.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>
</plugin>
</plugins>
</build>

Put the file xmlgateway-context.xml in src/main/webapp/WEB-INF and configure like this:
<build>
<filters>
<filter>src/main/filters/config-${targetenv}.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp/WEB-INF</directory>
</resource>
</resources>
</build>

Related

Can't add resources to jar in maven

This is my problem: I have a Maven project that is using a Swing library and it is a GUI app which I want to include a resources (images and one config.properties file) in my jar file.
Here is my pom.xml code:
<build>
<resources>
<resource>
<directory>${basedir}/resource</directory>
<includes>
<include>**/*.png</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/config</directory>
<includes>
<include>config.properties</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.settings</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>bis.debug.mode.ui.MainBISDebugMode</mainClass>
</manifest>
</archive>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
and here is my structure to my project:
I included directories as it seen in the pom.xml and the files in it and still doesn't included in jar file and it doesn't see images on the GUI app. When I run the program as Java application everything is fine.
Is it possible to be something wrong in my code? How I can fix the problem?
You need only to put your config file in src/main/resources and no need to add resources in pom.xml because src/main/resources is the maven's default resources folder

Spring BOOT resources

I have a maven project with spring boot, the project is packaged in a jar file. I want to access the txt file or xml file int the folder src/main/resources/. How can I achieve this? I've tried code like this: URL url = ClassLoader.getSystemResource("...");, but it does not work for me.
I have added the resources in the pom.xml file :
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.bin</include>
<include>**/*.tagger</include>
<include>**/*.txt</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
You don't need any resources configuration in pom.xml, I am doing it in this way.
URL url = this.getClass().getClassLoader().getResource("file_name_goes_here.txt");
String fileContent = IOUtils.toString(url);
Note that I have used org.apache.commons.io.IOUtils for getting the file contents.
This is what I have in <build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Maven exclude resources not working

I googled for my issue but I was not able in finding solution; I'm using maven 3.1.2
I have this profile in my pom.xml
<profile>
<id>test</id>
<build>
<finalName>CustomWebAppName</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>environmentConfiguration.properties</exclude>
</excludes>
</resource>
<resource>
<directory>resourcesTest</directory>
<includes>
<include>environmentConfiguration.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>testDist</outputDirectory>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>
As you can see I'm simply telling maven: when you build the test profile you must ignore the file "environmentConfiguration.properties" in folder src/main/resources and consider the file "environmentConfiguration.properties" in resourcesTest
Well when I launch the command mvn clean install -P test my final web application always contains the "environmentConfiguration.properties" file located in src/main/resources and doesn't contain the one in resourcesTest
I add to the question my fully debug log file generated by maven
Please note this file will be available till November 12nd 2015
Can anybody tell me where I'm wrong?
thank you
Angelo
I don't know exactly whether this is your problem or a typo in your question but there shouldn't be any space between -P and test
try this instead
mvn clean install -Ptest
EDIT
try to enable filtering
<profile>
<id>test</id>
<build>
<finalName>CustomWebAppName</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>environmentConfiguration.properties</exclude>
</excludes>
</resource>
<resource>
<directory>resourcesTest</directory>
<filtering>true</filtering>
<includes>
<include>environmentConfiguration.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>testDist</outputDirectory>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</profile>

maven-remote-resources-plugin doesn't copy files, only folder structure

I have a maven project with a module which is a java web application (A module) and another module (B module) that is also a java web application that uses the classes from the A module and its resources. Creating a jar and installing it in the maven repository its no problem to compile the B module but my problem its to copy the resources from A to B.
I am trying to copy the jsp and other files from A module. Following the documentation and some blogs I managed to get the target\classes\META-INF\maven\remote-resources.xml, but when I try to copy those resources in the B module I only get the folder structure but any file inside.
A module pom
<profile>
<id>validator</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<resourcesDirectory>${project.basedir}/src/main/webapp</resourcesDirectory>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
<includes>
<include>**/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
B module pom
<profile>
<id>embedded</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<configuration>
<resourceBundles>
<resourceBundle>my.group:my.artifact:${my.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/config/embedded</directory>
</resource>
</resources>
</build>
</profile>
In the output I get no errors but there are no files in the target\maven-shared-archive-resources\ only the folder structure as it is in module A.
Any idea what I am doing wrong?
Ok, I found a solution.
Looks like remote maven-remote-resources-plugin only works for resources under src/main/resources so in my case the trick was to copy /webapp/* to that folder via resources and to execute the plugin after the resources are copied I had specify <phase>process-resources</phase> in the plugin because otherwise it was been executing before having the resources in their place.
Here is the code for the profile
<profile>
<id>share-resources</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/config/embedded</directory>
</resource>
<!-- This is the one for webapp -->
<resource>
<directory>${project.basedir}/src/main/webapp</directory>
<targetPath>${project.build.outputDirectory}/shared-resources</targetPath>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<resourcesDirectory>${project.build.outputDirectory}</resourcesDirectory>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
<includes>
<include>**/shared-resources/**/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
To retrieve the resources in module B just did as in the documentation

Maven project src/main/resources/myfolder not being created on deployment

I have this set, and everything working fine on a Windows box during tests, Deployment to unix box for testing, the folder is missing and is not created :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Its location is conventional here:
src/main/resources/myfolder
It's deployed as a war, and the directory is not created in WEB-INF/classes.
The rest of my pom looks like this:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/myfolder</directory>
</resource>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>

Categories