Unable to do resources filtering u - maven-resources-plugin

I was trying to use resource filtering to substiute some properties into a file but not successful. It is for a Springboot Application, in which I have the following in the pom.xml
<build>
<resources>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<propertiesEncoding>default</propertiesEncoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
In src/main/resources-filtered, I have a file, test.xml, with the following contents
<test>
<version>${project.version}</version>
</test>
After running mvn clean install, I am still seeing the file contains the in target/classes folder
I am actually wanting to use assembly plugin later on to create a zip containing those filtered files. So I will expect the output file should have been filtered

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>

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

Resources