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

如何从外部属性文件访问 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>