Maven exclude resources not working - java

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>

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

Facing issue while executing OSGi bundle it throws “/hibernate.cfg.xml not found”

Facing issue “/hibernate.cfg.xml not found” while executing OSGi bundle.
I have created a OSGi bundle through Apache felix bundle plugin, in my bundle I am connecting database through hibernate. When I execute my bundle in OSGi container it throws error. I have added all hibernate related xml files (like hibernate.cfg.xml) through resources tag in POM.xml file in the root folder of the bundle.
Error is
SessionFactory creation failed:org.hibernate.HibernateException: /hibernate.cfg.xml not found
This may be minor issue but I could not figure it out, please help me to resolve the issue
This is my POM.xml file
……… …….................
..................................................
<build>
<sourceDirectory>D:\WSNew\HCB-VCDPlugin100\src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.5</version>
<extensions>true</extensions>
<configuration>
<excludeDependencies>*;scope=provided|runtime</excludeDependencies>
<instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Directory>OSGI-INF/lib</Embed-Directory>
<Embed-Transitive>true</Embed-Transitive>
<!-- Export-Package>${project.exports}</Export-Package> -->
<!--<_exportcontents>${project.exports}</_exportcontents> -->
<Import-Package>org.osgi.framework, ,org.apache.log4j,org.hibernate,org.hibernate.cfg,org.hibernate.transform,org.quartz,org.quartz.impl,javax.xml.transform,org.w3c.dom,org.xml.sax,javax.xml.parsers,!*</Import-Package>
<Bundle-Activator>com.abclcorp.hcb.vddservice.impl.Activator</Bundle-Activator>
<!-- <Private-Package>org.foo.myproject.*</Private-Package> -->
<_exportcontents>*</_exportcontents>
</instructions>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>resources/configuration</directory>
<includes>
<include>CronScheduler.xml</include>
</includes>
</resource>
<resource>
<directory>resources/log4j</directory>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
<resource>
<directory>resources/logs</directory>
<includes>
<include>CronScheduler.log</include>
</includes>
</resource>
<resource>
<directory>src</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>

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