Maven build is not filtering properties in Intellij
Asked Answered
B

5

9

I am having an issue where when I run by Maven build from Intellij 15.0.2 the Maven Resources Plugin is not filtering my properties into my files. It does work when I run mvn compile from the Windows command line. My plugin config is:

<properties>
    <prop1>aaa</prop1>
    <prop2>bbb</prop2>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>file1</include>
                <include>file2</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</configuration>
<executions>
    <execution>
        <phase>compile</phase>
        <goals>
            <goal>resources</goal>
        </goals>
    </execution>
</executions>
</plugin>
Betts answered 17/12, 2015 at 20:21 Comment(3)
Hmm, this is interesting (and strange). Can you post a bit more information about your Intellij version, OS and most importantly the run configuration (screenshot?) that you are using withing iJ ? I have never encountered this problem.Bangkok
Did you tell Intellij to delegate build action to mvn?Coom
@Coom IntelliJ should normally be able to do the replacements on its own… yet for me it seems quite random: it works for some modules, and not for others… then it starts working again… I wonder if it is not related to Spring Boot’s filtering.delimiter because it’s really the only resoruce configuration we have. But I agree: if I don’t find a solution I’ll switch on the delegate build option, but it is supposed to be slower (no incremental build).Hamil
B
12

The fix

tldr: I was able to reproduce your problem and then fixed it by moving out the <resources> element from the plugin configuration to directly under <build> like so:

<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- <snip> Other plugins -->
    </plugins>
</build>

Future readers, if you were only interested in the fix, read no further. For the intrepid SO-er, gory details await below!

Why did I do that?

I did the above since that is how I had turned on resource filtering in a previous project. I did not need to change the default phase (process-resources) and therefore did not need to explicitly specify the maven-resources-plugin at all. However, I was curious to find out why OP's configuration did not work and therefore looked at the examples for the resources mojo in maven-resources-plugin documentation which seemed to have the <resources> specified directly under <build>.

The wording in the Usage documentation seems to imply that the <resources> configuration is needed under plugin configuration only for the copy-resources mojo:

enter image description here

Update

Should have started with the maven-resources-plugin introduction which clearly states:

resources:resources copies the resources for the main source code to the main output directory.

This goal usually executes automatically, because it is bound by default to the process-resources life-cycle phase. It always uses the project.build.resources element to specify the resources, and by default uses the project.build.outputDirectory to specify the copy destination.



Intellij's weirdness?

I am tempted to suggest that Intellij is/was not at fault.

With Intellij 15.0.2, the filtering behaviour (i.e. whether it works or not) was identical when executing mvn clean compile from Intellij or from command line. I would've thought that the problem was in the plugin/pom configuration and not Intellij itself, unless there is a bug in Intellij's maven integration. For what's it worth, I have not yet encountered this problem when using maven from within Intellij (been using it for a while now starting from version 12.x).

Is your Intellij using a bundled mvn that is different from the mvn being used by the command line? i.e. is the maven same when seen here and from command line? That is the only thing I can think of, besides a bug in Intellij's maven integration (unlikely) that might account for the different behaviours that you are seeing.

enter image description here

Bangkok answered 18/12, 2015 at 11:35 Comment(0)
G
3

Thi was my solution.

Go to Run>Edit Configurations.

In the Server tab> Before launch.

Delete the artifact and add this maven goal: clean compile

enter image description here

Gid answered 19/4, 2018 at 18:26 Comment(1)
'clean compile' could be enoughAlgoid
C
1

For me the issue was that I forgot configure Intellij to delegate the build to mvn.

delegating to mvn

More detail is available in the documentation here: https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#delegate_to_maven.

I suspect Intellij only copies src/main/resources to target/classes and nothing else.

Coom answered 5/1, 2022 at 19:54 Comment(0)
E
0

Try adding ${pom.basedir} to the beginning to the <directory> tag:

from

<build>
    (...)
    <testResources>
        <testResource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
    </testResource>
    (...)
</build>

to

<build>
    (...)
    <testResources>
        <testResource>
            <filtering>true</filtering>
            <directory>${pom.basedir}/src/test/resources</directory>
        </testResource>
    </testResources>
    (...)
</build>

I suspect that this is necessary for Intellij to find the correct resource files to perform the replacement of pom.xml properties when the Maven project has more than one module.

Eucaine answered 6/10, 2020 at 17:24 Comment(0)
L
0
Go to Run > Edit Configurations.
Modify Options> On "Update" Action> Update resources.

enter image description here

Lyndsaylyndsey answered 19/12, 2023 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.