Reputation: 886
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 ?
Upvotes: 3
Views: 5512
Reputation: 8813
Immediate solution: Set the generated directory name as a property:
<fileset dir="${project.build.directory}/working/${generated.dir}/${project.artifactId}-${project.version}/ejbModule">
<include name="**/*.java"/>
</fileset>
The variable might be declared either in the properties
section of the pom, either in some profile the local settings.xml
file, or either be received directly from command line:
mvn -Dgenerated.dir=c:\generated clean install
If the generated directory is just one, you can get its name by a simple program in Ant. I recommend you to take all the ant stuff to a build.xml
file:
<project basedir=".">
<dirset id="dirs" dir="${project.build.directory}/working" includes="*" />
<pathconvert pathsep="${line.separator}" property="generated.dir" refid="dirs">
<chainedmapper>
<mapper type="flatten" />
<regexpmapper from="(.*)" to="\1" />
</chainedmapper>
</pathconvert>
<echo>Moving stubs file from ${generated.dir} to source project.</echo>
<copy todir="${project.build.sourceDirectory}">
<fileset dir="${project.build.directory}/working/${generated.dir}/${project.artifactId}-${project.version}/ejbModule">
<include name="**/*.java" />
</fileset>
</copy>
</project>
Then, it's just call it from the pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>ejbdeploy-copy</id>
<phase>verify</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant antfile="build.xml">
</ant>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Important: Respect version 1.8 of maven-antrun-plugin.
Upvotes: 0
Reputation: 137279
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>
Upvotes: 2