javamavenpom.xmlmaven-plugin

How to handle build path sources when using 'templating-maven-plugin'


I'm migrating from Ivy+Ant to Maven and I'm quite new to Maven so bear with me...

I need to 'filter' a single source file (.java) containing a placeholder for @@replaceme@@, then compiling the result and JAR the whole thing. I have managed to do the filtering for source file using org.codehaus.mojo:templating-maven-plugin. The plugin creates generated-sources into ${project.build.directory}/generated-sources/java-templates with the java-file content replaced as the way it should, then the compilation is correctly done and everything is working just the way it should.

BUT, in IDE (Eclipse) the build path source points now to ${project.build.directory}/generated-sources/java-templates = target/generated-sources/java-templates. I don't want that to be my primary build path source because now it contains replaced data, and would need the this to point directly to original source 'agent/src'. If I add the original source 'agent/src' into POM (and that way visible in IDE), then the compile will fail due to duplicate class errors.

The relevant parts of POM:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>filter-src</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                        <configuration>
                            <delimiters>@@</delimiters>
                            <overwrite>true</overwrite>
                            <sourceDirectory>agent/src</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-sources/java-templates</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>add-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>loader/src</source>
                                <!--<source>agent/src</source> duplicate class -errors with this-->
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-sources</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>test/src</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

And this is how Eclipse is showing me source code now that it is 'missing' the original source code 'agent/src':

Eclipse

What am I doing wrong or is it just meant to be this way this plugin? I'm feeling in my guts that there is a really easy solution for my problem...


Solution

  • I managed to resolve this. The key was to separate the 'java-templates' from src-folder (I guess the folder name won't matter as it can be set by <sourceDirectory>) and then creating the same package structure under the java-templates as the original file in src structure did have. I ended up with this POM:

                <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>filter-src</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                        <configuration>
                            <delimiters>@@</delimiters>
                            <overwrite>true</overwrite>
                            <sourceDirectory>agent/java-templates</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-sources/java-templates</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>add-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>loader/src</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-sources</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>test/src</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    And currently project looking like this:

    enter image description here

    ------------------------- EDIT --------------------------------

    Inspired by [another answer][1], I actually did refactor the source structure to be more like Maven standard; I moved agent/src and loader/src to src/main/java and thereby was able to use templating-maven-plugin with default values as suggested. lso removed build-helper-maven-plugin and used templating-maven-plugin in the POM by:

    [1]: https://stackoverflow.com/a/77605892/1281433

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    By then my project looked like this:

    enter image description here