简体   繁体   English

Maven从另一个Maven项目生成源

[英]Maven generate sources from another maven project

In my project I have a source code generator that relies on a lot of java code to build a java source that is then stored as a string. 在我的项目中,我有一个源代码生成器,它依赖于许多Java代码来构建Java源代码,然后将其存储为字符串。 Normally I would write the string to a .java file, but I've recently begun switching my project to Maven, and I am looking for a way to have the src file be part of the target folder so it can be used as a dependency for another maven project. 通常,我会将字符串写入.java文件,但是最近我开始将项目切换到Maven,我正在寻找一种方法来将src文件作为目标文件夹的一部分,以便可以将其用作依赖项。用于另一个Maven项目。

Maven as build tool has conventions for directory layouts in the module (like src/main/java , src/main/resources , you've probably seen this stuff already). Maven作为构建工具在模块中具有目录布局的约定(例如src/main/javasrc/main/resources ,您可能已经看到了这些东西)。 All the compiled classes are put into a folder called target during the compile phase. 在编译阶段,所有已编译的类都放在一个名为target的文件夹中。 Later on, during the package phase the jar is created inside this target folder. 稍后,在package阶段,将在此target文件夹中创建jar。

Now, for generated sources, there is also a kind of convention, although probably less known: 现在,对于生成的源,还有一种约定,尽管可能鲜为人知:

If you generate sources you should place them into: 如果生成源,则应将它们放入:

target/generated-sources

When you do this, your IDEs (IntelliJ at least) will use them as a source files. 执行此操作时,您的IDE(至少是IntelliJ)将把它们用作源文件。

A process of source generation in maven is usually triggered from maven plugins attached to one of the early phases. Maven中的源代码生成过程通常是由连接到早期阶段之一的Maven插件触发的。 Then, when it comes to the package phase these sources will be added into the jar, just like any other .class file compiled as a binary source. 然后,当涉及到package阶段时,这些源将被添加到jar中,就像其他任何编译为二进制源的.class文件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM