如何从外部属性文件访问pom.xml中的变量?

时间:2017-02-08 07:57:05

标签: maven

我需要通过外部属性文件中pom.xml的密钥获取价值。属性文件的位置在src/main/resources/dev.properties中。 我试图通过使用maven中的属性来解决这个问题。请帮我。     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

2 个答案:

答案 0 :(得分:2)

您必须使用属性插件:

<强>插件

<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 Command

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

<强>替代地

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

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

答案 1 :(得分:1)

首先,在pom.xml中定义properties标记并启用资源过滤(在build标记下),

<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>

    <properties>
        <Project.Name>My-Project-Name</Project.Name>
        <path>/home/user/warfile</path>
    </properties>

    <dependencies>
        .....
    </dependencies>
    ......

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    ........

假设您的dev.properties文件位于“ src / main / resources”文件夹下,则将dev.properties文件更新为;

projectPath=${path}
Project.Name=${Project.Name}
project.name=${project.name}

maven构建后,dev.properties的内容将如下:

projectPath=/home/user/warfile
Project.Name=My-Project-Name
project.name=Project Maven Webapp

但是,如果使用的是Spring Boot(最新版本),则需要更新dev.properties文件,例如:
(使用@@标记代替$ {}标记)

projectPath=@path@
Project.Name=@Project.Name@
project.name=@project.name@

在构建Maven之后将产生相同的内容,例如:

projectPath=/home/user/warfile
Project.Name=My-Project-Name
project.name=Project Maven Webapp