Table of Contents

Managing resources in Maven projects


Prerequisites

Maven project is opened in Visual Studio. You can find instructions how to create a Maven project in this section. You can also find an overview of Maven projects in this section.

Actions

Introduction

Resources are various types of files which are copied to the resulting build artifact (e.g. JAR archive) along with compiled Java classes. These might be XML files or other data files.

Maven supports resources through resource directories. You can configure any number of directories containing files to be copied to the resulting archive. By default there is a single directory named just resources. Internal structure of a resource directory is preserved when copying to the archive. If you specify more than one directory, all of them will be copied into destination location.

Optionally, resource files may be filtered, which means Maven will process them and expand any properties found in these files. This option has to be turn on manually per resource directory. By default Maven does not modify any resource files.

You can find more info on resources in this section of Maven docs as well as in this section.

Viewing and editing contents of resource directories

There are two kinds of nodes related to resource directories visible in Solution Explorer. The first kind is the contents node in which you can view and edit actual files and directories. The second kind is the configuration node (under Configuration subtree) in which you can edit properties of the resource directories configured in Maven project. Configuration nodes determine what is shown under contents nodes.

There are two resource contents nodes: regular resources and test resources. The latter ones are used only during testing.

You can use standard Add / New item, Remove and Rename options to manage the files. Double clicking on a file name opens it for editing.

Configuring resource directories

There are also two corresponding configuration nodes, as well as third node used to configure filtering.

If you do not specify any resource directories here, Maven will use default configuration in which standard resource directories exist at relative paths: src/main/resources and src/test/resources. However, if you add a directory manually, the list in the project will override default configuration entirely and corresponding default resource directory will be disabled. It will disappear from the contents view. If your intention is to keep it, you must explicitly add the directory into configuration node.

In order to add a resource directory, right-click on appropriate configuration node (Resources or Test resources) and choose Add / New item option. A property editor window will appear:

The most important parameter here is Directory. It specifies the relative path of resource directory inside your project. Target is optional and can be used to alter destination location. By default, the directory structure is copied "as is" to the final archive, but if Target, it will be copied to that subdirectory instead. Finally, Filtering turns on filtering (property expansion) feature described below.

You can also edit these properties later, using standard Properties option, or Remove resource directory from the configuration (it will not be removed from the disk).

Adding resource directories is also available on the contents nodes (Add / New item) and it works exactly the same as above. This is for convenience only.

Managing exclusion and inclusion wildcards

If you want to have additional control over what is copied to the destination from resource directories, you can use Excludes and Includes nodes located under the resource node. Those elements host wildcard entries which are matched against file names. You can use * or ** wildcard in these patterns. Single asterisk works as usual (any file or directory name) while double asterisk means "any number of arbitrary directories". Thus, **/*.properties means "any file with .properties extension, located anywhere".

The Includes list overrides default behavior to include everything and allows you to specify constraints on file paths to be included. The Excludes list is applied after Includes list, and blocks matching file paths from inclusion. If you want only to exclude some files, leave the inclusion list empty (by default it includes everything), so that only the exclusion list is in effect.

Controlling property expansion in resource files

If your resource files are text files and contain property references (in ${property.name} format), Maven can automatically expand them for you when it copies resources to the destination. Values for the expansion are taken from Properties node in your project, as well as property definition files configured under the Resource filters node.

The expansion is enabled or disabled per resource directory. To enable it, set the Filtering parameter to true.

The Properties node is located under the Configuration node and can be used to specify properties directly in the project file.

Additionally, you can order Maven to read one or more filter files, with property values in standard format. Add them to the Resource filters node. Paths specified there are relative to the project root.

Table of Contents


Copyright (C) 2014-2016 SOFT-ERG. All rights reserved.