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
Related
I am creating a jar out of my codebase with selectes packages. Also i need the xml files to be moved to META-INF folder. But i dont see that happening though my jar gets created.
<build>
<resources>
<resource>
<directory>${project.basedir}/src</directory>
<includes>
<include>jboss-ejb3.xml</include>
<include>ejb-jar.xml</include>
</includes>
<targetPath>${project.build.outputDirectory}/META-INF</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<includes>
<include>com/**/server/*EJB.class</include>
<include>com/**/common/*Remote.class</include>
</includes>
<!-- <archive> -->
<!-- <addMavenDescriptor>false</addMavenDescriptor> -->
<!-- </archive> -->
</configuration>
</plugin>
</plugins>
When i use Maven-resource-plugin i could see the META-INF folder generated in target folder. But not seeing the same inside jar.
This was solved by using <include>META-INF/**</include> as suggested by user M.Deinum
I've got an issue with a maven project where I am distributing dlls from the src/main/resources/lib folder.
The project is built as a single jar with dependencies using the maven-assembly-plugin.
Unfortunately, the maven process is corrupting my dll libraries during the copy process so that they are no longer useful to the application.
I've had a look at such concepts as resource filtering.
Here's my relevant pom.xml
Does anyone have any ideas?
I think I need to do something like this but so far it's not working for me.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
<archive>
<manifest>
<mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
My final solution (based on the answers below):
Thank you for the great answers. I ended up going with the answer that doesn't require extending the configuration of the maven-resources-plugin. I placed my binary files in the src/main/resources/unfiltered-resources folder as I needed to filter my other resources.
Here is a link to the source code.
Below is my final working pom at the time of writing.
<build>
<finalName>BehaviourCoder_${git.build.time}_${git.commit.id.describe-short}</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/src/main/unfiltered-resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<injectAllReactorProjects>true</injectAllReactorProjects>
<dateFormat>yyyy-MM-dd_HHmmss</dateFormat>
<dateFormatTimeZone>UTC</dateFormatTimeZone>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
This part:
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
Should be under under the <build/> section like this:
<project>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
...
</plugins>
</build>
</project>
When assembly plugin kicks in it is already too late, as the resources were already copied by maven resources plugin. You should exclude filtering on earlier phase (when the resources are being copied to target folder by maven resource plugin).
See maven's docs how to do this: https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html
For your case this can be something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>dll</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
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>
I have a
src/main/resources/log4j.xml
file which I would like to be included in the jar when I perform a mvn assembly:assembly but excluded when I perform a mvn package. How could I do this?
I have read several maven docs and questions here but I've been unable so far to do this. I tried many approaches, all of them failed.
Basically, I have in the <build><plugins> section of my pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
and then in the <build> section
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/log4j.xml</exclude>
</excludes>
</resource>
</resources>
but I get log4j.xml included everywhere.
Anything you place in the <exclude> or <include> elements is treated as relative to the <directory> specified. So, they way you have it currently, Maven will try to exclude resource ${basedir}/src/main/resources/src/main/resources/log4j.xml which probably does not exist. Try setting the resources element like this:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/log4j.xml</exclude>
</excludes>
</resource>
As a side note, the assembly:assembly goal is deprecated. Per the maven-assembly-plugin docs, use the single goal instead.
org.apache.maven.plugins works fine, simple control over including or excluding files in package phase!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>**/password.properties</exclude>
</excludes>
</configuration>
</plugin>
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>