Is there a property in Maven to access the "Resources" directory value?

Asked

Viewed 83 times

8

Issue-based https://stackoverflow.com/q/9216557/4438007

I know I can use ${project.build.sourceDirectory} to access my source file directory. If I want to access the resource files directory, the Resources, how do I? The ${project.build.resources.resource.directory} didn’t work out.

  • 1

    Have you looked at something from here help you?

  • 1

    @Statelessdev actually copied and pasted, but without understanding. Why is it a vector? Is there a position 1? What would it mean?

  • 2

    Actually, I googled to see if I could find something in English, I found it and put it here to help you haha. If I can solve and post an answer, I’ll learn too. ;)

  • 1

    You can use: ${basedir}/src/main/resources/. Or are you looking for a single variable, which carries all this information?

  • 1

    @Dherik I was looking for a variable that beautifully indicated the position. This is the default place, but it can be overwritten by someone with no heart, so I’d like to take this directory that overwrites the pattern.

  • 1

    @Jeffersonquesado As far as I know, for this heartless person to overwrite the default "Resources" directory he would have to specify the directory in pom.xml with the <Resources> tag. Because it is possible to specify multiple "Resources", the access form is an array ${project.build.Resources[N]. directory}. Here in the documentation explains about that.

  • @Tommelo is what I’m talking about. Thank you very much for the reference. If you can elaborate an answer, it would be of great value

Show 2 more comments

1 answer

3


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

N = Index of your resource (e.g.: 0.1.2 and so on).


According to the documentation, the default resource directory(a.k.a Resources) is located in:

src/main/resources

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

${basedir}/src/main/resources

However, it is possible to specify a different directory from the default for resources:

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

In addition, it is still possible to 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 directory expression of a resource is:

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

Where N is your resource index. 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, etc?

There is the plugin help:evaluate that helps 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 options -q(quiet) and -Dforcestdout, did not succeed to display only the result of the expression ${project.build.Resources[0]. directory}, for this reason I used grep -v to filter the output.

  • Great answer. I will leave the reward a little while open so that you have more visibility the question and, also, your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.