maven archetype不替换属性

时间:2015-12-22 16:53:07

标签: spring maven maven-archetype

我试图创建一个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}

这是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"]

我希望${project.artifactId}变量在两种情况下都被替换,而不会在生成的项目中发生。

这些是我的archetype-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 个答案:

答案 0 :(得分:2)

最后,我通过反复试验找到了解决方案:问题只是我使用了错误的变量名语法,使用${project.artifactId}我应该只写${artifactId}