我的项目使用Rats!生成源代码!解析器生成器。老鼠!没有我知道的 Maven 插件,所以我尝试使用 Ant Java 任务构建解析器,如下所示:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}/generated-sources/main/java/" />
<java classpath="lib/xtc.jar" classname="xtc.parser.Rats">
<arg line="-in ${project.build.sourceDirectory}" />
<arg line="-out ${project.build.directory}/generated-sources/main/java/" />
<arg path="${project.build.sourceDirectory}/Dot.rats" />
</java>
</tasks>
<sourceRoot>
${project.build.directory}/generated-sources/main/java
</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
什么老鼠的细节!确实并不重要:最终结果是上面生成Dot.java
并将其放置在
target/generated-sources/main/java
. 它工作正常。
问题是,使用plugin
my 中的这个元素pom.xml
,项目中的任何 Java 文件都不会被编译。
我使用“”生成了一个项目mvn archetype:create -DgroupId=foo -DartifactId=bar
并添加了文件src/main/java/Dot.rats
:
module Dot;
public void Dot = "." !_ ;
(这是一种只接受带有单个点的文件的语法。)
如果我在mvn compile
没有plugin
元素的情况下运行“”,我会得到:
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to
/home/chris/src/tests/maven/project1/bar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Jul 01 18:57:08 EDT 2009
[INFO] Final Memory: 6M/67M
[INFO] ------------------------------------------------------------------------
正在编译的一个Java 文件是src/main/java/foo/App.java
由原型创建的Java 类(即,不是生成的源文件)。
如果我做“ mvn clean
”,然后添加plugin
调用 Rats! 的元素,我得到:
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[mkdir] Created dir:
/home/chris/src/tests/maven/project1/bar/target/generated-sources/main/java
Rats! Parser Generator, v. 1.14.2, (C) 2004-2008 Robert Grimm
Processing /home/chris/src/tests/maven/project1/bar/src/main/java/Dot.rats ...
即,Maven 正在运行 Rats!(这并没有失败,AFAICT)但没有编译任何Java 类,甚至没有编译预先存在的 class App.java
。跑步后,我有target/generated-sources/main/java/Dot.java
但没有target/classes
。
我尝试过其他类型的 Ant 任务,它们不会干扰 Java 编译。例如,如果我用回显任务替换上面的任务元素
<tasks>
<mkdir dir="${project.build.directory}/generated-sources/main/java/" />
<echo file="${project.build.directory}/generated-sources/main/java/Dot.java">
public class Dot { }
</echo>
</tasks>
我明白了
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[INFO] Executed tasks
[INFO] Registering compile source root
/home/chris/src/tests/maven/project1/bar/target/generated-sources/main/java
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 2 source files to
/home/chris/src/tests/maven/project1/bar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Wed Jul 01 19:03:34 EDT 2009
[INFO] Final Memory: 7M/79M
[INFO] ------------------------------------------------------------------------
java
显然,对于 Maven 如何执行任务,我有些不理解。有什么简单的我做错了吗?是否有另一种方法来完成我应该尝试的这项任务(也许是一种更“Maven-native”的方式)?
[更新] 有趣的故事。我尝试用 Maven 插件替换 Ant 任务,方法是编写一个直接RatsMojo
调用的类并将上面的元素替换为xtc.parser.Rats
plugin
<plugin>
<groupId>edu.nyu.xtc</groupId>
<artifactId>maven-xtc-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>rats</goal>
</goals>
<configuration>
<inputDirectory>${project.build.sourceDirectory}</inputDirectory>
<outputDirectory> ${project.build.directory}/generated-sources/main/java</outputDirectory>
<grammarFile>${project.build.sourceDirectory}/Dot.rats</grammarFile>
</configuration>
</execution>
</executions>
</plugin>
它做同样的事情:它运行 Rats!然后终止而不编译项目中的任何Java 文件。