Ignoring binary files in Maven webapp resource filtering - maven

Using Maven I need a property replacement of ${project.version} in an HTML file, so I add this to my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
But now Maven corrupts some of my font-related files, e.g. MaterialIcons-Regular.woff.
Rather than excluding a path glob (which I assume would mean I would need to re-include it in a separate section without filtering), is there any way just to mark some file extensions as binary as you can with the maven-resources-plugin and <nonFilteredFileExtension>?

The solution is just as it is for maven-resources-pluging:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
</webResource>
</webResources>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>woff2</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
(I should have RTM for maven-war-plugin war:war.)

Related

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>

Filtering resources with different encodings

I have a maven project. In /src/main/resources/ I have two files foo.properties and bar.xml. The first is encoded in ISO-8859-1 and the second one in UTF-8.
Is there an easy way to get filtering for both files without breaking encoding?
As far as I see, I cannot specify an encoding in a <resource> block. It seems only possible to specify an encoding for the plugin itself, but this would lead in both files being filtered using the same encoding.
Ideally I would like to specify something like:
*.properties -> filter using ISO-8859-1
*.xml -> filter using UTF-8
*.pdf -> do not filter
anything else -> filter using default encoding
This doesn't feel like a very unusual situation, is there an easy way to configure this?
First i would suggest to put those resources which are iso based into a different directory like src/main/iso-resources and use something like this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<name>Resource Test</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-iso-part</id>
<goals><goal>copy-resources</goal></goals>
<phase>process-resources</phase>
<configuration>
<encoding>ISO-8859-1</encoding>
<resources>
<resource>
<directory>src/main/iso-resources/</directory>
</resource>
</resources>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you don't want to keep your resources in separate directories you can also keep them in the default directory src/main/resources and play with exclusions.
The idea is:
file extensions that should not be filtered should be added to the default configuration using nonFilteredFileExtensions
let the default execution of the resources plugin process resources with the default encoding
exclude files that do no use this encoding from the build/resources configuration
for each additional encoding you want to handle, add an execution of goal "copy-resources" with explicit includes that selects only the targeted files
This example only handles .properties but you can easily generalize:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
<executions>
<execution>
<id>properties</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>ISO-8859-1</encoding>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application*.properties</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

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

maven-ejb-plugin: include generated sources

I have an EJB-maven-Project that has some generated classes (generated by JAXB).
They are generated into: target/generated-sources/jaxb/
Now, with maven-ejb-plugin I want them (i.e. their compilated classes) to be included into the client-jar, something like that:
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- Tell Maven we are using EJB 3.1 -->
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
<clientIncludes>
<clientInclude>com/bla/ch/authorization/client/**</clientInclude>
<clientInclude>target/generated-sources/jaxb/**</clientInclude>
</clientIncludes>
</configuration>
</plugin>
This does not work, the generated classes are not part of the ejb-client-jar. (Though they are in the ejb-jar).
How can I do this correctly?
Include sources in your jar is probably not a good solution.
You may add generated sources in you resources locations, and then use source-plugin, to generate the so called artifact-sources.jar
<resources>
<resource>
<directory>${basedir}/target/generated-sources</directory>
<filtering>true</filtering>
</resource>
</resources>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is a better way than producing a jar with source code.

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources