0

我的项目使用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. 它工作正常。

问题是,使用pluginmy 中的这个元素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.Ratsplugin

  <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 文件。

4

2 回答 2

2

问题似乎是老鼠!调用System.exit()(或类似的),终止 JVM。我原以为 Ant 会包装这个(java 任务是从 a 复制过来的build.xml,它不会终止 Ant 构建),但是下面的工作是通过强制 Rats 来工作的!进程到一个单独的JVM:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <executable>java</executable>
    <arguments>
      <argument>-classpath</argument>
      <argument>${basedir}/lib/xtc.jar</argument>
      <argument>xtc.parser.Rats</argument>
      <argument>-in</argument>
      <argument>${project.build.sourceDirectory}</argument> 
      <argument>-out</argument>
      <argument>${project.build.directory}/generated-sources/main/java/</argument>
      <argument>${project.build.sourceDirectory}/Dot.rats</argument>
    </arguments>
  </configuration>
</plugin>

或者,只需添加fork="true"到插件中的java任务antrun

于 2009-07-03T15:58:53.127 回答
1

您需要将新目录添加到 Maven 源目录。可以使用buildhelper-maven-plugin来完成。将此添加到您的 pom 中:

  <build>
   <plugins>
     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>${project.build.directory}/generated-sources/main/java/</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
于 2009-07-02T15:18:05.447 回答