2012-08-13 54 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

相關問題