Search code examples
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

  • 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 ) and then creating the same package structure under the 'java-templates' as the original file in src structure did have.

    Ended up with 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 @khmarbaise answer, I actually did refactore the source structure to be more like Maven standard - moved agent/src & loader/src to src/main/java and by this way I was able to use 'templating-maven-plugin' with default values just the way @khmarbaise suggested. Also removed 'build-helper-maven-plugin' and used 'templatin-maven-plugin' in POM by:

            <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