How exclude files/folder from Maven outputDirectory - maven

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>

Related

maven override/replace file into WEB-INF/classes

I want to override/replace my spring config xml file with specified location file while package as war.
And I do not want to use filter plugin (filter plugin must to use dolloar placeholder, it will run with error while local deploy), is there any plugin or setting I can use to do this?
project structure as follow:
ROOT
----config
----prd
----spring-servlet.xml
----web.xml
----src
----main
----java
----resources
----spring-servlet.xml
----webapp
....
My pom.xml is like as follow:
...
<profiles>
<profile>
<id>prd</id>
<properties>
<filterDir>prd</filterDir>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webXml>${basedir}/config/${filterDir}/web.xml</webXml>
<webResources>
<resource>
<directory>config/${filterDir}</directory>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>mvc-dispatcher-servlet.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
run maven use :mvn clean package -P prd.
Will work fine if not set tartgetPath, file copied to web root.
You can use copy-resources of maven to do this.
Example maven code should be like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>your-target-directory</outputDirectory>
<resources>
<resource>
<directory>source-directory</directory>
<!--You can also mention files too-->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Read more about copy-resources for correct implementation.

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>

Excluding folders/files from specific folder in pom.xml [duplicate]

Trying to exlcude a folder src/main/resources/scripts/ from my build but the following does not work:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<excludes>
<exclude>src/main/resources/scripts/</exclude>
</excludes>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
Any ideas?
Instead try:
<exclude>scripts/**</exclude>
The exclude is based on directory, so your construction would exclude
src/main/resources/src/main/resources/scripts
I had a similar problem and found the following issues:
You may have a parent pom which already defines a configuration for the maven-compiler-plugin. For this, add combine.self="override" to the configuration tag. See Maven : Is it possible to override the configuration of a plugin already defined for a profile in a parent POM
It seems the plugin ignores excludes if it needs the excluded classes for the compilation: make sure you are not referencing the excluded classes from other classes which will get compiled. For example, if you exclude Foo.java, but in Bar.java you import Foo; it will (try to) compile Foo.java to compile Bar.java.
For example:
<profiles>
<profile>
<id>myId</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration combine.self="override">
<excludes>
<exclude>**/some/full/directory/*</exclude>
<exclude>**/some/single/File.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>
<profile>
<id>readBuild</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration >
<excludes>
<exclude>**/com/pyramid/controllers/EntitlementWriteController.java</exclude>
<exclude>**/com/pyramid/controllers/ProductWriteController.java</exclude>
</excludes>
<testExcludes>
<testExclude>**/com/pyramid/controllers/EntitlementWriteControllerTest.java</testExclude>
<testExclude>**/com/pyramid/controllers/ProductWriteControllerTest.java</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
<directory>yourDirectory</directory>
</build>
</profile>
It's so very simple and you not need add other plugin:
https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
</resources>
</build>

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.

How to exclude context.xml from war in maven

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp</directory>
<!-- there's no default value for this -->
<excludes>
<exclude>META-INF/context.xml</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
I have File src/main/webapp/META-INF/context.xml
I want to exclude it from war file. I refered to official documentation and add plugin to pom. But it appears under demo.war/META-INF/context.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceExcludes>META-INF/context.xml</warSourceExcludes>
</configuration>
</plugin>
this works

Resources