Spring BOOT resources - java

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>

Related

Eclipse (latest) artifact builder does not include ressources

i'm currently confused that eclipse ignores my configured pom building instructions.
Used : Eclipse 2023-03 / 2022-12 (both same failure)
parent pom :
...
<modules>
<module>my-module</module>
<module>my-webapp</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-module</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<excludes>
<exclude>**/module.dtd</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.14.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
my-module structure (pom refers to parent without any build instructions - packaging -> jar) :
src/main/java
---...
src/main/resources
src/main/resources/META-INF/test/file.xml
src/main/resources/example/bootstrap/file2.xml
my-webapp (refers to parent pom - packaging -> war) :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${config.path}</directory>
<targetPath>WEB-INF/config</targetPath>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
Build with mvn clean install results in a deployable war file which contains my-module.jar in lib folder as expected, size matches also the generated jar file in my-module targets folder.
so targets generated .war/.jar are correct.
Now i setup inside eclipse a default tomcat server above v.9.0.50+ (tried multiple versions, also latest 9.0.71). and added my-webapp(example-context) to the server (cleaned before and then publish).
at this point i inspected my unzipped war file inside tomcat webapproot (tried meta and tomcat location (both same failure)) , and was completly confused that my my-module-1.0.jar file only contains only META-INF/maven (pom.xml + pom.properties) and my java classes so that it doesen't match the size as expected (due to missing resource files).
Is there any trick or option to configure eclipse to build the artifact with resources as instructed by my poms or any other vaiable soloution ?
If i use intellij, it worked out of the box but i prefer eclipse for my project
Tried different versions of eclipse , tomcat , maven and plugin versions (latest) but nothing seems to work.
Sometimes if i change my resource or java files of my-mopdule, eclipse makes a redeploy and then the artifact contains the correct my-module-1.0.jar with resources included but after another mvn clean install its gone and i need to modify any files again up to 10 times if it happens again.

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>

Duplicate WEB-INF folder created with maven build with package goal from Eclipse Kepler

I have created a maven application with JSF, Spring and JSF facets. The goals clean (deleting the target directory) and compile are working fine. But when I am running Maven with package option, I see that in the generated xxx-SNAPSHOT.war file the webapp and WEB-INF directory are copied recursively.
The Maven version is 3.1.0.
Please see the below pom.xml
<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>com.organization.elr</groupId>
<artifactId>employeerepository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Employee Location Repository</name>
<properties>
<version.spring>3.1.2.RELEASE</version.spring>
<version.aopalliance>1.0</version.aopalliance>
<version.standard.taglibs>1.1.2</version.standard.taglibs>
<version.commons.logging>1.1.1</version.commons.logging>
</properties>
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<outputDirectory>${basedir}/src/main/WEB-INF/classes</outputDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<testOutputDirectory>${basedir}/src/main/WEB-INF/classes/test</testOutputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>${basedir}/src/main/webapp</webappDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Any ideas how I can avoid this?
Thanks in advance,
Mouli.
This makes no sense
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<outputDirectory>${basedir}/src/main/WEB-INF/classes</outputDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<testOutputDirectory>${basedir}/src/main/WEB-INF/classes/test</testOutputDirectory>
Remove those lines. You're building application to source dir.
This have no sense as well
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
Why do you have .java files in resources in the first place?

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>

Filtering Maven files into WEB-INF

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>

Categories