Maven:代码生成之前的部分编译 [英] Maven: partial compilation before code generation

查看:95
本文介绍了Maven:代码生成之前的部分编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr-edition:我有一个我知道会失败的编译,但是想要在我的目标/类编译后的文件夹。我已经配置了< failOnError> false< / failOnError> ,但是没有生成类,甚至没有生成任何其他类的虚拟类,除了对象。是否有一些配置来实现这一目标?

tl;dr-edition: I have a compilation I know that will fail, but want the subset of classes that are still compilable in my target/classes folder after compilation. I have configured <failOnError>false</failOnError>, but no classes are generated, not even a dummy class that is independent of any other classes except Object. Is there some configuration to achieve this?

我有一个maven驱动的项目,其工作流程基本上包括以下内容(相关的) )目标:

I have a maven-powered project whose workflow consists of basically the following (relevant) goals:


  • ...

  • init-compile

代码生成器(下面)使用基于反射的配置所以,在第一遍中,我想尝试尽可能多地编译项目,以便不会抛出ClassNotFoundExceptions。此编译配置为< failOnError> false< / failOnError> ,以便继续构建。

The code generator (below) uses a config that is reflection-based, so, in a first pass, I want to try compile as much of the project as possible so that no ClassNotFoundExceptions are thrown there. This compilation is configured with <failOnError>false</failOnError> so that the build continues.

不幸的是(您可以将其称为设计缺陷),配置用于代码生成(指定OWL文件和命名空间以封装映射)和运行时,因此它还包含代码生成器不需要的其他元素,但仍然读取并因此需要在类路径上成功。

Unfortunately (you could call it a design flaw), the config is used both for code generation (specifying the OWL file and namespace to package mappings) and at runtime, so it also contains other elements that are not needed for the code generator, but are still read and therefore needed on the classpath to succeed.

generate-model

在这一步中,一些模型类是从OWL-Ontology生成的,创建了使项目其余部分完全可编译的代码。

In this step, some model classes are generated from an OWL-Ontology, creating the code that makes the rest of the project completely compileable.

default-compile

现在,应该编译其余的类,显然

Now, the rest of the classes should be compiled, obviously

save-model

现在,来自本体的实例被读取并序列化为运行时文件

Now, the instances from the ontology are read and serialized to a file for runtime

...

附注:生成并保存模型使用 maven-exec-plugin ,但我真诚地认为不重要全部。

Side note: both generate and save model use maven-exec-plugin, but I sincerely don't think that matters at all.

问题:

当我使用<$ c运行构建时$ c> mvn -e -U清理包源:jar javadoc:jar install:install ,它在 generate-model 目标期间失败我试图避免的错误。 target / classes 是空的,因此编译器似乎没有吐出它可以/应该能够处理的类的子集。有没有办法实现这个目标?

When I run my build with mvn -e -U clean package source:jar javadoc:jar install:install, it fails during the generate-model goal with the errors I'm trying to avoid. target/classes is empty, so it seems that the compiler doesn't spit out the subset of classes it could/should have been able to process. Is there a way to achieve this?

我有两个我不喜欢的解决方法:

I have two workarounds in mind which I both don't like:


  • 在将配置文件解析为Java-Objects之前编辑配置文件AST,以便只解析与代码生成器相关的部分(需要调整我有权访问的代码,但应该考虑我的项目不可变);

  • 并配置 init-compile 目标只包括所需的类(太不灵活了,因为POM应该/可能是使用相同模型的未来应用程序的模板。)

  • Editing the config file "AST" before parsing it into Java-Objects, so that only the part relevant for the code generator is parsed (needs tweaking of code that I have access to but should be considered immutable by my project);
  • and to configure the init-compile goal to only include the needed classes (too inflexible, because the POM should/could be a template for future applications using the same model).

如果你能想象另一种方法来解决我的问题你可以从我的描述中看到,我也很高兴听到他们的声音!

If you can imagine another way to work around my problem that you can see from my description, I would be glad to hear them, too!

推荐答案

首先,让我概括一下你的问题确定我已经正确掌握了它。

First, let me recapitulate your problem to be sure I've grasped it properly.


  • 你有一组类,其编译形式的函数是
    配置代码gen erator和运行时。 (
    的一个子集与代码生成器相关,但是除非完整配置存在,否则生成将失败
    。所以我们可以将其视为
    ,尽管整个配置是必要的。)

  • You have a set of classes whose function, in their compiled form, is to configure both the code generator and the runtime. (A subset of these are relevant to the code generator, but generation will fail unless the complete configuration is present. So we can treat it as though the entire configuration were necessary.)

然后你有一组将作为源
代码生成的类。它们具有生成时间,可能是编译时,以及对配置类的
运行时依赖性。

You then have a set of classes which will be generated as source code. These have a generation-time, perhaps a compile-time, and a run-time dependency upon the configuration classes.

最后你有一些生成的类具有编译时依赖性
的其他代码,以及对
生成的类和配置类的运行时依赖性。

Finally you have some other code which has a compile-time dependency upon the generated classes, and a runtime dependency upon both the generated classes and the configuration classes.

但是,您的配置类对生成的类和其他代码没有任何编译时依赖性。你没有明确说出来,但我假设它,否则你就会遇到循环依赖问题。

However, your configuration classes don't have any compile-time dependencies upon the generated classes nor upon the other code. You don't explicitly say this, but I'm assuming it, otherwise you've got a circular dependency problem.

以下是我的建议:将项目划分为多模块(reactor)项目。您当前的项目将是反应堆项目的一个模块。创建一个名为config或类似的新模块,并将配置类移入其中。让主模块依赖它。

Here's my suggestion: Divide the project into a multi-module ("reactor") project. Your current project will be a module of the reactor project. Create a new module called "config" or similar and move your config classes into it. Have the main module depend upon it.

如果你不喜欢多模块项目,你可以通过声明额外执行编译插件来实现同样的目的,绑定到生成源阶段。 (你没有说,但我假设你正在这个阶段进行代码生成。如果你在POM中的代码生成器插件之前声明编译插件,Maven将以相同的顺序执行它们。)你会使用编译插件的include过滤器只编译配置类。为此,您需要将配置类与其他所有包放在一个单独的包中,这无论如何都是很好的做法。

If you don't like multi-module projects, you could achieve the same thing by declaring an extra execution of the compile plugin, bound to the generate-sources phase. (You don't say, but I assume you're doing the code generation in this phase. If you declare the compile plugin before the code generator plugin within the POM, Maven will execute them in the same order.) You would use the "include" filter of the compile plugin to compile only the config classes. For this, you would need to have the config classes in a separate package from all the rest, which is good practice anyway.

这篇关于Maven:代码生成之前的部分编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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