Maven:Java类不会在Ant任务之后编译

我的项目使用鼠标生成源代码! 解析器生成器。 老鼠! 没有我知道的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 。 它工作正常。

问题是,使用我的pom.xml这个plugin元素,项目中的Java文件都没有被编译。

我使用“ mvn archetype:create -DgroupId=foo -DartifactId=bar ”生成了一个项目,并添加了src/main/java/Dot.rats

module Dot;

public void Dot = "." !_ ; 

(这是一种只接受单点的文件的语法。)

如果我运行没有plugin元素的“ mvn compile ”,我会得到:

$ 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 ”,然后添加调用Rats!的plugin元素,我会得到:

$ 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正在运行老鼠! (这不是失败,AFAICT),但不编译任何Java类,甚至没有预先存在的类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] ------------------------------------------------------------------------

很显然,我不了解Maven如何执行java任务。 有什么简单的,我做错了吗? 有没有其他方法可以完成我应该尝试的任务(也许更“原生Maven”)?

[更新]有趣的故事。 我尝试用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>

它完全一样:它运行老鼠! 然后终止而不编译项目中的任何Java文件。


问题似乎是老鼠! 调用System.exit() (或类似的),终止JVM。 我原以为Ant会包装这个(java任务是从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"添加到antrun插件中的java任务antrun


您需要将新目录添加到maven源目录。 它可以使用buildhelper-maven-plugin来完成。 添加到你的朋友:

  <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>
链接地址: http://www.djcxy.com/p/60657.html

上一篇: Maven: Java classes don't compile after Ant task

下一篇: 1: error: LNK2019: