在Maven插件中使用属性文件中的变量

时间:2018-11-26 20:52:57

标签: java maven

我需要能够对像这样的Maven插件使用“变量”:

                <plugin>
                    <groupId>com.jelastic</groupId>
                    <artifactId>jelastic-maven-plugin</artifactId>
                    <version>1.8.4</version>
                    <configuration>
                        <api_hoster>${api_hoster}</api_hoster>
                        <email>${email}</email>
                        <password>${password}</password>
                        <environment>${environment}</environment>
                        <!--<context>[specify the context if you need it]</context>-->
                        <!--<comment>[insert comment if you need it]</comment>-->
                    </configuration>
                </plugin>

已经在基本目录中设置了属性文件,并使用了插件:

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0-alpha-2</version>
                    <executions>
                        <execution>
                            <phase>initialize</phase>
                            <goals>
                                <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                <files>
                                    <file>${basedir}/jelastic.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

仍然,插件中的变量无法解析,这可能是什么问题?

2 个答案:

答案 0 :(得分:1)

您必须在项目或配置文件中将这些变量声明为<properties>或将它们作为env变量传递,例如mvn whatever -Dyourprop=value

了解属性:https://maven.apache.org/pom.html#Properties

了解个人资料:https://maven.apache.org/guides/introduction/introduction-to-profiles.html

答案 1 :(得分:1)

将这些行添加到您的POM:

    <build>
    <resources>
    <resource>
    <includes>
    <include>**/*.properties</include>
    </includes>
    <directory>src/main/resources</directory>
    </resource>
    </resources>
    </build>

此属性文件:

   api_hoster:${api_hoster}
   email:${email}
   password:${password}
   environment:${environment}

并通过提及个人资料调用任何Maven插件:

mvn clean -Pname
相关问题