如何从外部属性文件访问 pom.xml 中的变量? [英] how to access variable in pom.xml from external properties file?

查看:42
本文介绍了如何从外部属性文件访问 pom.xml 中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 pom.xml 中的一个键从外部属性文件中获取值.属性文件的位置在 src/main/resources/dev.properties 中.我试图通过在 maven 中使用属性来解决这个问题.请帮我.pom.xml:

I need to get value through a key in pom.xml from external properties files. Location of properties file is in src/main/resources/dev.properties. I have tried to solve this by using properties in maven. Please help me. pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Simple_project</groupId>
    <artifactId>Project</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Project Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${Project.Name}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <outputDirectory>${path}</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
    </build>

</project>

// properties File

dev.properties:

projectPath=/home/user/warfile
Project.Name = newProject

Thanks & Regards,
Tharanya B

推荐答案

你必须使用属性插件:

插件

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

Mvn 命令

mvn -DpropertiesFile=dev.properties properties:read-project-properties clean install

或者

您可以对文件进行硬编码:

You can hardcode the file:

<file>${basedir}/dev.properties</file>

这篇关于如何从外部属性文件访问 pom.xml 中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆