Include/exclude files from jar with maven depending on phase - java

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>

Related

XML files not moving to META-INF folder of jar

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

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

Why is maven-war-plugin ignoring my configured filters for web resources?

I have a filter file in src/main/filters/base.properties with contents:
testProperty=testValue
My POM has resources, filtering, and additional web resources defined (using maven-war-plugin). It also overrides the default filtering delimiter.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/filters/base.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<overwrite>true</overwrite>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/more-web-resources</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
All filtering works fine for files located in src/main/resources. However, a configuration file located in src/main/more-web-resources is not being filtered properly. So if I have a file src/main/more-web-resources/test.properties, with contents of:
finalTestProperty=#testProperty#
The final src/main/webapp/test.properties file looks like:
finalTestProperty=#testProperty#
As opposed to:
finalTestProperty=testValue
So, filtering just isn't working for the additional web resources specified with the maven-war-plugin. What am I missing?
Well, after a night of sleep, I came back to it this morning, built, and the properties are being filtered properly. I made sure my POM and project structure matches my question's exactly, and it does. It's possible I was looking at the original test.properties and not the built test.properties.
To be clear, and to address the comments in the question, I did not have to define an additional resource in the <resources/> section for the more-web-resources directory, I did not have to (re-)specify the filters in another <filters/> section inside the maven-war-plugin configuration, and I did not have to (re-)specify the # delimiter inside the maven-war-plugin. It all just works as I originally expected it to, by using the resources and filters defined in the standard sections of the POM, as well as the maven-resources-plugin.

How to include filtered resources into WEB-INF/classes using maven-resources-plugin?

I'm using the maven-resources-plugin because I need to use a custom delimiter when doing my filtering. My artifact is a WAR.
I have some resources in my src/main/resources directory. Normally, when packaging a WAR, anything in this directory ends up in WEB-INF/classes. But I need those resources to be filtered first, then placed into the WEB-INF/classes directory. I'm having a hard time configuring the maven-resources-plugin to do that. Is there a way?
My POM looks like this:
<build>
<finalName>my-app</finalName>
<filters>
<filter>${basedir}/src/main/properties/base.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<useDefaultDelimiter>false</useDefaultDelimiter>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>
${basedir}/src/main/resources
</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Short answer:
The phase is wrong, validate is the second phase, should only be used for validation of the build environment and comes before the default process-resources phase, so your correct files become overriden.
Long answer:
Basically, you have two options two fix your problem:
Use a different / new execution for copying
If you really need different behaviours.
In that case, your phase is wrong. What happens is:
in the phase validate (Why there?), the resources are correctly filtered and copied
in the phase process-resources, the default-resources execution of the resources plugin runs the goal copy-resources, which overrides your filtered resources again in the unfiltered state.
If you really want to do it this way, put the resources into a different directory than src/main/resources, change the phase of of the execution to somewhat appropriate (as to not confuse) and please also change the id of the execution (to something like copy-specially-filtered-resource - the id can be used to explain why there is the need for a second execution)
Also, the output directory is wrong (would need to be: ${project.build.outputDirectory}, directory points only to the target folder.
<build>
<finalName>my-app</finalName>
<filters>
<filter>${basedir}/src/main/properties/base.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-special-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<useDefaultDelimiter>false</useDefaultDelimiter>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>
${basedir}/src/main/filtered
</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Also, in this case, I would place the filters into the execution configuration as well (everything else is misleading).
Preferred: Simply reconfigure the resources plugin
Way shorter and easier would be to simply reconfigure the resources plugin, activate filtering for the resources globally and use the default execution (however, this would also be active for test-resources):
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<finalName>my-app</finalName>
<filters>
<filter>${basedir}/src/main/properties/base.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<useDefaultDelimiter>false</useDefaultDelimiter>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
Hint: Using the override config is usually a smell...
First why not reading the documentation of maven-war-plugin
Based on the documentation you can define a configuration to use a supplemental directory and activate filtering for that directory like this:
<configuration>
<filters>
<filter>properties/config.prop</filter>
</filters>
<nonFilteredFileExtensions>
<!-- default value contains jpg,jpeg,gif,bmp,png -->
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<webResources>
<resource>
<directory>resource2</directory>
<!-- it's not a good idea to filter binary files -->
<filtering>false</filtering>
</resource>
<resource>
<directory>configurations</directory>
<!-- enable filtering -->
<filtering>true</filtering>
<excludes>
<exclude>**/properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
I would recommend to make a supplemental directory like src/main/filtered-resources which you configure to do filtering and the default resources directory I wouldn't change (no filtering by default). This makes it easier to understand.
If you want to change the delimiters you can add the configuration to the maven-war-plugin like this:
<delimiters>
<delimiter>#</delimiter>
</delimiters>
So in the end there is no need to make supplemental configuration for maven-resources-plugin etc.

Maven Exclude Files During Build

I am having problems getting Maven to build my webapp without including extraneous development file, such as unminified script and css files.
First i tried using exclude in combination with webResources
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<webResources>
<resource>
<directory>src/main/webapp/resources/</directory>
<excludes>
<exclude>annotated-js/*.js</exclude>
<exclude>compiled-js/*.js</exclude>
<exclude>css/*.css</exclude>
<exclude>less/*.less/exclude>
</excludes>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>connection.json</exclude>
<exclude>reportRocket.jsp</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The result was the contents of WEB-INF being duplicated in the project root and no excluded directories or files.
So, I looked around here and found this: maven2: excluding directory from WAR but running mvn clean package using either warSourceExcludes or packagingExcludes results in the directories i'm trying to exclude not being, well, excluded...
The build section of my pom.xml
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<packagingExcludes>
src/main/webapp/resources/annotated-js/,
src/main/webapp/resources/compiled-js/,
src/main/webapp/resources/css/,
src/main/webapp/resources/less/,
src/main/webapp/WEB-INF/connection.json
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Building the project with these settings results in the following project structure:
META-INF
resources
//desired folders
annotated-js
compiled-js
css
less
WEB-INF
// desired files
connection.json
This is my first time using a build tool, so i'm probably overlooking something simple but in the meantime, this is driving me crazy. Any suggestions or obvious problems with my xml?
First you should read the documentation of the maven-war-plugin cause the packagingExclude is intended for a different purpose, but in your case you need to do the configuration in that way:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/resources/</directory>
<!-- there's no default value for this -->
<excludes>
<exclude>annotated-js/**</exclude>
</excludes>
</resource>
</webResources>
</configuration>

Categories