2.1 maven-resources-plugin copy resources plugin

If you are building a web application, then you will just have a bunch of different resources in it. These are jar libraries, jsp servlets, settings files. And of course, this is a bunch of static files like html, css, js, as well as various pictures.

By default, when building a project, Maven will simply copy all your files from the folder src/main/resourcesto the target directory. If you want to make changes to this behavior, then the plugin will help you maven-resources-plugin.

Sample code for such a plugin:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>
                   ${basedir}/target/resources
                </outputDirectory>
                <resources>
                    <resource>  instructions for copying a resource 1 </resource>
                    <resource>  instructions for copying a resource 2 </resource>
                    <resource>  instructions for copying a resource N </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

This plugin will be called during the validate phase. Using the tag, <outputDirectory>you can set the directory where the plugin should copy the resources specified in the <resources>. And this is where the plugin can unfold in all its might.

2.2 Filtering resources with the maven-resources-plugin

Plugin resources can be specified not only in the form of files, but immediately in the form of directories. Moreover, a mask can be added to the directory, which specifies which files from it will be included in this resource.

Example:


            <resource>
                <directory>src/main/resources/images</directory>
                <includes>
                     <include>**/*.png</include>
                </includes>
            </resource>

Two asterisks as a mask represent any number of directories . In the example above, all png files contained in the directory src/main/resources/images(and its subdirectories) will be taken as resource data.

If you want to exclude some files, you can use the exclude. Example:

<resource>
    <directory>src/main/resources/images</directory>
    <includes>
        <include>**/*.png</include>
    </includes>
    <excludes>
         <exclude>old/*.png</exclude>
    </excludes>
</resource>

Tags are applied sequentially: first, the include files specified in the include files will be added to the resource, and then the exclude files will be excluded from this list.

But that's not all. The plugin can look inside files (if they are text, of course). And, for example, add application.propertiesthe required version of the assembly to the file. In order for the plugin to process the contents of the file, you need to specify the parameter <filtering>true</filtering>.

Example:

<resource>
    <directory>src/main/resources/properties</directory>
    <filtering>true</filtering>
    <includes>
        <include>**/*. properties </include>
    </includes>
</resource>

More details about this plugin can be found at the link: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

2.3 maven-source-plugin source inclusion plugin

Another useful plugin - maven-source-pluginallows you to include the source code of your java files in the assembly. For what?

The thing is that in addition to web applications, a very large number of libraries are assembled using Maven. A lot of Java projects follow the concept of open-source and are distributed to the Java community with their own sources.

Why do you need a separate plugin? Why can't you just copy the source and that's it?

First, in any complex project, the sources can be stored in several places.

Secondly, the generation of sources based on xml specifications is often used, such sources also need to be included in the assembly.

And thirdly, you can decide not to include any especially secret files in your assembly.

An example of using the maven-source-plugin plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2.4 maven-dependency-plugin dependency copy plugin

You may also need to smartly copy dependencies (libraries) when building a project. A plugin is used for this maven-dependency-plugin.

Example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
        <outputDirectory>
            ${project.build.directory}/lib/
        </outputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

In this example, the default behavior of the plugin is written - copying libraries to the ${project.build.directory}/lib.

The execution section states that the plugin will be called during the build phase - package, goal - copy-dependences.

In general, this plugin has a fairly large set of goals, here are the most popular ones:

1 dependency:analyze dependency analysis (used, unused, specified, unspecified)
2 dependency:analyze-duplicate identifying duplicate dependencies
3 dependency:resolve resolution (definition) of all dependencies
4 dependency:resolve-plugin permission (definition) of all plugins
5 dependency:tree displaying the dependency tree

You can also set additional parameters in the configuration section:

1 outputDirectory Directory where dependencies will be copied
2 overWriteReleases Flag to overwrite dependencies when creating a release
3 overWriteSnapshots Flag to overwrite non-terminal dependencies that have SNAPSHOT
4 overWriteIfNewer Flag to overwrite libraries with newer versions

Example:


<configuration>
    <outputDirectory>
         ${project.build.directory}/lib/
    </outputDirectory>
    <overWriteReleases>false</overWriteReleases>
    <overWriteSnapshots>false</overWriteSnapshots>
    <overWriteIfNewer>true</overWriteIfNewer>
 </configuration>

By default <overWriteReleases>, and <overWriteSnapshots>is false, for <overWriteIfNewer>is true.