Maven: Use the filtering mechanism for text files not under 'resources'? - maven

I need to customize a number of XML files which are not under resources (in particular, they are under an EAR's project src/main/application).
The filtering mechanism would be perfect for this, but my understanding (correct?) is that it works for resources only.
Is there a way to use filtering for files in other directories than src/main/resources?
Thanks in advance.

The Maven EAR Plugin can filter the content of src/main/application. As documented in Filtering EAR Resources:
Filtering the sources
Filtering the content of the
src/main/application directory or the
one defined by the
earSourcesDirectory parameter is as
easy as:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<filtering>true</filtering>
[...]
</configuration>
</plugin>
</plugins>
</build>
Note that the standard properties are
available for filtering. It is also
possible to specify a set of property
files to add extra values if
necessary. The configuration below
uses also the properties defined in
src/main/filters/config.properties
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<filtering>true</filtering>
<filters>
<filter>src/main/filters/config.properties</filter>
</filters>
[...]
</configuration>
</plugin>
</plugins>
</build>

Related

How can I exclude certain overlay-derived directories from my war using maven?

We build a webapp using maven 3.5.0 and the maven-war-plugin. We have many overlays, many of which have a tests directory that contains JSP files used for testing certain things in a dev environment. We don't want these included in our war.
We tried the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<overlays>...</overlays>
<warSourceExcludes>tests</warSourceExcludes>
</configuration>
</plugin>
This didn't work, nor did a couple of configuration variations:
<packagingExcludes>tests</packagingExcludes>
and
<packagingExcludes>**/${project.artifactId}/tests/**</packagingExcludes>
Is this due to my naive misuse of configuration options, or does it have to do with how overlays are processed?
You should use <excludes>..</excludes> (see https://maven.apache.org/plugins/maven-war-plugin/overlays.html) instead of <packagingExcludes>..</packagingExclude>...
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<overlays>
<overlay>
<groupId>com.example.projects</groupId>
<artifactId>documentedprojectdependency</artifactId>
<excludes>
<exclude>WEB-INF/classes/images/sampleimage-dependency.jpg</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
...
The dependentWarExcludes element worked to eliminate the tests directories and their contents:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<overlays>...</overlays>
<dependentWarExcludes>tests/**</dependentWarExcludes>
</configuration>
</plugin>

Ignoring binary files in Maven webapp resource filtering

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

Override maven-assembly-plugin output file name

Is it possible to override the default name of the jar file created running the assembly:single goal? I want to add a jar with dependencies that has a non-standard name. I am using version 2.6 of the plugin.
Yes, you need to use the finalName configuration attribute. Note that you probaby also want to remove the assembly id that is appended by default to the final name, with the help of the appendAssemblyId attribute.
Sample configuration:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
...
<finalName>myFinalName</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
The proper way to do this (as mentioned on Maven JIRA tracker) is to set the build.finalName and not to use the readonly finalName plugin property. Here is an example how to get rid of both the "jar-with-dependencies" and the version, which is very useful for creating a stable name and eliminating potentially confusing existence of 2 JARs for use in README instructions:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
...
<configuration>
<appendAssemblyId>false</appendAssemblyId>
...
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Getting Maven Build to include empty directories

I am trying to get maven to include an empty directory which is just used for output files created by the webapp (which is why it is empty).
I have googled and the maven-resources-plugin seemed to be the best option for this, the documentation states that the property includeEmptyDirs is since 2.3 and I am using 2.4, however this seems to do nothing and so far the only way Ive managed to get the directory to create is by putting a text file into it, I dont really want to do this if Maven should be able to do this for me.
Can someone tell me what I am doing wrong? Below is my build section from the pom file:
<build>
<finalName>MyApp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
</plugin>
</plugins>
</build>
Take a look at the maven-war-plugin which has a configuration item includeEmptyDirectories.

maven resource plugin or packageExclude in war plugin

I have a (exploded) war file generated by maven. I want to exclude some directories from the packaging. So I tried it with the two following ways:
Way:
<resources>
<resource>
<directory>${basedir}/src/main/webapp/resources</directory>
<excludes>
<exclude>css/*.css</exclude>
</excludes>
</resource>
</resources>
This does not work as expected. The plugin will copy all other files but the excluded above from my resource-directory (src/main/webapp/resources/) into the war-folder src/main/resources. I do not want to copy all the other files from src/main/webapp/resources to src/main/resources. The resource-plugin should ONLY exclude the above files and should not additionally copy the other files from src/main/webapp/resources into src/main/resources. Is there a way to say that to the resource-plugin?
The second way, does also not work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<packagingExcludes>${basedir}/src/main/webapp/resources/css/*.css</packagingExcludes>
</configuration>
</plugin>
The debug output shows, that the path is correct:
[DEBUG] (s) packagingExcludes = /Users/test/src/main/webapp/resources/css/*.css
However, looking in my exploded war-archive, the "css/*.css" are NOT excluded. What is wrong?
Okay, I found the solution:
I have to use this
<configuration>
<warSourceExcludes>
...
</warSourceExcludes>
</configuration>
to exclude resources in the exploded war file.

Resources