Is there any property in Maven to access the directory value of "resources"?

8
  

Based on the question link

I know I can use ${project.build.sourceDirectory} to access my source files directory. If I want to access the resource files directory, the resources , how do I? ${project.build.resources.resource.directory} did not work.

    
asked by anonymous 26.06.2018 / 11:25

1 answer

3
${project.build.resources[N].directory}

N = Index of your resource (ex: 0,1,2 and etc).

According to documentation , the default resource directory (aka resources) is located in:

src/main/resources

What would give us the possibility of access through the expression:

${basedir}/src/main/resources

However , you can specify a directory other than the default directory for features:

<project>
    ...
    <build>
    ...
        <resources>
            <resource>
                <directory>[seu-diretório]</directory>
            </resource>
        </resources>
    ...
    </build>
    ...
</project>

In addition, you can still specify multiple resource directories:

<project>
    ...
    <build>
    ...
        <resources>
            <resource>
                <directory>src/main/recursos-1</directory>
            </resource>
            <resource>
                <directory>src/main/recursos-2</directory>
            </resource>
            ...
        </resources>
    ...
    </build>
    ...
</project>

Therefore, the expression for a resource directory is:

${project.build.resources[N].directory}

Where N is the index of your resource. Example:

${project.build.resources[0].directory}
${project.build.resources[1].directory}
...

This may raise another question: How do I know which directory will be associated with index 0, 1, 2, and so on?

There is the help: evaluate plugin that helps you test expressions.

Example:

<project>
    ...
    <build>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-help-plugin</artifactId>
        </plugin>
    ...
        <resources>
            <resource>
                <directory>src/main/recursos-1</directory>
            </resource>
            <resource>
                <directory>src/main/recursos-2</directory>
            </resource>
            ...
        </resources>
    ...
    </build>
    ...
</project>

Testing (bash):

mvn help:evaluate -Dexpression=project.build.resources[0].directory | grep -v "\["
mvn help:evaluate -Dexpression=project.build.resources[1].directory | grep -v "\["

Although the plugin documentation specifies the -q (quiet) and -DforceStdout options, I was not successful in displaying only the result of the expression $ {project.build. resources [0] .directory}, so I used grep -v to filter the output.

    
25.07.2018 / 04:24