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

发布于 2024-07-26 04:28:19 字数 5996 浏览 3 评论 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 并将其放入 目标/生成源/main/java。 效果很好。

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

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

module Dot;

public void Dot = "." !_ ; 

这是一种仅接受带有单个点的文件的语法。)

如果我运行“mvncompile”而不带< code>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”,然后添加调用 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 正在运行 Rats! (这并没有失败,AFAICT)但不编译任何 Java 类,甚至不编译预先存在的类App.java。 运行后,我有 target/ generated-sources/main/java/Dot.java 但没有 target/classes

我尝试过其他类型的 Ant 任务,它们不会干扰 Java 汇编。 例如,如果我将上面的任务元素替换为 echo

<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-native”的方式)?

[更新]有趣的故事。 我尝试用 Maven 插件替换 Ant 任务,方法是编写直接调用 xtc.parser.RatsRatsMojo 类并替换 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 文件。

My project generates source code using the Rats! parser generator. Rats! doesn't have a Maven plugin that I'm aware of, so I'm trying to build the parser using an Ant Java
task, like so:

 <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>

The details of what Rats! does aren't important: the end result is that
the above generates Dot.java and places it in
target/generated-sources/main/java. It works fine.

The problem is that, with this plugin element in my pom.xml, none of
the Java files in the project get compiled.

I generated a project
using "mvn archetype:create -DgroupId=foo -DartifactId=bar" and added the
file src/main/java/Dot.rats:

module Dot;

public void Dot = "." !_ ; 

(This is a grammar that accepts only files with a single dot.)

If I run "mvn compile" without the plugin element, I get:

$ 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] ------------------------------------------------------------------------

Where the one Java file being compiled is src/main/java/foo/App.java, a Java class created by the archetype (i.e., not a generated source file).

If I do "mvn clean" then add the plugin element invoking Rats!, I get:

$ 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 ...

I.e., Maven is running Rats! (which is not failing, AFAICT) but not compiling any Java classes, not even the pre-existing class App.java. After the run, I have target/generated-sources/main/java/Dot.java but no target/classes.

I've tried other kinds of Ant tasks and they don't interfere with Java
compilation. For example, if I replace the task element above with an
echo task

<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>

I get

$ 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] ------------------------------------------------------------------------

Obviously there's something I'm not understanding about how Maven
executes the java task. Is there something simple that I'm doing
wrong? Is there an alternative way to accomplish this task that I
should try (perhaps a more "Maven-native" way)?

[UPDATE] Funny story. I tried replacing the Ant task with a Maven plugin, by writing a RatsMojo class that invokes xtc.parser.Rats directly and replacing the plugin element above with

  <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>

It does the exact same thing: it runs Rats! then terminates without compiling any of the Java files in the project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后彩虹 2024-08-02 04:28:19

问题似乎出在老鼠身上! 调用 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 任务中。

The problem seems to be that Rats! calls System.exit() (or similar), terminating the JVM. I would have thought that Ant would wrap this (the java task is copied over from a build.xml and it doesn't terminate the Ant build), but the following works by forcing the Rats! process into a separate 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>

or, just add fork="true" to the java task in the antrun plugin.

阳光下的泡沫是彩色的 2024-08-02 04:28:19

您需要将新目录添加到 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>

You need to add the new directory to the maven source directories. It can be done using the buildhelper-maven-plugin. Add this to your 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文