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

问题描述 投票:0回答:1

我需要通过外部属性文件中的

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
maven
1个回答
5
投票

你必须使用属性插件:

插件

        <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

或者

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

<file>${basedir}/dev.properties</file>
© www.soinside.com 2019 - 2024. All rights reserved.