mavenantmaven-antrun-plugin

maven-antrun-plugin copy files from one unknown subdirectory name


I am trying to use the maven-antrun-plugin to copy generated .java files to my current source directory. My problem is that one part of the path directory I want to copy is generated.

The path I want to copy look like this :

${project.build.directory}/working/<GENERATED_DIRECTORY_I_DONT_KNOW_THE_NAME>/${project.artifactId}-${project.version}/ejbModule

and from this point I want to copy all .java files with its subdirectories (to keep the tree of the packages.

See my above plugin configuration :

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>ejbdeploy-copy</id>
            <phase>verify</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Moving stubs file to source project.</echo>
                    <copy todir="${project.build.sourceDirectory}">
                        <fileset dir="${project.build.directory}/working/**/${project.artifactId}-${project.version}/ejbModule">
                            <include name="**/*.java"/>
                        </fileset>
                    </copy>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

The thing is that the maven-antrun-plugin cannot convert my ** to the generated directory name.

I tried with this kind of configuration but it didn't work :

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>ejbdeploy-copy</id>
            <phase>verify</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>Moving stubs file to source project.</echo>
                    <copy todir="${project.build.sourceDirectory}">
                        <fileset dir="${project.build.directory}">
                            <include name="**/*.java"/>
                            <exclude name="**/ejbModule/"/>
                        </fileset>
                    </copy>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

Any ideas ?


Solution

  • You are going at it the wrong way. You definitely do not want to copy source files into your main source directory. Generated files should never end up in src/main/java. This is because generated code must not be version-controled; those are files that are re-generated during the build, and, thus, should always be located inside the build directory of Maven, which is target by default.

    To add those files as sources, instead of copying them into the main source directory, you can use the build-helper-maven-plugin:add-source goal. This will add a specified folder as a source directory. In this case, you can use it and refer to the generated folder containing your sources.

    The trick is that you do not know the path to the folder. To tackle this, you can make use of the iterator-maven-plugin. With this plugin, you can iterate over all subdirectories of a given directory; the current directory can be obtained with the variable @item@. This way, the configuration is dynamic with regard to the name of the directory.

    <plugin>
      <groupId>com.soebes.maven.plugins</groupId>
      <artifactId>iterator-maven-plugin</artifactId>
      <version>0.4</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>iterator</goal>
          </goals>
          <configuration>
            <folder>${project.build.directory}/working</folder>
            <pluginExecutors>
              <pluginExecutor>
                <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>build-helper-maven-plugin</artifactId>
                  <version>1.12</version>
                </plugin>
                <goal>add-source</goal>
                <configuration>
                  <sources>
                    <source>${project.build.directory}/working/@item@/${project.artifactId}-${project.version}/ejbModule</source>
                  </sources>
                </configuration>
              </pluginExecutor>
            </pluginExecutors>
          </configuration>
        </execution>
      </executions>
    </plugin>