Getting Maven Build to include empty directories - maven

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.

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>

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>

maven-war-plugin without maven-compiler-plugin how it works

I've just downloaded a sample project off the internet which had this in the pom...
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</build>
There was an error so what i did was to remove maven-compiler-plugin and then I ran the pom and it worked (i.e. the war file was created).
Why is the compiler plugin required? And what is happening here now that I have removed it? What compiler will it be using?
The maven-compiler-plugin part is needed for changing default configurations. In this case, it was to set the JDK versions to 1.5.
You can manage without it if you don't have anything to compile, or are OK with the default values (I think 1.4).
I hope this helps.

maven-war-plugin always running even without child pom.xml reference...why?

I am specifying:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<outputDirectory>C:\apache-tomcat-7.0.42\webapps</outputDirectory>
</configuration>
</plugin>
.
.
.
However, even though I do not specify anything in the respective children pom.xml, the war file is still being moved to that output directory.
As far as I know, pluginManagement is only there for reference from the children pom right?
Thank you for your help
By default Maven will invoke the maven-war-plugin war:war goal as part of the package phase if you specify <packaging>war</packaging> in your pom.xml
Since you have a <pluginManagement> stanza for maven-war-plugin specifying a custom <outputDirectory> in your parent pom.xml your child pom.xml will use this property when you run the package phase.
http://maven.apache.org/plugins/maven-war-plugin/usage.html

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

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>

Resources