2012-08-13 124 views
2

我编写了一个生成代码的mojo,并将其粘贴在{root}/target/generated-sources/foo下。当我执行:将mojo生成的代码动态添加到源路径

mvn clean install 

我得到这表明没有被包含在构建路径(生成的文件是有的,但在编译阶段没有被拾起)生成的源代码错误。我从this answer了解到,我需要动态地将{root}/target/generated-sources/foo作为POM的源目录。问题是,我无法追查任何有关如何操作的信息。

作为一个备份计划,我打算使用Build Helper Maven Plugin,但我希望如果可能的话,在我的mojo中自动执行此操作。

回答

2

我宁愿把它添加到我的魔:

/** 
    * The current project representation. 
    * @parameter expression="${project}" 
    * @required 
    * @readonly 
    */ 
private MavenProject project; 

/** 
* Directory wherein generated source will be put; main, test, site, ... will be added implictly. 
* @parameter expression="${outputDir}" default-value="${project.build.directory}/src-generated" 
* @required 
*/ 
private File outputDir; 

很明显,你可以改变default-value,以符合您自己的模式。

然后在​​方法:

if (!settings.isInteractiveMode()) { 
    LOG.info("Adding " + outputDir.getAbsolutePath() + " to compile source root"); 
} 
project.addCompileSourceRoot(outputDir.getAbsolutePath()); 
+0

感谢。我已经有了第一个位(即输出位置属性)。这是我试图让第二个工作 - 加入编译源代码。 '项目'对我来说没有定义。看看它是否是一个Maven版本的东西,或其他。但如果您有任何想法,请告诉我。 – 2012-08-13 09:51:04

+0

@KentBoogaart添加了'MavenProperty'属性,该属性将被自动拾取并准备使用。 – maba 2012-08-13 09:52:42

+0

太棒了 - 感谢您的帮助。 – 2012-08-13 09:55:27

相关问题