exclude file from maven build - maven

I've a web application with file src/main/webapp/META-INF/context.xml which contains some configuration for testing database. On production server this file is in $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml and I'm testing with embedded tomcat so I don't want to package this file. I'd like to exclude this file from maven build. I tried following:
<build>
...
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</build>
and also following:
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<resources>
<resource>
<directory>src/main/webapp/META-INF</directory>
<filtering>true</filtering>
<excludes>
<exclude>context.xml</exclude>
</excludes>
</resource>
</resources>
</configuration>
</plugin>
</build>
But the file still gets packaged in war and in build directory (eg. target/myapp-1.0-SNAPSHOT/META-INF/context.xml). What am I doing wrong?

You can try using the packagingExcludes parameter
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>META-INF/context.xml</packagingExcludes>
</configuration>
</plugin>
To exclude a resource from build, the first snippet in the question looks fine, except that the absolute path of the resource directory should be specified. For instance
<directory>${basedir}/src/main/webapp/META-INF</directory>

The others have answered the main question but one other detail I noticed from your original attempted solution -
<filtering>true</filtering>
In Maven, "resource filtering" doesn't mean what you probably think it means. It's not about including/excluding resources but rather whether they should be processed to fill in embedded variable references.
See http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Recently I had a similar problem with persistence.xml. Try putting this into your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>META-INF/context.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
If it does not help, try replace maven-jar-plugin with maven-war-plugin.

Related

application.yml is not copied when resource exclude is used

I am using the following build settings in my Spring Boot object:
<build>
<resources>
<resource>
<directory>src/main/resources/clientApp</directory>
<excludes>
<exclude>node_modules/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The whole clientApp directory, containing a node.js application is properly copied, without the unwanted node_modules directory. As an unwanted side effect, the application.properties/.yml file is not copied anymore.
How do I only get the node_modules directory from getting copied and while application.yml keeps getting copied?
You can specify an include for application.properties|yaml
<build>
<resources>
<resource>
<directory>src/main/resources/clientApp</directory>
<includes>
<include>node_modules/application.*</include>
</includes>
<excludes>
<exclude>node_modules/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Alternatively you could consider moving application.properties|yaml from src/main/resources/clientApp/node_modules up to src/main/resources/clientApp.

Unable to remove specific files from Maven build

I am currently working over Maven build. I want to remove specific file from my Maven build. I am currently trying with below code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<!-- override the destination directory for this resource -->
<targetPath>WEB-INF/classes</targetPath>
<!-- enable filtering -->
<filtering>true</filtering>
<includes>
<include>*.pem</include>
<include>*.pfx</include>
<include>META-INF/jpa-persistence.xml</include>
<include>META-INF/spring-persistence.xml</include>
<include>com/abc/config/build-config-${propertyenv}.properties</include>
</includes>
<excludes>
<exclude>%regex[(?!.*${propertyenv}/)*]</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
I have defined propertyenv as property value in my POM:
<properties>
<propertyenv>abc</propertyenv>
</properties>
I only want abc.properties file from com/abc/config folder. But after build all .properties files are present. Kindly help.
Use packagingExcludes or packagingIncludes Parameters to include or exclude files from the final war
Example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
....
<configuration>
....
<packagingExcludes>
WEB-INF/lib/pax-logging-*.jar, WEB-INF/lib/jaxb-*-2.2.7.jar
</packagingExcludes>
</configuration>
</plugin>

How exclude files/folder from Maven outputDirectory

I am developing maven project using Spring boot and JSF. According to this example, the managed bean .class files should be put into /src/webapp/WEB-INF/classes directory, to be shown on JFS views. This is achieved by adding outputDirectory tag to pom.xml:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-compiler-plugin.version}</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
But, I have noticed that except *.class files, there is also stored content of src/main/resources directory: i18n folder, templates folder, *.sql files, application.properties etc....
My question: How to exclude these unnecessary files/folders from /src/webapp/WEB-INF/classes directory?
Just add the file you dont want to the maven exludes.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/src/main/java/myClassesToExclude*.java</exclude>
</excludes>
</configuration>
</plugin>
There are different ways of excluding files in Maven. I am pointing out the general ways here.
To exclude a resource, you only need to add an element.
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>[your directory]</directory>
<excludes>
<exclude>[non-resource file #1]</exclude>
<exclude>[non-resource file #2]</exclude>
<exclude>[non-resource file #3]</exclude>
...
<exclude>[non-resource file #n]</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
To include everything except the bitmaps, jpegs, and gifs, we can simply exclude them by:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<excludes>
<exclude>**/*.bmp</exclude>
<exclude>**/*.jpg</exclude>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
You can also have both and elements. For example, if you want to include all text files that does not contain the word "test" in their filename.
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
resource maven.apache.org
If you want to exclude from war file then use [warSourceExcludes] or [packagingExcludes] tag as :
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</warSourceExcludes>
<packagingExcludes>WEB-INF/abc.xml,WEB-INF/pqr.xml</packagingExcludes>
</configuration>
</plugin>

How to exclude separate files with nonFilteredFileExtension

I have this bundle.
admin.common.upper_case=Uma ou mais letras maiúsculas
During building project this bundle becomes corrupted.
admin.common.upper_case=Uma ou mais letras mai�sculas
In order to fix issue we use this configuration for maven-resources-plugin
Namely we added nonFilteredFileExtension tag for properties extension.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>properties</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
This configuration caused issue with another property file like build.properties :
build.version=${project.version}
static.url.version=${project.build.timestamp}
We tried to use this configuration(namely added filter tag in order to filter build.properties file):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<filters>
<filter>properties/build.properties</filter>
</filters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>properties</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
Also we tried with this configuration in order to avoid filtering for current bundles:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<!-- enable filtering -->
<filtering>true</filtering>
<excludes>
<exclude>${basedir}/src/main/resources/one.properties</exclude>
<exclude>${basedir}/src/main/resources/two.properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
The question is how to allow filtering only for this property file build.properties
First of all, you should try to fix the root of your problem by Specifying a character encoding scheme:
<project ...>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
..
</project>
One way to do what you want is to specify files to include and files to excludes. From the documentation:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
The elements include and exclude should also works with specific files such as path/to/abc.properties.

Maven war packaging, include classes ignores excluded resources

I have the following pom
<project>
....
<packaging>war</packaging>
....
<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>WEB-INF/sass/**</warSourceExcludes>
<attachClasses>true</attachClasses>
<webResources>
<resource>
<directory>src/main/resources/config</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
...
</project>
As you can see, I package a WAR while my .class files are not in my WEB-INF/classes folder - they are packaged into a JAR instead.
Now, I am desperately trying to exclude some resources from my JAR - but it does not work. When I run mvn jar:jar - the resources are excluded, however when I run mvn package the resources are there.
Please help.
It seems that #user944849 is right - indeed, the war plugin does not use the jar plugin in order to achieve the JAR packaging.
However, his answer gave me a wrong result still as it will simply create 2 jars - one will be with the resources and the other without. The WAR will still use the wrong one.
The correct answer is to use the new maven resources tag.
The one that corresponds to my configuration looks as follows
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
You do not have the jar:jar goal bound to a lifecycle phase. When you run jar:jar from the command line, the exclusions happen fine; when you run mvn clean package I suspect jar:jar is not executing. Try binding the goal to a phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals><goal>jar</goal></goals>
<phase>prepare-package</phase>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
FYI, the archiveClasses feature of the war:war plugin goal does something similar to what I think you're trying to achieve without requiring a separate plugin config.

Resources