通过在Maven中运行项目的Java类来生成源 [英] Generating sources by running a project's java class in Maven

查看:132
本文介绍了通过在Maven中运行项目的Java类来生成源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个大型的Ant构建转换为Maven.作为Ant构建的一部分,我们有几个步骤通过调用项目的一个类来创建Java类,简化为:

I'm converting a largish Ant build to Maven. As part of the Ant build, we have several steps which created Java classes by invoking one of the project's classes, simplified as:

javac SomeGenerator.java
java  SomeGenerator  generated # generate classes in generated/
javac generated/*.java

我已经将每个生成器拆分到其自己的Maven模块中,但是由于无法在generate-sources阶段对其进行编译,因此存在无法运行该生成器的问题.

I've split each generator in its own Maven module, but I have the problem of not being able to run the generator since it's not yet compiled in the generate-sources phase.

我尝试过类似的操作

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1.1</version>
            <executions>
                <execution>
                    <id>generate-model</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <phase>generate-sources</phase>

                    <configuration>
                        <mainClass>DTOGenerator</mainClass>
                        <arguments>
                            <argument>${model.generated.dir}</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

遗憾的是,由于上述原因,该方法不起作用.将代码生成器分别分成两个项目,一个用于编译生成器,另一个用于生成DTO,似乎有些过分.

Which sadly does not work, for the reasons outlined above. Splitting the code generators into two projects each, one for compiling the generator and another for generating the DTOs seems overkill.

有什么替代品?

使用Maven 2.2.1

Using Maven 2.2.1.

推荐答案

您可以在generate-sources阶段执行maven-compile-plugin.只需在现有执行之前添加另一个执行,然后对其进行配置,以便它可以为生成器选择源.

You can execute the maven-compile-plugin in the generate-sources phase. Just add another execution before the existing execution and configure it so that it just picks up the sources for the generator.

或将项目分为两部分:使用单独的POM构建生成器,并将生成器库包括为生成源的POM的依赖项.

Or split the project in two: build the generator with a separate POM and include the generator library as a dependency to the POM that's generating the sources.

我个人将拆分项目.使构建文件更整洁,更易于维护.

Personally I would split the project. Keeps the build files cleaner and easier to maintain.

这篇关于通过在Maven中运行项目的Java类来生成源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆