2015-12-22 93 views
1

我試圖創建一個maven原型以從預定義模板生成許多項目。未替換屬性的maven原型

原型生成的模板項目是Spring Boot服務。以下爲原型項目文件夾樹的內容:

my-archetype 
¦ pom.xml 
¦ 
+---src 
¦ +---main 
¦ ¦ +---resources 
¦ ¦  +---archetype-resources 
¦ ¦  ¦ ¦ mvnw 
¦ ¦  ¦ ¦ mvnw.cmd 
¦ ¦  ¦ ¦ pom.xml 
¦ ¦  ¦ ¦ rebuild.sh 
¦ ¦  ¦ ¦ 
¦ ¦  ¦ +---src 
¦ ¦  ¦  +---main 
¦ ¦  ¦  ¦ +---docker 
¦ ¦  ¦  ¦ ¦  Dockerfile 
¦ ¦  ¦  ¦ ¦ 
¦ ¦  ¦  ¦ +---java 
¦ ¦  ¦  ¦ ¦  __moduleClassName__ServiceApplication.java 
¦ ¦  ¦  ¦ ¦  __moduleClassName__ServiceController.java 
¦ ¦  ¦  ¦ ¦ 
¦ ¦  ¦  ¦ +---resources 
¦ ¦  ¦  ¦   application.yml 
¦ ¦  ¦  ¦   bootstrap.yml 
¦ ¦  ¦  ¦ 
¦ ¦  ¦  +---test 
¦ ¦  ¦   +---java 
¦ ¦  ¦     __moduleClassName__ServiceApplicationTests.java 
¦ ¦  ¦ 
¦ ¦  +---META-INF 
¦ ¦   +---maven 
¦ ¦     archetype-metadata.xml 
¦ ¦ 
... 

${moduleClassName}定義爲允許類名的定製自定義屬性。我使用mvn clean install在本地存儲庫中安裝原型,然後嘗試使用mvn archetype:generate ... -DmoduleClassName=<myModule>從其創建項目。

一切似乎正常工作,除了,* .java文件正確命名和放置在正確的pacakges(和它們內部的字符串也被替換爲屬性值),對於其他文件替換不會發生。尤其這是src\main\resources\archetype-resources\src\main\resources\bootstrap.yml

spring: 
    application: 
     name: ${project.artifactId} 

內容,這是我想${project.artifactId}變量,在這兩種情況下被替換的src\main\resources\archetype-resources\src\main\docker\Dockerfile

FROM java:8 
VOLUME /tmp 
ADD ${project.artifactId}.jar app.jar 
RUN bash -c 'touch /app.jar' 
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 

的內容,同時也不要在發生生成的項目。

這是我的原型 - metadata.xml中的相關部分:

<?xml version="1.0" encoding="UTF-8"?> 
<archetype-descriptor ... > 
    <fileSets> 
    <fileSet filtered="true" packaged="true" encoding="UTF-8"> 
     <directory>src/main/java</directory> 
     <includes> 
     <include>**/*.java</include> 
     </includes> 
    </fileSet> 
    <fileSet filtered="true" encoding="UTF-8"> 
     <directory>src/main/docker</directory> 
     <includes> 
     <include>**/Dockerfile</include> 
     </includes> 
    </fileSet> 
    <fileSet filtered="true" encoding="UTF-8"> 
     <directory>src/main/resources</directory> 
     <includes> 
     <include>**/*.yml</include> 
     </includes> 
    </fileSet> 
    <fileSet filtered="true" packaged="true" encoding="UTF-8"> 
     <directory>src/test/java</directory> 
     <includes> 
     <include>**/*.java</include> 
     </includes> 
    </fileSet> 
    . 
    . 
    . 
    </fileSets> 
    <requiredProperties> 
     <requiredProperty key="moduleClassName"/> 
    </requiredProperties> 
</archetype-descriptor> 

回答

1

最後,我發現通過試錯的解決方案:這個問題剛纔我是用變量名的語法錯誤,使用${project.artifactId}我應該寫簡單${artifactId}