我试图使用maven-antrun-plugin将生成的.java文件复制到当前源目录中。我的问题是生成了我想要复制的路径目录的一部分。
我想复制的路径如下所示:
${project.build.directory}/working/<GENERATED_DIRECTORY_I_DONT_KNOW_THE_NAME>/${project.artifactId}-${project.version}/ejbModule
从这一点开始,我希望复制所有.java文件及其子目录(以保留包的树)。
请参阅我的上述插件配置:
<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>问题是maven-antrun-plugin不能将我的**转换为生成的目录名。
我尝试过这种配置,但没有成功:
<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>有什么想法吗?
发布于 2016-09-12 11:23:23
你走错路了。您肯定不希望将源文件复制到主源目录中。生成的文件永远不会在src/main/java中结束。这是因为生成的代码不能被版本控制;这些文件是在构建过程中重新生成的,因此,应该始终位于Maven的构建目录中,默认情况下这是target。
要将这些文件作为源添加,可以使用build-helper-maven-plugin:add-source目标,而不是将它们复制到主源目录中。这将添加指定的文件夹作为源目录。在这种情况下,您可以使用它并引用包含源的生成文件夹。
诀窍是您不知道文件夹的路径。要解决这个问题,您可以使用iterator-maven-plugin。使用这个插件,您可以遍历给定目录的所有子目录;可以使用变量@item@获得当前目录。这样,相对于目录的名称,配置是动态的。
<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>发布于 2016-09-12 10:55:59
立即解决方案:将生成的目录名设置为属性
<fileset dir="${project.build.directory}/working/${generated.dir}/${project.artifactId}-${project.version}/ejbModule">
<include name="**/*.java"/>
</fileset>变量可以在pom的properties部分中声明,也可以在某个概要文件(本地settings.xml文件)中声明,或者直接从命令行接收:
mvn -Dgenerated.dir=c:\generated clean install更新
如果生成的目录只有一个,您可以通过Ant中的一个简单程序获得它的名称。我建议您将所有的ant内容放到一个build.xml文件中:
<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>然后,它就从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>重要:尊重版本1.8的maven-antrun-插件.
https://stackoverflow.com/questions/39448399
复制相似问题